| 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> | 
|  | 33 | #include <sys/atomics.h> | 
|  | 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 |  | 
|  | 40 | #include "private/bionic_atomic_inline.h" | 
|  | 41 | #include "private/bionic_futex.h" | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 42 | #include "private/bionic_time_conversions.h" | 
|  | 43 | #include "private/bionic_tls.h" | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 44 |  | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 45 | // We use one bit in pthread_condattr_t (long) values as the 'shared' flag | 
|  | 46 | // and one bit for the clock type (CLOCK_REALTIME is ((clockid_t) 1), and | 
|  | 47 | // CLOCK_MONOTONIC is ((clockid_t) 0).). The rest of the bits are a counter. | 
|  | 48 | // | 
|  | 49 | // The 'value' field pthread_cond_t has the same layout. | 
|  | 50 |  | 
|  | 51 | #define COND_SHARED_MASK 0x0001 | 
|  | 52 | #define COND_CLOCK_MASK 0x0002 | 
|  | 53 | #define COND_COUNTER_STEP 0x0004 | 
|  | 54 | #define COND_FLAGS_MASK (COND_SHARED_MASK | COND_CLOCK_MASK) | 
|  | 55 | #define COND_COUNTER_MASK (~COND_FLAGS_MASK) | 
|  | 56 |  | 
|  | 57 | #define COND_IS_SHARED(c) (((c) & COND_SHARED_MASK) != 0) | 
|  | 58 | #define COND_GET_CLOCK(c) (((c) & COND_CLOCK_MASK) >> 1) | 
|  | 59 | #define COND_SET_CLOCK(attr, c) ((attr) | (c << 1)) | 
|  | 60 |  | 
|  | 61 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 62 | int pthread_condattr_init(pthread_condattr_t* attr) { | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 63 | *attr = 0; | 
|  | 64 | *attr |= PTHREAD_PROCESS_PRIVATE; | 
|  | 65 | *attr |= (CLOCK_REALTIME << 1); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 66 | return 0; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | int pthread_condattr_getpshared(const pthread_condattr_t* attr, int* pshared) { | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 70 | *pshared = static_cast<int>(COND_IS_SHARED(*attr)); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 71 | return 0; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | int pthread_condattr_setpshared(pthread_condattr_t* attr, int pshared) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 75 | if (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE) { | 
|  | 76 | return EINVAL; | 
|  | 77 | } | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 78 |  | 
|  | 79 | *attr |= pshared; | 
|  | 80 | return 0; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | int pthread_condattr_getclock(const pthread_condattr_t* attr, clockid_t* clock) { | 
|  | 84 | *clock = COND_GET_CLOCK(*attr); | 
|  | 85 | return 0; | 
|  | 86 | } | 
|  | 87 |  | 
|  | 88 | int pthread_condattr_setclock(pthread_condattr_t* attr, clockid_t clock) { | 
|  | 89 | if (clock != CLOCK_MONOTONIC && clock != CLOCK_REALTIME) { | 
|  | 90 | return EINVAL; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | *attr = COND_SET_CLOCK(*attr, clock); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 94 | return 0; | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | int pthread_condattr_destroy(pthread_condattr_t* attr) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 98 | *attr = 0xdeada11d; | 
|  | 99 | return 0; | 
|  | 100 | } | 
|  | 101 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 102 |  | 
|  | 103 | // XXX *technically* there is a race condition that could allow | 
|  | 104 | // XXX a signal to be missed.  If thread A is preempted in _wait() | 
|  | 105 | // XXX after unlocking the mutex and before waiting, and if other | 
|  | 106 | // XXX threads call signal or broadcast UINT_MAX/2 times (exactly), | 
|  | 107 | // XXX before thread A is scheduled again and calls futex_wait(), | 
|  | 108 | // XXX then the signal will be lost. | 
|  | 109 |  | 
|  | 110 | int pthread_cond_init(pthread_cond_t* cond, const pthread_condattr_t* attr) { | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 111 | if (attr != NULL) { | 
|  | 112 | cond->value = (*attr & COND_FLAGS_MASK); | 
|  | 113 | } else { | 
|  | 114 | cond->value = 0; | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
|  | 117 | return 0; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | int pthread_cond_destroy(pthread_cond_t* cond) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 121 | cond->value = 0xdeadc04d; | 
|  | 122 | return 0; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | // This function is used by pthread_cond_broadcast and | 
|  | 126 | // pthread_cond_signal to atomically decrement the counter | 
|  | 127 | // then wake up 'counter' threads. | 
|  | 128 | static int __pthread_cond_pulse(pthread_cond_t* cond, int counter) { | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 129 | int flags = (cond->value & COND_FLAGS_MASK); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 130 | while (true) { | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 131 | int old_value = cond->value; | 
|  | 132 | int new_value = ((old_value - COND_COUNTER_STEP) & COND_COUNTER_MASK) | flags; | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 133 | if (__bionic_cmpxchg(old_value, new_value, &cond->value) == 0) { | 
|  | 134 | break; | 
|  | 135 | } | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | // Ensure that all memory accesses previously made by this thread are | 
|  | 139 | // visible to the woken thread(s).  On the other side, the "wait" | 
|  | 140 | // code will issue any necessary barriers when locking the mutex. | 
|  | 141 | // | 
|  | 142 | // This may not strictly be necessary -- if the caller follows | 
|  | 143 | // recommended practice and holds the mutex before signaling the cond | 
|  | 144 | // var, the mutex ops will provide correct semantics.  If they don't | 
|  | 145 | // hold the mutex, they're subject to race conditions anyway. | 
|  | 146 | ANDROID_MEMBAR_FULL(); | 
|  | 147 |  | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 148 | __futex_wake_ex(&cond->value, COND_IS_SHARED(cond->value), counter); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 149 | return 0; | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | __LIBC_HIDDEN__ | 
|  | 153 | int __pthread_cond_timedwait_relative(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) { | 
|  | 154 | int old_value = cond->value; | 
|  | 155 |  | 
|  | 156 | pthread_mutex_unlock(mutex); | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 157 | int status = __futex_wait_ex(&cond->value, COND_IS_SHARED(cond->value), old_value, reltime); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 158 | pthread_mutex_lock(mutex); | 
|  | 159 |  | 
| Elliott Hughes | 9e79af3 | 2013-12-18 10:05:42 -0800 | [diff] [blame] | 160 | if (status == -ETIMEDOUT) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 161 | return ETIMEDOUT; | 
|  | 162 | } | 
|  | 163 | return 0; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | __LIBC_HIDDEN__ | 
|  | 167 | int __pthread_cond_timedwait(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime, clockid_t clock) { | 
|  | 168 | timespec ts; | 
|  | 169 | timespec* tsp; | 
|  | 170 |  | 
|  | 171 | if (abstime != NULL) { | 
| Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 172 | if (__timespec_from_absolute(&ts, abstime, clock) < 0) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 173 | return ETIMEDOUT; | 
|  | 174 | } | 
|  | 175 | tsp = &ts; | 
|  | 176 | } else { | 
|  | 177 | tsp = NULL; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | return __pthread_cond_timedwait_relative(cond, mutex, tsp); | 
|  | 181 | } | 
|  | 182 |  | 
|  | 183 | int pthread_cond_broadcast(pthread_cond_t* cond) { | 
|  | 184 | return __pthread_cond_pulse(cond, INT_MAX); | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | int pthread_cond_signal(pthread_cond_t* cond) { | 
|  | 188 | return __pthread_cond_pulse(cond, 1); | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex) { | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 192 | return __pthread_cond_timedwait(cond, mutex, NULL, COND_GET_CLOCK(cond->value)); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
|  | 195 | int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t * mutex, const timespec *abstime) { | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 196 | return __pthread_cond_timedwait(cond, mutex, abstime, COND_GET_CLOCK(cond->value)); | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 199 | #if !defined(__LP64__) | 
|  | 200 | // TODO: this exists only for backward binary compatibility on 32 bit platforms. | 
|  | 201 | extern "C" int pthread_cond_timedwait_monotonic(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 202 | return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC); | 
|  | 203 | } | 
|  | 204 |  | 
| Narayan Kamath | 51e6cb3 | 2014-03-03 15:38:51 +0000 | [diff] [blame] | 205 | extern "C" int pthread_cond_timedwait_monotonic_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* abstime) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 206 | return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC); | 
|  | 207 | } | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 208 |  | 
| Halton Huo | f0870c3 | 2014-02-21 18:05:29 +0800 | [diff] [blame] | 209 | extern "C" int pthread_cond_timedwait_relative_np(pthread_cond_t* cond, pthread_mutex_t* mutex, const timespec* reltime) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 210 | return __pthread_cond_timedwait_relative(cond, mutex, reltime); | 
|  | 211 | } | 
|  | 212 |  | 
| Halton Huo | f0870c3 | 2014-02-21 18:05:29 +0800 | [diff] [blame] | 213 | 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] | 214 | timespec ts; | 
|  | 215 | timespec_from_ms(ts, ms); | 
|  | 216 | return __pthread_cond_timedwait_relative(cond, mutex, &ts); | 
|  | 217 | } | 
| Halton Huo | f0870c3 | 2014-02-21 18:05:29 +0800 | [diff] [blame] | 218 | #endif // !defined(__LP64__) |