1#include <errno.h>
2#include <pthread.h>
3#include <time.h>
4#include "musl_features.h"
5#include "syscall.h"
6
7int __clock_nanosleep(clockid_t clk, int flags, const struct timespec * req, struct timespec * rem)
8{
9 if (clk == CLOCK_THREAD_CPUTIME_ID)
10 return EINVAL;
11 int old_cancel_type;
12 int status;
13 /// We cannot port __syscall_cp because musl has very limited cancellation point implementation.
14 /// For example, c++ destructors won't get called and exception unwinding isn't implemented.
15 /// Instead, we use normal __syscall here and turn on the asynchrous cancel mode to allow
16 /// cancel. This works because nanosleep doesn't contain any resource allocations or
17 /// deallocations.
18 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old_cancel_type);
19 if (clk == CLOCK_REALTIME && !flags)
20 status = -__syscall(SYS_nanosleep, req, rem);
21 else
22 status = -__syscall(SYS_clock_nanosleep, clk, flags, req, rem);
23 pthread_setcanceltype(old_cancel_type, NULL);
24 return status;
25}
26
27weak_alias(__clock_nanosleep, clock_nanosleep);
28