From: Alan Cox Date: Fri, 2 Jan 2015 18:06:27 +0000 (+0000) Subject: libc: More of the POSIX time APIs X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=505a8a7da7cbdd4f7a471dff87af5e863e4e3e33;p=FUZIX.git libc: More of the POSIX time APIs --- diff --git a/Library/libs/clock_getres.c b/Library/libs/clock_getres.c new file mode 100644 index 00000000..c5bc7002 --- /dev/null +++ b/Library/libs/clock_getres.c @@ -0,0 +1,17 @@ +#include +#include +#include + +int clock_getres(clockid_t clk_id, struct timespec *res) +{ + res->tv_nsec = 0; + res->tv_sec = 1; + switch(clk_id) { + case CLOCK_REALTIME: + case CLOCK_MONOTONIC: + return 0; + default: + errno = EINVAL; + } + return -1; +} \ No newline at end of file diff --git a/Library/libs/clock_gettime.c b/Library/libs/clock_gettime.c new file mode 100644 index 00000000..6b605760 --- /dev/null +++ b/Library/libs/clock_gettime.c @@ -0,0 +1,19 @@ +#include +#include +#include + +int clock_gettime(clockid_t clk_id, struct timespec *res) +{ + res->tv_nsec = 0; + switch(clk_id) { + case CLOCK_REALTIME: + _time(&res->tv_sec, 0); + return 0; + case CLOCK_MONOTONIC: + _time(&res->tv_sec, 1); + return 0; + default: + errno = EINVAL; + } + return -1; +} \ No newline at end of file diff --git a/Library/libs/clock_settime.c b/Library/libs/clock_settime.c new file mode 100644 index 00000000..084fa359 --- /dev/null +++ b/Library/libs/clock_settime.c @@ -0,0 +1,17 @@ +#include +#include +#include + +int clock_settime(clockid_t clk_id, const struct timespec *tp) +{ + switch(clk_id) { + case CLOCK_REALTIME: + _stime(&tp->tv_sec, 0); + return 0; + case CLOCK_MONOTONIC: + return -EPERM; + default: + errno = EINVAL; + } + return -1; +} \ No newline at end of file diff --git a/Library/libs/gettimeofday.c b/Library/libs/gettimeofday.c new file mode 100644 index 00000000..22db97f8 --- /dev/null +++ b/Library/libs/gettimeofday.c @@ -0,0 +1,21 @@ +#define _BSD_SOURCE + +#include +#include + +int gettimeofday(struct timeval *tv, struct timezone *tz) +{ + if (tv) { + _time(&tv->tv_sec, 0); + tv->tv_usec = 0; + } + if (tz) { + /* FIXME: although this is obsolete */ + tz->tz_minuteswest = 0; + /* and this is not used */ + tz->tz_dsttime = 0; + } + return 0; +} + + \ No newline at end of file diff --git a/Library/libs/nanosleep.c b/Library/libs/nanosleep.c new file mode 100644 index 00000000..c85e599a --- /dev/null +++ b/Library/libs/nanosleep.c @@ -0,0 +1,49 @@ +#include +#include +#include + +/* Ok so this doesn't score too highly on accuracy but it'll wrap code using + the modern API's for long sleeps just fine */ + +int clock_nanosleep(clockid_t clock, int flags, const struct timespec *req, struct timespec *rem) +{ + time_t tbase; + time_t tend; + uint16_t t = req->tv_sec * 10; + long nsec = req->tv_nsec; + + if (clock != CLOCK_REALTIME && clock != CLOCK_MONOTONIC) { + errno = EINVAL; + return -1; + } + + /* Will be 0-9 range so avoid costly divides */ + while(nsec > 50000000UL) { + nsec -= 50000000UL; + t++; + } + + if (_time(&tbase, clock) == -1) + return -1; + + if (flags & TIMER_ABSTIME) { + if (tbase < req->tv_sec) + return 0; + t += (tbase - req->tv_sec) * 10; + } + if (_pause(t) == 0) + return 0; + _time(&tend, clock); + if (rem) { + rem->tv_sec = tbase + t/10 - tend; + rem->tv_nsec = 0; + } + return -1; +} + + +int nanosleep(const struct timespec *req, struct timespec *rem) +{ + return clock_nanosleep(CLOCK_REALTIME, 0, req, rem); +} + diff --git a/Library/libs/settimeofday.c b/Library/libs/settimeofday.c new file mode 100644 index 00000000..86115735 --- /dev/null +++ b/Library/libs/settimeofday.c @@ -0,0 +1,13 @@ +#define _BSD_SOURCE + +#include +#include + +int settimeofday(struct timeval *tv, const struct timezone *tz) +{ + int ret = 0; + if (tv) { + ret = _stime(&tv->tv_sec, 0); + } + return ret; +}