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