add legacy getloadavg api
[musl.git] / src / thread / __timedwait.c
blob302273ae6a7712ccbb6fb911af82f243405a41ed
1 #include <pthread.h>
2 #include <time.h>
3 #include <errno.h>
4 #include "futex.h"
5 #include "syscall.h"
7 static int do_wait(volatile int *addr, int val,
8 clockid_t clk, const struct timespec *at, int priv)
10 int r;
11 struct timespec to, *top=0;
13 if (at) {
14 if (at->tv_nsec >= 1000000000UL) return EINVAL;
15 if (clock_gettime(clk, &to)) return EINVAL;
16 to.tv_sec = at->tv_sec - to.tv_sec;
17 if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) {
18 to.tv_sec--;
19 to.tv_nsec += 1000000000;
21 if (to.tv_sec < 0) return ETIMEDOUT;
22 top = &to;
25 r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT, val, top);
26 if (r == EINTR || r == EINVAL || r == ETIMEDOUT) return r;
27 return 0;
30 int __timedwait(volatile int *addr, int val,
31 clockid_t clk, const struct timespec *at,
32 void (*cleanup)(void *), void *arg, int priv)
34 int r, cs;
36 if (!cleanup) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
37 pthread_cleanup_push(cleanup, arg);
39 r = do_wait(addr, val, clk, at, priv);
41 pthread_cleanup_pop(0);
42 if (!cleanup) pthread_setcancelstate(cs, 0);
44 return r;