1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include "private/bionic_futex.h"
30
31 #include <stdatomic.h>
32 #include <time.h>
33
34 #include "private/bionic_time_conversions.h"
35
FutexWithTimeout(volatile void * ftx,int op,int value,bool use_realtime_clock,const timespec * abs_timeout,int bitset)36 static inline __always_inline int FutexWithTimeout(volatile void* ftx, int op, int value,
37 bool use_realtime_clock,
38 const timespec* abs_timeout, int bitset) {
39 // pthread's and semaphore's default behavior is to use CLOCK_REALTIME, however this behavior is
40 // essentially never intended, as that clock is prone to change discontinuously.
41 //
42 // What users really intend is to use CLOCK_MONOTONIC, however only pthread_cond_timedwait()
43 // provides this as an option and even there, a large amount of existing code does not opt into
44 // CLOCK_MONOTONIC.
45 //
46 // We have seen numerous bugs directly attributable to this difference. Therefore, we provide
47 // this general workaround to always use CLOCK_MONOTONIC for waiting, regardless of what the input
48 // timespec is.
49 timespec converted_timeout;
50 if (abs_timeout) {
51 if ((op & FUTEX_CMD_MASK) == FUTEX_LOCK_PI) {
52 if (!use_realtime_clock) {
53 realtime_time_from_monotonic_time(converted_timeout, *abs_timeout);
54 abs_timeout = &converted_timeout;
55 }
56 } else {
57 op &= ~FUTEX_CLOCK_REALTIME;
58 if (use_realtime_clock) {
59 monotonic_time_from_realtime_time(converted_timeout, *abs_timeout);
60 abs_timeout = &converted_timeout;
61 }
62 }
63 if (abs_timeout->tv_sec < 0) {
64 return -ETIMEDOUT;
65 }
66 }
67
68 return __futex(ftx, op, value, abs_timeout, bitset);
69 }
70
__futex_wait_ex(volatile void * ftx,bool shared,int value,bool use_realtime_clock,const timespec * abs_timeout)71 int __futex_wait_ex(volatile void* ftx, bool shared, int value, bool use_realtime_clock,
72 const timespec* abs_timeout) {
73 return FutexWithTimeout(ftx, (shared ? FUTEX_WAIT_BITSET : FUTEX_WAIT_BITSET_PRIVATE), value,
74 use_realtime_clock, abs_timeout, FUTEX_BITSET_MATCH_ANY);
75 }
76
__futex_pi_lock_ex(volatile void * ftx,bool shared,bool use_realtime_clock,const timespec * abs_timeout)77 int __futex_pi_lock_ex(volatile void* ftx, bool shared, bool use_realtime_clock,
78 const timespec* abs_timeout) {
79 // We really want FUTEX_LOCK_PI2 which is default CLOCK_MONOTONIC, but that isn't supported
80 // on linux before 5.14. FUTEX_LOCK_PI uses CLOCK_REALTIME. Here we verify support.
81
82 static atomic_int lock_op = 0;
83 int op = atomic_load_explicit(&lock_op, memory_order_relaxed);
84 if (op == 0) {
85 uint32_t tmp = 0;
86 if (__futex(&tmp, FUTEX_LOCK_PI2, 0, nullptr, 0) == 0) {
87 __futex(&tmp, FUTEX_UNLOCK_PI, 0, nullptr, 0);
88 op = FUTEX_LOCK_PI2;
89 } else {
90 op = FUTEX_LOCK_PI;
91 }
92 atomic_store_explicit(&lock_op, op, memory_order_relaxed);
93 }
94
95 if (!shared) op |= FUTEX_PRIVATE_FLAG;
96 return FutexWithTimeout(ftx, op, 0 /* value */, use_realtime_clock, abs_timeout, 0 /* bitset */);
97 }
98