From bd5969fc876a10b18613302db7087ef3c40f18e1 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 7 Mar 2024 17:28:00 -0500 Subject: lib: Add mlibc Signed-off-by: Ian Moffett --- lib/mlibc/options/posix/generic/sys-time-stubs.cpp | 107 +++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 lib/mlibc/options/posix/generic/sys-time-stubs.cpp (limited to 'lib/mlibc/options/posix/generic/sys-time-stubs.cpp') diff --git a/lib/mlibc/options/posix/generic/sys-time-stubs.cpp b/lib/mlibc/options/posix/generic/sys-time-stubs.cpp new file mode 100644 index 0000000..5cc0fe5 --- /dev/null +++ b/lib/mlibc/options/posix/generic/sys-time-stubs.cpp @@ -0,0 +1,107 @@ + +#include +#include +#include + +#include +#include +#include + +int gettimeofday(struct timeval *__restrict result, void *__restrict unused) { + (void)unused; // Linux just ignores gettimeofday(). + + if(result) { + long nanos; + if(int e = mlibc::sys_clock_get(CLOCK_REALTIME, &result->tv_sec, &nanos); e) { + errno = e; + return -1; + } + result->tv_usec = nanos / 1000; + } + return 0; +} + +int settimeofday(const struct timeval *, const struct timezone *) { + __ensure(!"Not implemented"); + __builtin_unreachable(); +} + +void timeradd(const struct timeval *a, const struct timeval *b, struct timeval *res) { + res->tv_sec = a->tv_sec + b->tv_sec; + res->tv_usec = a->tv_usec + b->tv_usec; + while(res->tv_usec > 999999) { + res->tv_usec -= 1000000; + res->tv_sec += 1; + } +} + +void timersub(const struct timeval *a, const struct timeval *b, struct timeval *res) { + res->tv_sec = a->tv_sec - b->tv_sec; + res->tv_usec = a->tv_usec - b->tv_usec; + while(res->tv_usec < 0) { + res->tv_usec += 1000000; + res->tv_sec -= 1; + } +} + +void timerclear(struct timeval *tvp) { + tvp->tv_sec = 0; + tvp->tv_usec = 0; +} + +int timerisset(struct timeval *tvp) { + if(tvp->tv_sec != 0 || tvp->tv_usec != 0) { + return 1; + } + return 0; +} + +int getitimer(int which, struct itimerval *curr_value) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_getitimer, -1); + if(int e = mlibc::sys_getitimer(which, curr_value); e) { + errno = e; + return -1; + } + return 0; +} + +int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_setitimer, -1); + if(int e = mlibc::sys_setitimer(which, new_value, old_value); e) { + errno = e; + return -1; + } + return 0; +} + +int timer_create(clockid_t clk, struct sigevent *__restrict evp, timer_t *__restrict res) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_timer_create, -1); + if(int e = mlibc::sys_timer_create(clk, evp, res); e) { + errno = e; + return -1; + } + return 0; +} + +int timer_settime(timer_t t, int flags, const struct itimerspec *__restrict val, struct itimerspec *__restrict old) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_timer_settime, -1); + if(int e = mlibc::sys_timer_settime(t, flags, val, old); e) { + errno = e; + return -1; + } + return 0; +} + +int timer_gettime(timer_t, struct itimerspec *) { + __ensure(!"Not implemented"); + __builtin_unreachable(); +} + +int timer_delete(timer_t t) { + MLIBC_CHECK_OR_ENOSYS(mlibc::sys_timer_delete, -1); + if(int e = mlibc::sys_timer_delete(t); e) { + errno = e; + return -1; + } + return 0; +} -- cgit v1.2.3