xref: /haiku/src/system/libroot/posix/time/nanosleep.c (revision 1294543de9ac0eff000eaea1b18368c36435d08e)
1 /*
2 ** Copyright 2007, Jérôme Duval. All rights reserved.
3 ** Distributed under the terms of the Haiku License.
4 */
5 #include <errno.h>
6 #include <time.h>
7 #include <OS.h>
8 
9 int
10 nanosleep(const struct timespec *rqtp, struct timespec *rmtp)
11 {
12 	bigtime_t ms, begin_ms;
13 	status_t err;
14 
15 	if (!rqtp) {
16 		errno = EINVAL;
17 		return -1;
18 	}
19 
20 	// round up tv_nsec
21 	ms = rqtp->tv_sec * 1000000LL + (rqtp->tv_nsec + 999) / 1000;
22 	begin_ms = system_time();
23 	err = snooze(ms);
24 	if (err == B_INTERRUPTED) {
25 		errno = EINTR;
26 		if (rmtp) {
27 			bigtime_t remain_ms = ms - system_time() + begin_ms;
28 			rmtp->tv_sec = remain_ms / 1000000;
29 			rmtp->tv_nsec = (remain_ms - rmtp->tv_sec) * 1000;
30 		}
31 		return -1;
32 	}
33 	return 0;
34 }
35 
36