7 int __pthread_setcancelstate(int, int *);
8 int __clock_gettime(clockid_t, struct timespec *);
10 int __timedwait(volatile int *addr, int val,
11 clockid_t clk, const struct timespec *at,
12 void (*cleanup)(void *), void *arg, int priv)
15 struct timespec to, *top=0;
20 if (at->tv_nsec >= 1000000000UL) return EINVAL;
21 if (__clock_gettime(clk, &to)) return EINVAL;
22 to.tv_sec = at->tv_sec - to.tv_sec;
23 if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) {
25 to.tv_nsec += 1000000000;
27 if (to.tv_sec < 0) return ETIMEDOUT;
31 if (!cleanup) __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
32 pthread_cleanup_push(cleanup, arg);
34 r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT|priv, val, top);
35 if (r == ENOSYS) r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT, val, top);
36 if (r != EINTR && r != ETIMEDOUT && r != ECANCELED) r = 0;
38 pthread_cleanup_pop(0);
39 if (!cleanup) __pthread_setcancelstate(cs, 0);