blob: 19816470f876abf57a28153b568c2d38c8bae3dc [file] [log] [blame]
Elliott Hughes04303f52014-09-18 16:11:59 -07001/*
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
Yabin Cuia3d97da2015-01-30 12:30:08 -080029// Memory order requirements for POSIX semaphores appear unclear and are
30// currently interpreted inconsistently.
31// We conservatively prefer sequentially consistent operations for now.
32// CAUTION: This is more conservative than some other major implementations,
33// and may change if and when the issue is resolved.
34
Elliott Hughes04303f52014-09-18 16:11:59 -070035#include <semaphore.h>
36#include <errno.h>
37#include <limits.h>
Yabin Cuia3d97da2015-01-30 12:30:08 -080038#include <stdatomic.h>
Elliott Hughes04303f52014-09-18 16:11:59 -070039#include <sys/time.h>
40#include <time.h>
41
Elliott Hughes04303f52014-09-18 16:11:59 -070042#include "private/bionic_constants.h"
43#include "private/bionic_futex.h"
Yabin Cuica482742016-01-25 17:38:44 -080044#include "private/bionic_sdk_version.h"
Elliott Hughes04303f52014-09-18 16:11:59 -070045#include "private/bionic_time_conversions.h"
46
47// In this implementation, a semaphore contains a
48// 31-bit signed value and a 1-bit 'shared' flag
49// (for process-sharing purpose).
50//
51// We use the value -1 to indicate contention on the
52// semaphore, 0 or more to indicate uncontended state,
53// any value lower than -2 is invalid at runtime.
54//
55// State diagram:
56//
57// post(1) ==> 2
58// post(0) ==> 1
59// post(-1) ==> 1, then wake all waiters
60//
61// wait(2) ==> 1
62// wait(1) ==> 0
63// wait(0) ==> -1 then wait for a wake up + loop
64// wait(-1) ==> -1 then wait for a wake up + loop
65
66// Use the upper 31-bits for the counter, and the lower one
67// for the shared flag.
68#define SEMCOUNT_SHARED_MASK 0x00000001
69#define SEMCOUNT_VALUE_MASK 0xfffffffe
70#define SEMCOUNT_VALUE_SHIFT 1
71
72// Convert a value into the corresponding sem->count bit pattern.
73#define SEMCOUNT_FROM_VALUE(val) (((val) << SEMCOUNT_VALUE_SHIFT) & SEMCOUNT_VALUE_MASK)
74
75// Convert a sem->count bit pattern into the corresponding signed value.
Yabin Cuia3d97da2015-01-30 12:30:08 -080076static inline int SEMCOUNT_TO_VALUE(unsigned int sval) {
Elliott Hughes04303f52014-09-18 16:11:59 -070077 return (static_cast<int>(sval) >> SEMCOUNT_VALUE_SHIFT);
78}
79
80// The value +1 as a sem->count bit-pattern.
81#define SEMCOUNT_ONE SEMCOUNT_FROM_VALUE(1)
82
83// The value -1 as a sem->count bit-pattern.
Stephen Hines23360cc2015-09-29 09:28:41 -070084#define SEMCOUNT_MINUS_ONE SEMCOUNT_FROM_VALUE(~0U)
Elliott Hughes04303f52014-09-18 16:11:59 -070085
86#define SEMCOUNT_DECREMENT(sval) (((sval) - (1U << SEMCOUNT_VALUE_SHIFT)) & SEMCOUNT_VALUE_MASK)
87#define SEMCOUNT_INCREMENT(sval) (((sval) + (1U << SEMCOUNT_VALUE_SHIFT)) & SEMCOUNT_VALUE_MASK)
88
Yabin Cuia3d97da2015-01-30 12:30:08 -080089static inline atomic_uint* SEM_TO_ATOMIC_POINTER(sem_t* sem) {
90 static_assert(sizeof(atomic_uint) == sizeof(sem->count),
91 "sem->count should actually be atomic_uint in implementation.");
92
93 // We prefer casting to atomic_uint instead of declaring sem->count to be atomic_uint directly.
94 // Because using the second method pollutes semaphore.h.
95 return reinterpret_cast<atomic_uint*>(&sem->count);
Elliott Hughes04303f52014-09-18 16:11:59 -070096}
97
Yabin Cuia3d97da2015-01-30 12:30:08 -080098// Return the shared bitflag from a semaphore counter.
99static inline unsigned int SEM_GET_SHARED(atomic_uint* sem_count_ptr) {
100 // memory_order_relaxed is used as SHARED flag will not be changed after init.
101 return (atomic_load_explicit(sem_count_ptr, memory_order_relaxed) & SEMCOUNT_SHARED_MASK);
102}
Elliott Hughes04303f52014-09-18 16:11:59 -0700103
104int sem_init(sem_t* sem, int pshared, unsigned int value) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700105 // Ensure that 'value' can be stored in the semaphore.
106 if (value > SEM_VALUE_MAX) {
107 errno = EINVAL;
108 return -1;
109 }
110
Yabin Cuia3d97da2015-01-30 12:30:08 -0800111 unsigned int count = SEMCOUNT_FROM_VALUE(value);
Elliott Hughes04303f52014-09-18 16:11:59 -0700112 if (pshared != 0) {
Yabin Cuia3d97da2015-01-30 12:30:08 -0800113 count |= SEMCOUNT_SHARED_MASK;
Elliott Hughes04303f52014-09-18 16:11:59 -0700114 }
Yabin Cuia3d97da2015-01-30 12:30:08 -0800115
116 atomic_uint* sem_count_ptr = SEM_TO_ATOMIC_POINTER(sem);
117 atomic_init(sem_count_ptr, count);
Elliott Hughes04303f52014-09-18 16:11:59 -0700118 return 0;
119}
120
Elliott Hughes75129ae2014-10-08 15:11:44 -0700121int sem_destroy(sem_t*) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700122 return 0;
123}
124
125sem_t* sem_open(const char*, int, ...) {
126 errno = ENOSYS;
127 return SEM_FAILED;
128}
129
130int sem_close(sem_t*) {
131 errno = ENOSYS;
132 return -1;
133}
134
135int sem_unlink(const char*) {
136 errno = ENOSYS;
137 return -1;
138}
139
140// Decrement a semaphore's value atomically,
141// and return the old one. As a special case,
142// this returns immediately if the value is
143// negative (i.e. -1)
Yabin Cuia3d97da2015-01-30 12:30:08 -0800144static int __sem_dec(atomic_uint* sem_count_ptr) {
145 unsigned int old_value = atomic_load_explicit(sem_count_ptr, memory_order_relaxed);
146 unsigned int shared = old_value & SEMCOUNT_SHARED_MASK;
Elliott Hughes04303f52014-09-18 16:11:59 -0700147
Yabin Cuia3d97da2015-01-30 12:30:08 -0800148 // Use memory_order_seq_cst in atomic_compare_exchange operation to ensure all
149 // memory access made by other threads can be seen in current thread.
150 // An acquire fence may be sufficient, but it is still in discussion whether
151 // POSIX semaphores should provide sequential consistency.
Elliott Hughes04303f52014-09-18 16:11:59 -0700152 do {
Yabin Cuia3d97da2015-01-30 12:30:08 -0800153 if (SEMCOUNT_TO_VALUE(old_value) < 0) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700154 break;
155 }
Yabin Cuia3d97da2015-01-30 12:30:08 -0800156 } while (!atomic_compare_exchange_weak(sem_count_ptr, &old_value,
157 SEMCOUNT_DECREMENT(old_value) | shared));
Elliott Hughes04303f52014-09-18 16:11:59 -0700158
Yabin Cuia3d97da2015-01-30 12:30:08 -0800159 return SEMCOUNT_TO_VALUE(old_value);
Elliott Hughes04303f52014-09-18 16:11:59 -0700160}
161
162// Same as __sem_dec, but will not touch anything if the
163// value is already negative *or* 0. Returns the old value.
Yabin Cuia3d97da2015-01-30 12:30:08 -0800164static int __sem_trydec(atomic_uint* sem_count_ptr) {
165 unsigned int old_value = atomic_load_explicit(sem_count_ptr, memory_order_relaxed);
166 unsigned int shared = old_value & SEMCOUNT_SHARED_MASK;
Elliott Hughes04303f52014-09-18 16:11:59 -0700167
Yabin Cuia3d97da2015-01-30 12:30:08 -0800168 // Use memory_order_seq_cst in atomic_compare_exchange operation to ensure all
169 // memory access made by other threads can be seen in current thread.
170 // An acquire fence may be sufficient, but it is still in discussion whether
171 // POSIX semaphores should provide sequential consistency.
Elliott Hughes04303f52014-09-18 16:11:59 -0700172 do {
Yabin Cuia3d97da2015-01-30 12:30:08 -0800173 if (SEMCOUNT_TO_VALUE(old_value) <= 0) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700174 break;
175 }
Yabin Cuia3d97da2015-01-30 12:30:08 -0800176 } while (!atomic_compare_exchange_weak(sem_count_ptr, &old_value,
177 SEMCOUNT_DECREMENT(old_value) | shared));
Elliott Hughes04303f52014-09-18 16:11:59 -0700178
Yabin Cuia3d97da2015-01-30 12:30:08 -0800179 return SEMCOUNT_TO_VALUE(old_value);
Elliott Hughes04303f52014-09-18 16:11:59 -0700180}
181
Elliott Hughes04303f52014-09-18 16:11:59 -0700182// "Increment" the value of a semaphore atomically and
183// return its old value. Note that this implements
184// the special case of "incrementing" any negative
185// value to +1 directly.
186//
187// NOTE: The value will _not_ wrap above SEM_VALUE_MAX
Yabin Cuia3d97da2015-01-30 12:30:08 -0800188static int __sem_inc(atomic_uint* sem_count_ptr) {
189 unsigned int old_value = atomic_load_explicit(sem_count_ptr, memory_order_relaxed);
190 unsigned int shared = old_value & SEMCOUNT_SHARED_MASK;
191 unsigned int new_value;
Elliott Hughes04303f52014-09-18 16:11:59 -0700192
Yabin Cuia3d97da2015-01-30 12:30:08 -0800193 // Use memory_order_seq_cst in atomic_compare_exchange operation to ensure all
194 // memory access made before can be seen in other threads.
195 // A release fence may be sufficient, but it is still in discussion whether
196 // POSIX semaphores should provide sequential consistency.
Elliott Hughes04303f52014-09-18 16:11:59 -0700197 do {
Elliott Hughes04303f52014-09-18 16:11:59 -0700198 // Can't go higher than SEM_VALUE_MAX.
Yabin Cuia3d97da2015-01-30 12:30:08 -0800199 if (SEMCOUNT_TO_VALUE(old_value) == SEM_VALUE_MAX) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700200 break;
201 }
202
Yabin Cuia3d97da2015-01-30 12:30:08 -0800203 // If the counter is negative, go directly to one, otherwise just increment.
204 if (SEMCOUNT_TO_VALUE(old_value) < 0) {
205 new_value = SEMCOUNT_ONE | shared;
Elliott Hughes04303f52014-09-18 16:11:59 -0700206 } else {
Yabin Cuia3d97da2015-01-30 12:30:08 -0800207 new_value = SEMCOUNT_INCREMENT(old_value) | shared;
Elliott Hughes04303f52014-09-18 16:11:59 -0700208 }
Yabin Cuia3d97da2015-01-30 12:30:08 -0800209 } while (!atomic_compare_exchange_weak(sem_count_ptr, &old_value,
210 new_value));
Elliott Hughes04303f52014-09-18 16:11:59 -0700211
Yabin Cuia3d97da2015-01-30 12:30:08 -0800212 return SEMCOUNT_TO_VALUE(old_value);
Elliott Hughes04303f52014-09-18 16:11:59 -0700213}
214
215int sem_wait(sem_t* sem) {
Yabin Cuia3d97da2015-01-30 12:30:08 -0800216 atomic_uint* sem_count_ptr = SEM_TO_ATOMIC_POINTER(sem);
217 unsigned int shared = SEM_GET_SHARED(sem_count_ptr);
Elliott Hughes04303f52014-09-18 16:11:59 -0700218
219 while (true) {
Yabin Cuia3d97da2015-01-30 12:30:08 -0800220 if (__sem_dec(sem_count_ptr) > 0) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700221 return 0;
222 }
223
Yabin Cuica482742016-01-25 17:38:44 -0800224 int result = __futex_wait_ex(sem_count_ptr, shared, shared | SEMCOUNT_MINUS_ONE, false, nullptr);
225 if (bionic_get_application_target_sdk_version() > 23) {
226 if (result ==-EINTR) {
227 errno = EINTR;
228 return -1;
229 }
230 }
Elliott Hughes04303f52014-09-18 16:11:59 -0700231 }
232}
233
234int sem_timedwait(sem_t* sem, const timespec* abs_timeout) {
Yabin Cuia3d97da2015-01-30 12:30:08 -0800235 atomic_uint* sem_count_ptr = SEM_TO_ATOMIC_POINTER(sem);
236
Elliott Hughes04303f52014-09-18 16:11:59 -0700237 // POSIX says we need to try to decrement the semaphore
238 // before checking the timeout value. Note that if the
239 // value is currently 0, __sem_trydec() does nothing.
Yabin Cuia3d97da2015-01-30 12:30:08 -0800240 if (__sem_trydec(sem_count_ptr) > 0) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700241 return 0;
242 }
243
244 // Check it as per POSIX.
Elliott Hughesdd586f22015-12-16 15:15:58 -0800245 int result = check_timespec(abs_timeout, false);
Yabin Cuic9a659c2015-11-05 15:36:08 -0800246 if (result != 0) {
247 errno = result;
Elliott Hughes04303f52014-09-18 16:11:59 -0700248 return -1;
249 }
250
Yabin Cuia3d97da2015-01-30 12:30:08 -0800251 unsigned int shared = SEM_GET_SHARED(sem_count_ptr);
Elliott Hughes04303f52014-09-18 16:11:59 -0700252
253 while (true) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700254 // Try to grab the semaphore. If the value was 0, this will also change it to -1.
Yabin Cuia3d97da2015-01-30 12:30:08 -0800255 if (__sem_dec(sem_count_ptr) > 0) {
Yabin Cuic9a659c2015-11-05 15:36:08 -0800256 return 0;
Elliott Hughes04303f52014-09-18 16:11:59 -0700257 }
258
259 // Contention detected. Wait for a wakeup event.
Yabin Cuic9a659c2015-11-05 15:36:08 -0800260 int result = __futex_wait_ex(sem_count_ptr, shared, shared | SEMCOUNT_MINUS_ONE, true, abs_timeout);
Elliott Hughes04303f52014-09-18 16:11:59 -0700261
262 // Return in case of timeout or interrupt.
Yabin Cuic9a659c2015-11-05 15:36:08 -0800263 if (result == -ETIMEDOUT || result == -EINTR) {
264 errno = -result;
Elliott Hughes04303f52014-09-18 16:11:59 -0700265 return -1;
266 }
267 }
Elliott Hughes04303f52014-09-18 16:11:59 -0700268}
269
270int sem_post(sem_t* sem) {
Yabin Cuia3d97da2015-01-30 12:30:08 -0800271 atomic_uint* sem_count_ptr = SEM_TO_ATOMIC_POINTER(sem);
272 unsigned int shared = SEM_GET_SHARED(sem_count_ptr);
Elliott Hughes04303f52014-09-18 16:11:59 -0700273
Yabin Cuia3d97da2015-01-30 12:30:08 -0800274 int old_value = __sem_inc(sem_count_ptr);
Elliott Hughes04303f52014-09-18 16:11:59 -0700275 if (old_value < 0) {
276 // Contention on the semaphore. Wake up all waiters.
Yabin Cuia3d97da2015-01-30 12:30:08 -0800277 __futex_wake_ex(sem_count_ptr, shared, INT_MAX);
Elliott Hughes04303f52014-09-18 16:11:59 -0700278 } else if (old_value == SEM_VALUE_MAX) {
279 // Overflow detected.
280 errno = EOVERFLOW;
281 return -1;
282 }
283
284 return 0;
285}
286
287int sem_trywait(sem_t* sem) {
Yabin Cuia3d97da2015-01-30 12:30:08 -0800288 atomic_uint* sem_count_ptr = SEM_TO_ATOMIC_POINTER(sem);
289 if (__sem_trydec(sem_count_ptr) > 0) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700290 return 0;
291 } else {
292 errno = EAGAIN;
293 return -1;
294 }
295}
296
297int sem_getvalue(sem_t* sem, int* sval) {
Yabin Cuia3d97da2015-01-30 12:30:08 -0800298 atomic_uint* sem_count_ptr = SEM_TO_ATOMIC_POINTER(sem);
299
300 // Use memory_order_seq_cst in atomic_load operation.
301 // memory_order_relaxed may be fine here, but it is still in discussion
302 // whether POSIX semaphores should provide sequential consistency.
303 int val = SEMCOUNT_TO_VALUE(atomic_load(sem_count_ptr));
Elliott Hughes04303f52014-09-18 16:11:59 -0700304 if (val < 0) {
305 val = 0;
306 }
307
308 *sval = val;
309 return 0;
310}