| Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -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 <semaphore.h> | 
|  | 30 | #include <errno.h> | 
|  | 31 | #include <limits.h> | 
|  | 32 | #include <sys/time.h> | 
|  | 33 | #include <time.h> | 
|  | 34 |  | 
|  | 35 | #include "private/bionic_atomic_inline.h" | 
|  | 36 | #include "private/bionic_constants.h" | 
|  | 37 | #include "private/bionic_futex.h" | 
|  | 38 | #include "private/bionic_time_conversions.h" | 
|  | 39 |  | 
|  | 40 | // In this implementation, a semaphore contains a | 
|  | 41 | // 31-bit signed value and a 1-bit 'shared' flag | 
|  | 42 | // (for process-sharing purpose). | 
|  | 43 | // | 
|  | 44 | // We use the value -1 to indicate contention on the | 
|  | 45 | // semaphore, 0 or more to indicate uncontended state, | 
|  | 46 | // any value lower than -2 is invalid at runtime. | 
|  | 47 | // | 
|  | 48 | // State diagram: | 
|  | 49 | // | 
|  | 50 | // post(1)  ==> 2 | 
|  | 51 | // post(0)  ==> 1 | 
|  | 52 | // post(-1) ==> 1, then wake all waiters | 
|  | 53 | // | 
|  | 54 | // wait(2)  ==> 1 | 
|  | 55 | // wait(1)  ==> 0 | 
|  | 56 | // wait(0)  ==> -1 then wait for a wake up + loop | 
|  | 57 | // wait(-1) ==> -1 then wait for a wake up + loop | 
|  | 58 |  | 
|  | 59 | // Use the upper 31-bits for the counter, and the lower one | 
|  | 60 | // for the shared flag. | 
|  | 61 | #define SEMCOUNT_SHARED_MASK      0x00000001 | 
|  | 62 | #define SEMCOUNT_VALUE_MASK       0xfffffffe | 
|  | 63 | #define SEMCOUNT_VALUE_SHIFT      1 | 
|  | 64 |  | 
|  | 65 | // Convert a value into the corresponding sem->count bit pattern. | 
|  | 66 | #define SEMCOUNT_FROM_VALUE(val)    (((val) << SEMCOUNT_VALUE_SHIFT) & SEMCOUNT_VALUE_MASK) | 
|  | 67 |  | 
|  | 68 | // Convert a sem->count bit pattern into the corresponding signed value. | 
|  | 69 | static inline int SEMCOUNT_TO_VALUE(uint32_t sval) { | 
|  | 70 | return (static_cast<int>(sval) >> SEMCOUNT_VALUE_SHIFT); | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | // The value +1 as a sem->count bit-pattern. | 
|  | 74 | #define SEMCOUNT_ONE              SEMCOUNT_FROM_VALUE(1) | 
|  | 75 |  | 
|  | 76 | // The value -1 as a sem->count bit-pattern. | 
|  | 77 | #define SEMCOUNT_MINUS_ONE        SEMCOUNT_FROM_VALUE(-1) | 
|  | 78 |  | 
|  | 79 | #define SEMCOUNT_DECREMENT(sval)    (((sval) - (1U << SEMCOUNT_VALUE_SHIFT)) & SEMCOUNT_VALUE_MASK) | 
|  | 80 | #define SEMCOUNT_INCREMENT(sval)    (((sval) + (1U << SEMCOUNT_VALUE_SHIFT)) & SEMCOUNT_VALUE_MASK) | 
|  | 81 |  | 
|  | 82 | // Return the shared bitflag from a semaphore. | 
|  | 83 | static inline uint32_t SEM_GET_SHARED(sem_t* sem) { | 
|  | 84 | return (sem->count & SEMCOUNT_SHARED_MASK); | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 |  | 
|  | 88 | int sem_init(sem_t* sem, int pshared, unsigned int value) { | 
| Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 89 | // Ensure that 'value' can be stored in the semaphore. | 
|  | 90 | if (value > SEM_VALUE_MAX) { | 
|  | 91 | errno = EINVAL; | 
|  | 92 | return -1; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | sem->count = SEMCOUNT_FROM_VALUE(value); | 
|  | 96 | if (pshared != 0) { | 
|  | 97 | sem->count |= SEMCOUNT_SHARED_MASK; | 
|  | 98 | } | 
|  | 99 | return 0; | 
|  | 100 | } | 
|  | 101 |  | 
| Elliott Hughes | 75129ae | 2014-10-08 15:11:44 -0700 | [diff] [blame] | 102 | int sem_destroy(sem_t*) { | 
| Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 103 | return 0; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | sem_t* sem_open(const char*, int, ...) { | 
|  | 107 | errno = ENOSYS; | 
|  | 108 | return SEM_FAILED; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | int sem_close(sem_t*) { | 
|  | 112 | errno = ENOSYS; | 
|  | 113 | return -1; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | int sem_unlink(const char*) { | 
|  | 117 | errno = ENOSYS; | 
|  | 118 | return -1; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | // Decrement a semaphore's value atomically, | 
|  | 122 | // and return the old one. As a special case, | 
|  | 123 | // this returns immediately if the value is | 
|  | 124 | // negative (i.e. -1) | 
|  | 125 | static int __sem_dec(volatile uint32_t* sem) { | 
|  | 126 | volatile int32_t* ptr = reinterpret_cast<volatile int32_t*>(sem); | 
|  | 127 | uint32_t shared = (*sem & SEMCOUNT_SHARED_MASK); | 
|  | 128 | uint32_t old_value, new_value; | 
|  | 129 | int ret; | 
|  | 130 |  | 
|  | 131 | do { | 
|  | 132 | old_value = (*sem & SEMCOUNT_VALUE_MASK); | 
|  | 133 | ret = SEMCOUNT_TO_VALUE(old_value); | 
|  | 134 | if (ret < 0) { | 
|  | 135 | break; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | new_value = SEMCOUNT_DECREMENT(old_value); | 
|  | 139 | } while (__bionic_cmpxchg((old_value|shared), (new_value|shared), ptr) != 0); | 
|  | 140 |  | 
|  | 141 | return ret; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | // Same as __sem_dec, but will not touch anything if the | 
|  | 145 | // value is already negative *or* 0. Returns the old value. | 
|  | 146 | static int __sem_trydec(volatile uint32_t* sem) { | 
|  | 147 | volatile int32_t* ptr = reinterpret_cast<volatile int32_t*>(sem); | 
|  | 148 | uint32_t shared = (*sem & SEMCOUNT_SHARED_MASK); | 
|  | 149 | uint32_t old_value, new_value; | 
|  | 150 | int          ret; | 
|  | 151 |  | 
|  | 152 | do { | 
|  | 153 | old_value = (*sem & SEMCOUNT_VALUE_MASK); | 
|  | 154 | ret = SEMCOUNT_TO_VALUE(old_value); | 
|  | 155 | if (ret <= 0) { | 
|  | 156 | break; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | new_value = SEMCOUNT_DECREMENT(old_value); | 
|  | 160 | } while (__bionic_cmpxchg((old_value|shared), (new_value|shared), ptr) != 0); | 
|  | 161 |  | 
|  | 162 | return ret; | 
|  | 163 | } | 
|  | 164 |  | 
|  | 165 |  | 
|  | 166 | // "Increment" the value of a semaphore atomically and | 
|  | 167 | // return its old value. Note that this implements | 
|  | 168 | // the special case of "incrementing" any negative | 
|  | 169 | // value to +1 directly. | 
|  | 170 | // | 
|  | 171 | // NOTE: The value will _not_ wrap above SEM_VALUE_MAX | 
|  | 172 | static int __sem_inc(volatile uint32_t* sem) { | 
|  | 173 | volatile int32_t* ptr = reinterpret_cast<volatile int32_t*>(sem); | 
|  | 174 | uint32_t shared = (*sem & SEMCOUNT_SHARED_MASK); | 
|  | 175 | uint32_t old_value, new_value; | 
|  | 176 | int ret; | 
|  | 177 |  | 
|  | 178 | do { | 
|  | 179 | old_value = (*sem & SEMCOUNT_VALUE_MASK); | 
|  | 180 | ret = SEMCOUNT_TO_VALUE(old_value); | 
|  | 181 |  | 
|  | 182 | // Can't go higher than SEM_VALUE_MAX. | 
|  | 183 | if (ret == SEM_VALUE_MAX) { | 
|  | 184 | break; | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | // If the counter is negative, go directly to +1, otherwise just increment. | 
|  | 188 | if (ret < 0) { | 
|  | 189 | new_value = SEMCOUNT_ONE; | 
|  | 190 | } else { | 
|  | 191 | new_value = SEMCOUNT_INCREMENT(old_value); | 
|  | 192 | } | 
|  | 193 | } while (__bionic_cmpxchg((old_value|shared), (new_value|shared), ptr) != 0); | 
|  | 194 |  | 
|  | 195 | return ret; | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | int sem_wait(sem_t* sem) { | 
| Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 199 | uint32_t shared = SEM_GET_SHARED(sem); | 
|  | 200 |  | 
|  | 201 | while (true) { | 
|  | 202 | if (__sem_dec(&sem->count) > 0) { | 
|  | 203 | ANDROID_MEMBAR_FULL(); | 
|  | 204 | return 0; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | __futex_wait_ex(&sem->count, shared, shared|SEMCOUNT_MINUS_ONE, NULL); | 
|  | 208 | } | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | int sem_timedwait(sem_t* sem, const timespec* abs_timeout) { | 
| Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 212 | // POSIX says we need to try to decrement the semaphore | 
|  | 213 | // before checking the timeout value. Note that if the | 
|  | 214 | // value is currently 0, __sem_trydec() does nothing. | 
|  | 215 | if (__sem_trydec(&sem->count) > 0) { | 
|  | 216 | ANDROID_MEMBAR_FULL(); | 
|  | 217 | return 0; | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | // Check it as per POSIX. | 
|  | 221 | if (abs_timeout == NULL || abs_timeout->tv_sec < 0 || abs_timeout->tv_nsec < 0 || abs_timeout->tv_nsec >= NS_PER_S) { | 
|  | 222 | errno = EINVAL; | 
|  | 223 | return -1; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | uint32_t shared = SEM_GET_SHARED(sem); | 
|  | 227 |  | 
|  | 228 | while (true) { | 
|  | 229 | // POSIX mandates CLOCK_REALTIME here. | 
|  | 230 | timespec ts; | 
|  | 231 | if (!timespec_from_absolute_timespec(ts, *abs_timeout, CLOCK_REALTIME)) { | 
|  | 232 | errno = ETIMEDOUT; | 
|  | 233 | return -1; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | // Try to grab the semaphore. If the value was 0, this will also change it to -1. | 
|  | 237 | if (__sem_dec(&sem->count) > 0) { | 
|  | 238 | ANDROID_MEMBAR_FULL(); | 
|  | 239 | break; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | // Contention detected. Wait for a wakeup event. | 
|  | 243 | int ret = __futex_wait_ex(&sem->count, shared, shared|SEMCOUNT_MINUS_ONE, &ts); | 
|  | 244 |  | 
|  | 245 | // Return in case of timeout or interrupt. | 
|  | 246 | if (ret == -ETIMEDOUT || ret == -EINTR) { | 
|  | 247 | errno = -ret; | 
|  | 248 | return -1; | 
|  | 249 | } | 
|  | 250 | } | 
|  | 251 | return 0; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | int sem_post(sem_t* sem) { | 
| Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 255 | uint32_t shared = SEM_GET_SHARED(sem); | 
|  | 256 |  | 
|  | 257 | ANDROID_MEMBAR_FULL(); | 
|  | 258 | int old_value = __sem_inc(&sem->count); | 
|  | 259 | if (old_value < 0) { | 
|  | 260 | // Contention on the semaphore. Wake up all waiters. | 
|  | 261 | __futex_wake_ex(&sem->count, shared, INT_MAX); | 
|  | 262 | } else if (old_value == SEM_VALUE_MAX) { | 
|  | 263 | // Overflow detected. | 
|  | 264 | errno = EOVERFLOW; | 
|  | 265 | return -1; | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | return 0; | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | int sem_trywait(sem_t* sem) { | 
| Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 272 | if (__sem_trydec(&sem->count) > 0) { | 
|  | 273 | ANDROID_MEMBAR_FULL(); | 
|  | 274 | return 0; | 
|  | 275 | } else { | 
|  | 276 | errno = EAGAIN; | 
|  | 277 | return -1; | 
|  | 278 | } | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | int sem_getvalue(sem_t* sem, int* sval) { | 
| Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 282 | int val = SEMCOUNT_TO_VALUE(sem->count); | 
|  | 283 | if (val < 0) { | 
|  | 284 | val = 0; | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | *sval = val; | 
|  | 288 | return 0; | 
|  | 289 | } |