Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 <pthread.h> |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <limits.h> |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 33 | #include <stdatomic.h> |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 34 | #include <sys/mman.h> |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 35 | #include <time.h> |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 36 | #include <unistd.h> |
| 37 | |
| 38 | #include "pthread_internal.h" |
| 39 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 40 | #include "private/bionic_futex.h" |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 41 | #include "private/bionic_time_conversions.h" |
| 42 | #include "private/bionic_tls.h" |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 43 | |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 44 | // We use one bit in pthread_condattr_t (long) values as the 'shared' flag |
| 45 | // and one bit for the clock type (CLOCK_REALTIME is ((clockid_t) 1), and |
| 46 | // CLOCK_MONOTONIC is ((clockid_t) 0).). The rest of the bits are a counter. |
| 47 | // |
| 48 | // The 'value' field pthread_cond_t has the same layout. |
| 49 | |
| 50 | #define COND_SHARED_MASK 0x0001 |
| 51 | #define COND_CLOCK_MASK 0x0002 |
| 52 | #define COND_COUNTER_STEP 0x0004 |
| 53 | #define COND_FLAGS_MASK (COND_SHARED_MASK | COND_CLOCK_MASK) |
| 54 | #define COND_COUNTER_MASK (~COND_FLAGS_MASK) |
| 55 | |
| 56 | #define COND_IS_SHARED(c) (((c) & COND_SHARED_MASK) != 0) |
| 57 | #define COND_GET_CLOCK(c) (((c) & COND_CLOCK_MASK) >> 1) |
| 58 | #define COND_SET_CLOCK(attr, c) ((attr) | (c << 1)) |
| 59 | |
| 60 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 61 | int pthread_condattr_init(pthread_condattr_t* attr) { |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 62 | *attr = 0; |
| 63 | *attr |= PTHREAD_PROCESS_PRIVATE; |
| 64 | *attr |= (CLOCK_REALTIME << 1); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int pthread_condattr_getpshared(const pthread_condattr_t* attr, int* pshared) { |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 69 | *pshared = static_cast<int>(COND_IS_SHARED(*attr)); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int pthread_condattr_setpshared(pthread_condattr_t* attr, int pshared) { |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 74 | if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE) { |
| 75 | return EINVAL; |
| 76 | } |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 77 | |
| 78 | *attr |= pshared; |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | int pthread_condattr_getclock(const pthread_condattr_t* attr, clockid_t* clock) { |
| 83 | *clock = COND_GET_CLOCK(*attr); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock) { |
| 88 | if (clock != CLOCK_MONOTONIC && clock != CLOCK_REALTIME) { |
| 89 | return EINVAL; |
| 90 | } |
| 91 | |
| 92 | *attr = COND_SET_CLOCK(*attr, clock); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int pthread_condattr_destroy(pthread_condattr_t* attr) { |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 97 | *attr = 0xdeada11d; |
| 98 | return 0; |
| 99 | } |
| 100 | |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 101 | static inline atomic_uint* COND_TO_ATOMIC_POINTER(pthread_cond_t* cond) { |
| 102 | static_assert(sizeof(atomic_uint) == sizeof(cond->value), |
| 103 | "cond->value should actually be atomic_uint in implementation."); |
| 104 | |
| 105 | // We prefer casting to atomic_uint instead of declaring cond->value to be atomic_uint directly. |
| 106 | // Because using the second method pollutes pthread.h, and causes an error when compiling libcxx. |
| 107 | return reinterpret_cast<atomic_uint*>(&cond->value); |
| 108 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 109 | |
| 110 | // XXX *technically* there is a race condition that could allow |
| 111 | // XXX a signal to be missed. If thread A is preempted in _wait() |
| 112 | // XXX after unlocking the mutex and before waiting, and if other |
| 113 | // XXX threads call signal or broadcast UINT_MAX/2 times (exactly), |
| 114 | // XXX before thread A is scheduled again and calls futex_wait(), |
| 115 | // XXX then the signal will be lost. |
| 116 | |
| 117 | int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr) { |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 118 | atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond); |
| 119 | unsigned int init_value = 0; |
| 120 | |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 121 | if (attr != NULL) { |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 122 | init_value = (*attr & COND_FLAGS_MASK); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 123 | } |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 124 | atomic_init(cond_value_ptr, init_value); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | int pthread_cond_destroy(pthread_cond_t* cond) { |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 130 | atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond); |
| 131 | atomic_store_explicit(cond_value_ptr, 0xdeadc04d, memory_order_relaxed); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | // This function is used by pthread_cond_broadcast and |
| 136 | // pthread_cond_signal to atomically decrement the counter |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 137 | // then wake up thread_count threads. |
| 138 | static int __pthread_cond_pulse(atomic_uint* cond_value_ptr, int thread_count) { |
| 139 | unsigned int old_value = atomic_load_explicit(cond_value_ptr, memory_order_relaxed); |
| 140 | bool shared = COND_IS_SHARED(old_value); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 141 | |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 142 | // We don't use a release/seq_cst fence here. Because pthread_cond_wait/signal can't be |
| 143 | // used as a method for memory synchronization by itself. It should always be used with |
| 144 | // pthread mutexes. Note that Spurious wakeups from pthread_cond_wait/timedwait may occur, |
| 145 | // so when using condition variables there is always a boolean predicate involving shared |
| 146 | // variables associated with each condition wait that is true if the thread should proceed. |
| 147 | // If the predicate is seen true before a condition wait, pthread_cond_wait/timedwait will |
| 148 | // not be called. That's why pthread_wait/signal pair can't be used as a method for memory |
| 149 | // synchronization. And it doesn't help even if we use any fence here. |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 150 | |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 151 | // The increase of value should leave flags alone, even if the value can overflows. |
| 152 | atomic_fetch_add_explicit(cond_value_ptr, COND_COUNTER_STEP, memory_order_relaxed); |
| 153 | |
| 154 | __futex_wake_ex(cond_value_ptr, shared, thread_count); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | __LIBC_HIDDEN__ |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 159 | int __pthread_cond_timedwait_relative(atomic_uint* cond_value_ptr, pthread_mutex_t* mutex, |
| 160 | const timespec* reltime) { |
| 161 | unsigned int old_value = atomic_load_explicit(cond_value_ptr, memory_order_relaxed); |
| 162 | bool shared = COND_IS_SHARED(old_value); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 163 | |
| 164 | pthread_mutex_unlock(mutex); |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 165 | int status = __futex_wait_ex(cond_value_ptr, shared, old_value, reltime); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 166 | pthread_mutex_lock(mutex); |
| 167 | |
Elliott Hughes | 9e79af3 | 2013-12-18 10:05:42 -0800 | [diff] [blame] | 168 | if (status == -ETIMEDOUT) { |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 169 | return ETIMEDOUT; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | __LIBC_HIDDEN__ |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 175 | int __pthread_cond_timedwait(atomic_uint* cond_value_ptr, pthread_mutex_t* mutex, |
| 176 | const timespec* abs_ts, clockid_t clock) { |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 177 | timespec ts; |
| 178 | timespec* tsp; |
| 179 | |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 180 | if (abs_ts != NULL) { |
| 181 | if (!timespec_from_absolute_timespec(ts, *abs_ts, clock)) { |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 182 | return ETIMEDOUT; |
| 183 | } |
| 184 | tsp = &ts; |
| 185 | } else { |
| 186 | tsp = NULL; |
| 187 | } |
| 188 | |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 189 | return __pthread_cond_timedwait_relative(cond_value_ptr, mutex, tsp); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int pthread_cond_broadcast(pthread_cond_t* cond) { |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 193 | atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond); |
| 194 | return __pthread_cond_pulse(cond_value_ptr, INT_MAX); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | int pthread_cond_signal(pthread_cond_t* cond) { |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 198 | atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond); |
| 199 | return __pthread_cond_pulse(cond_value_ptr, 1); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) { |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 203 | atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond); |
| 204 | return __pthread_cond_timedwait(cond_value_ptr, mutex, NULL, |
| 205 | COND_GET_CLOCK(atomic_load_explicit(cond_value_ptr, memory_order_relaxed))); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t * mutex, const timespec *abstime) { |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 209 | atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond); |
| 210 | return __pthread_cond_timedwait(cond_value_ptr, mutex, abstime, |
| 211 | COND_GET_CLOCK(atomic_load_explicit(cond_value_ptr, memory_order_relaxed))); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 214 | #if !defined(__LP64__) |
| 215 | // TODO: this exists only for backward binary compatibility on 32 bit platforms. |
| 216 | extern "C" int pthread_cond_timedwait_monotonic(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) { |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 217 | atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond); |
| 218 | return __pthread_cond_timedwait(cond_value_ptr, mutex, abstime, CLOCK_MONOTONIC); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 221 | extern "C" int pthread_cond_timedwait_monotonic_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) { |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 222 | atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond); |
| 223 | return __pthread_cond_timedwait(cond_value_ptr, mutex, abstime, CLOCK_MONOTONIC); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 224 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 225 | |
Halton Huo | f0870c3 | 2014-02-21 18:05:29 +0800 | [diff] [blame] | 226 | extern "C" int pthread_cond_timedwait_relative_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) { |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 227 | atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond); |
| 228 | return __pthread_cond_timedwait_relative(cond_value_ptr, mutex, reltime); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Halton Huo | f0870c3 | 2014-02-21 18:05:29 +0800 | [diff] [blame] | 231 | extern "C" int pthread_cond_timeout_np(pthread_cond_t* cond, pthread_mutex_t* mutex, unsigned ms) { |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 232 | timespec ts; |
| 233 | timespec_from_ms(ts, ms); |
Yabin Cui | e5f816c | 2015-01-29 12:13:33 -0800 | [diff] [blame^] | 234 | atomic_uint* cond_value_ptr = COND_TO_ATOMIC_POINTER(cond); |
| 235 | return __pthread_cond_timedwait_relative(cond_value_ptr, mutex, &ts); |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 236 | } |
Halton Huo | f0870c3 | 2014-02-21 18:05:29 +0800 | [diff] [blame] | 237 | #endif // !defined(__LP64__) |