1 #include <pthread.h>
2 #include <time.h>
3 #include <errno.h>
4 #include "futex.h"
5 #include "syscall.h"
6 #include "pthread_impl.h"
7
8 static volatile int dummy = 0;
9 weak_alias(dummy, __eintr_valid_flag);
10
__timedwait_cp(volatile int * addr,int val,clockid_t clk,const struct timespec * at,int priv)11 int __timedwait_cp(volatile int *addr, int val,
12 clockid_t clk, const struct timespec *at, int priv)
13 {
14 int r;
15 struct timespec to, *top=0;
16
17 if (priv) priv = FUTEX_PRIVATE;
18
19 if (at) {
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) {
24 to.tv_sec--;
25 to.tv_nsec += 1000000000;
26 }
27 if (to.tv_sec < 0) return ETIMEDOUT;
28 top = &to;
29 }
30
31 r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT|priv, val, top);
32 if (r == ENOSYS) r = -__syscall_cp(SYS_futex, addr, FUTEX_WAIT, val, top);
33 if (r != EINTR && r != ETIMEDOUT && r != ECANCELED) r = 0;
34 /* Mitigate bug in old kernels wrongly reporting EINTR for non-
35 * interrupting (SA_RESTART) signal handlers. This is only practical
36 * when NO interrupting signal handlers have been installed, and
37 * works by sigaction tracking whether that's the case. */
38 if (r == EINTR && !__eintr_valid_flag) r = 0;
39
40 return r;
41 }
42
__timedwait(volatile int * addr,int val,clockid_t clk,const struct timespec * at,int priv)43 int __timedwait(volatile int *addr, int val,
44 clockid_t clk, const struct timespec *at, int priv)
45 {
46 int cs, r;
47 __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
48 r = __timedwait_cp(addr, val, clk, at, priv);
49 __pthread_setcancelstate(cs, 0);
50 return r;
51 }
52