Rich Felker | ec381af | 2011-08-02 21:11:36 -0400 | [diff] [blame] | 1 | #include <pthread.h> |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 2 | #include <time.h> |
| 3 | #include <errno.h> |
| 4 | #include "futex.h" |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 5 | #include "syscall.h" |
Rich Felker | 0fc317d | 2015-03-02 17:46:22 -0500 | [diff] [blame] | 6 | #include "pthread_impl.h" |
Rich Felker | feee989 | 2011-04-17 11:43:03 -0400 | [diff] [blame] | 7 | |
Rich Felker | 1492bdf | 2019-07-28 17:44:51 -0400 | [diff] [blame] | 8 | #define IS32BIT(x) !((x)+0x80000000ULL>>32) |
| 9 | #define CLAMP(x) (int)(IS32BIT(x) ? (x) : 0x7fffffffU+((0ULL+(x))>>63)) |
| 10 | |
| 11 | static int __futex4_cp(volatile void *addr, int op, int val, const struct timespec *to) |
| 12 | { |
| 13 | int r; |
| 14 | #ifdef SYS_futex_time64 |
| 15 | time_t s = to ? to->tv_sec : 0; |
| 16 | long ns = to ? to->tv_nsec : 0; |
| 17 | r = -ENOSYS; |
| 18 | if (SYS_futex == SYS_futex_time64 || !IS32BIT(s)) |
| 19 | r = __syscall_cp(SYS_futex_time64, addr, op, val, |
| 20 | to ? ((long long[]){s, ns}) : 0); |
| 21 | if (SYS_futex == SYS_futex_time64 || r!=-ENOSYS) return r; |
| 22 | to = to ? (void *)(long[]){CLAMP(s), ns} : 0; |
| 23 | #endif |
| 24 | r = __syscall_cp(SYS_futex, addr, op, val, to); |
| 25 | if (r != -ENOSYS) return r; |
| 26 | return __syscall_cp(SYS_futex, addr, op & ~FUTEX_PRIVATE, val, to); |
| 27 | } |
| 28 | |
Rich Felker | a63c010 | 2018-12-18 12:17:33 -0500 | [diff] [blame] | 29 | static volatile int dummy = 0; |
| 30 | weak_alias(dummy, __eintr_valid_flag); |
| 31 | |
Rich Felker | 0fc317d | 2015-03-02 17:46:22 -0500 | [diff] [blame] | 32 | int __timedwait_cp(volatile int *addr, int val, |
| 33 | clockid_t clk, const struct timespec *at, int priv) |
Rich Felker | ec381af | 2011-08-02 21:11:36 -0400 | [diff] [blame] | 34 | { |
Rich Felker | 0fc317d | 2015-03-02 17:46:22 -0500 | [diff] [blame] | 35 | int r; |
Rich Felker | ec381af | 2011-08-02 21:11:36 -0400 | [diff] [blame] | 36 | struct timespec to, *top=0; |
| 37 | |
Jens Gustedt | d906fa3 | 2017-06-24 10:18:05 +0200 | [diff] [blame] | 38 | if (priv) priv = FUTEX_PRIVATE; |
Rich Felker | bc09d58 | 2014-08-15 23:54:52 -0400 | [diff] [blame] | 39 | |
Rich Felker | b3c08a1 | 2011-08-07 11:14:32 -0400 | [diff] [blame] | 40 | if (at) { |
| 41 | if (at->tv_nsec >= 1000000000UL) return EINVAL; |
Jens Gustedt | df7d0df | 2014-09-01 00:46:23 +0200 | [diff] [blame] | 42 | if (__clock_gettime(clk, &to)) return EINVAL; |
Rich Felker | b3c08a1 | 2011-08-07 11:14:32 -0400 | [diff] [blame] | 43 | to.tv_sec = at->tv_sec - to.tv_sec; |
| 44 | if ((to.tv_nsec = at->tv_nsec - to.tv_nsec) < 0) { |
| 45 | to.tv_sec--; |
| 46 | to.tv_nsec += 1000000000; |
| 47 | } |
| 48 | if (to.tv_sec < 0) return ETIMEDOUT; |
| 49 | top = &to; |
Rich Felker | ec381af | 2011-08-02 21:11:36 -0400 | [diff] [blame] | 50 | } |
Rich Felker | b3c08a1 | 2011-08-07 11:14:32 -0400 | [diff] [blame] | 51 | |
Rich Felker | 1492bdf | 2019-07-28 17:44:51 -0400 | [diff] [blame] | 52 | r = -__futex4_cp(addr, FUTEX_WAIT|priv, val, top); |
Rich Felker | 76ca7a5 | 2015-02-27 23:25:45 -0500 | [diff] [blame] | 53 | if (r != EINTR && r != ETIMEDOUT && r != ECANCELED) r = 0; |
Rich Felker | a63c010 | 2018-12-18 12:17:33 -0500 | [diff] [blame] | 54 | /* Mitigate bug in old kernels wrongly reporting EINTR for non- |
| 55 | * interrupting (SA_RESTART) signal handlers. This is only practical |
| 56 | * when NO interrupting signal handlers have been installed, and |
| 57 | * works by sigaction tracking whether that's the case. */ |
| 58 | if (r == EINTR && !__eintr_valid_flag) r = 0; |
Rich Felker | b3c08a1 | 2011-08-07 11:14:32 -0400 | [diff] [blame] | 59 | |
Rich Felker | 0fc317d | 2015-03-02 17:46:22 -0500 | [diff] [blame] | 60 | return r; |
| 61 | } |
Rich Felker | b3c08a1 | 2011-08-07 11:14:32 -0400 | [diff] [blame] | 62 | |
Rich Felker | 0fc317d | 2015-03-02 17:46:22 -0500 | [diff] [blame] | 63 | int __timedwait(volatile int *addr, int val, |
| 64 | clockid_t clk, const struct timespec *at, int priv) |
| 65 | { |
| 66 | int cs, r; |
| 67 | __pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); |
| 68 | r = __timedwait_cp(addr, val, clk, at, priv); |
| 69 | __pthread_setcancelstate(cs, 0); |
Rich Felker | ec381af | 2011-08-02 21:11:36 -0400 | [diff] [blame] | 70 | return r; |
Rich Felker | 0b44a03 | 2011-02-12 00:22:29 -0500 | [diff] [blame] | 71 | } |