diff --git a/headers/posix/time.h b/headers/posix/time.h index ae8ce3f100..aa5be1b204 100644 --- a/headers/posix/time.h +++ b/headers/posix/time.h @@ -67,6 +67,7 @@ extern struct tm *gmtime(const time_t *timer); extern struct tm *gmtime_r(const time_t *timer, struct tm *tm); extern struct tm *localtime(const time_t *timer); extern struct tm *localtime_r(const time_t *timer, struct tm *tm); +extern int nanosleep(const struct timespec *, struct timespec *); extern size_t strftime(char *buffer, size_t maxSize, const char *format, const struct tm *tm); extern char *strptime(const char *buf, const char *format, struct tm *tm); diff --git a/src/system/libroot/posix/time/Jamfile b/src/system/libroot/posix/time/Jamfile index b1de2b4a73..c451ccd1eb 100644 --- a/src/system/libroot/posix/time/Jamfile +++ b/src/system/libroot/posix/time/Jamfile @@ -12,6 +12,7 @@ MergeObject posix_time.o : ctime.c difftime.c localtime.c + nanosleep.c stime.c strftime.c strptime.c diff --git a/src/system/libroot/posix/time/nanosleep.c b/src/system/libroot/posix/time/nanosleep.c new file mode 100644 index 0000000000..2f7a0cb9d2 --- /dev/null +++ b/src/system/libroot/posix/time/nanosleep.c @@ -0,0 +1,35 @@ +/* +** Copyright 2007, Jérôme Duval. All rights reserved. +** Distributed under the terms of the Haiku License. +*/ +#include +#include +#include + +int +nanosleep(const struct timespec *rqtp, struct timespec *rmtp) +{ + bigtime_t ms, begin_ms; + status_t err; + + if (!rqtp) { + errno = EINVAL; + return -1; + } + + // round up tv_nsec + ms = rqtp->tv_sec * 1000000LL + (rqtp->tv_nsec + 999) / 1000; + begin_ms = system_time(); + err = snooze(ms); + if (err == B_INTERRUPTED) { + errno = EINTR; + if (rmtp) { + bigtime_t remain_ms = ms - system_time() + begin_ms; + rmtp->tv_sec = remain_ms / 1000000; + rmtp->tv_nsec = (remain_ms - rmtp->tv_sec) * 1000; + } + return -1; + } + return 0; +} +