Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/sys/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ static inline int beginthread(void (*start)(void *), unsigned int priority, void
extern int threadsinfo(int n, threadinfo_t *info);


extern time_t getThreadCpuTime(void);


extern int priority(int priority);


Expand Down
9 changes: 5 additions & 4 deletions include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#define _LIBPHOENIX_TIME_H_


#define SECS_TO_USECS_T(secs) (1000000ULL * (secs))
#define SECS_TO_USECS_T(secs) (1000000ULL * (secs))
#define MSECS_TO_USECS_T(msecs) (1000ULL * (msecs))

#include <sys/types.h>
Expand All @@ -26,9 +26,10 @@
#define CLOCKS_PER_SEC 1000000


#define CLOCK_MONOTONIC 0
#define CLOCK_MONOTONIC_RAW 1
#define CLOCK_REALTIME 2
#define CLOCK_MONOTONIC 0
#define CLOCK_MONOTONIC_RAW 1
#define CLOCK_REALTIME 2
#define CLOCK_THREAD_CPUTIME_ID 3


#ifdef __cplusplus
Expand Down
24 changes: 15 additions & 9 deletions time/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <ctype.h>
#include <stdio.h>
#include <limits.h>
#include <sys/threads.h>


char *tzname[2];
Expand Down Expand Up @@ -91,26 +92,31 @@ time_t time(time_t *tp)

int clock_gettime(clockid_t clk_id, struct timespec *tp)
{
time_t now, offs;
time_t time, offs;

if (tp == NULL) {
errno = EINVAL;
return -1;
}

if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW) {
if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW && clk_id != CLOCK_THREAD_CPUTIME_ID) {
errno = EINVAL;
return -1;
}

gettime(&now, &offs);

if (clk_id == CLOCK_REALTIME)
now += offs;
if (clk_id == CLOCK_THREAD_CPUTIME_ID) {
time = (unsigned int)getThreadCpuTime();
}
else {
gettime(&time, &offs);
if (clk_id == CLOCK_REALTIME) {
time += offs;
}
}

tp->tv_sec = now / (1000 * 1000);
now -= tp->tv_sec * 1000 * 1000;
tp->tv_nsec = now * 1000;
tp->tv_sec = time / (1000 * 1000);
time -= tp->tv_sec * 1000 * 1000;
tp->tv_nsec = time * 1000;

return EOK;
}
Expand Down
Loading