From e4bbf18b3fc5d04509a19b665446ea8f69ca61dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Tue, 25 Sep 2007 19:18:27 +0000 Subject: [PATCH] added an implementation for nanosleep() based on snooze() git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22311 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/posix/time.h | 1 + src/system/libroot/posix/time/Jamfile | 1 + src/system/libroot/posix/time/nanosleep.c | 35 +++++++++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 src/system/libroot/posix/time/nanosleep.c 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; +} +