blob: 23dc3b0b753488b00d52e2a4b4c7a9172874d51f [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 */
Pierre Peifferd0c884d2012-02-22 16:40:15 +010028
Elliott Hughes6f94de32013-02-12 06:06:22 +000029#include <pthread.h>
Elliott Hughes3e898472013-02-12 16:40:24 +000030
31#include <errno.h>
32#include <limits.h>
Yabin Cui86fc96f2015-01-29 21:50:48 -080033#include <stdatomic.h>
Yabin Cui17393b02015-03-21 15:08:25 -070034#include <string.h>
Yabin Cui86fc96f2015-01-29 21:50:48 -080035#include <sys/cdefs.h>
Elliott Hughes84114c82013-07-17 13:33:19 -070036#include <sys/mman.h>
Pierre Peifferd0c884d2012-02-22 16:40:15 +010037#include <unistd.h>
38
Pierre Peifferd0c884d2012-02-22 16:40:15 +010039#include "pthread_internal.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070040
Elliott Hughes04303f52014-09-18 16:11:59 -070041#include "private/bionic_constants.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070042#include "private/bionic_futex.h"
Yabin Cui86fc96f2015-01-29 21:50:48 -080043#include "private/bionic_systrace.h"
Elliott Hughes04303f52014-09-18 16:11:59 -070044#include "private/bionic_time_conversions.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070045#include "private/bionic_tls.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046
Yabin Cuie69c2452015-02-13 16:21:25 -080047/* a mutex attribute holds the following fields
48 *
49 * bits: name description
50 * 0-3 type type of mutex
51 * 4 shared process-shared flag
52 */
53#define MUTEXATTR_TYPE_MASK 0x000f
54#define MUTEXATTR_SHARED_MASK 0x0010
55
56int pthread_mutexattr_init(pthread_mutexattr_t *attr)
57{
58 *attr = PTHREAD_MUTEX_DEFAULT;
59 return 0;
60}
61
62int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
63{
64 *attr = -1;
65 return 0;
66}
67
68int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type_p)
69{
70 int type = (*attr & MUTEXATTR_TYPE_MASK);
71
72 if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK) {
73 return EINVAL;
74 }
75
76 *type_p = type;
77 return 0;
78}
79
80int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
81{
82 if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK ) {
83 return EINVAL;
84 }
85
86 *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
87 return 0;
88}
89
90/* process-shared mutexes are not supported at the moment */
91
92int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
93{
94 switch (pshared) {
95 case PTHREAD_PROCESS_PRIVATE:
96 *attr &= ~MUTEXATTR_SHARED_MASK;
97 return 0;
98
99 case PTHREAD_PROCESS_SHARED:
100 /* our current implementation of pthread actually supports shared
101 * mutexes but won't cleanup if a process dies with the mutex held.
102 * Nevertheless, it's better than nothing. Shared mutexes are used
103 * by surfaceflinger and audioflinger.
104 */
105 *attr |= MUTEXATTR_SHARED_MASK;
106 return 0;
107 }
108 return EINVAL;
109}
110
111int pthread_mutexattr_getpshared(const pthread_mutexattr_t* attr, int* pshared) {
112 *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE;
113 return 0;
114}
115
116/* a mutex contains a state value and a owner_tid.
117 * The value is implemented as a 16-bit integer holding the following fields:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118 *
119 * bits: name description
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120 * 15-14 type mutex type
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700121 * 13 shared process-shared flag
122 * 12-2 counter counter of recursive mutexes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123 * 1-0 state lock state (0, 1 or 2)
Yabin Cuie69c2452015-02-13 16:21:25 -0800124 *
125 * The owner_tid is used only in recursive and errorcheck mutex to hold the mutex owner thread tid.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126 */
127
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100128/* Convenience macro, creates a mask of 'bits' bits that starts from
129 * the 'shift'-th least significant bit in a 32-bit word.
130 *
131 * Examples: FIELD_MASK(0,4) -> 0xf
132 * FIELD_MASK(16,9) -> 0x1ff0000
133 */
134#define FIELD_MASK(shift,bits) (((1 << (bits))-1) << (shift))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100136/* This one is used to create a bit pattern from a given field value */
137#define FIELD_TO_BITS(val,shift,bits) (((val) & ((1 << (bits))-1)) << (shift))
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100138
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100139/* And this one does the opposite, i.e. extract a field's value from a bit pattern */
140#define FIELD_FROM_BITS(val,shift,bits) (((val) >> (shift)) & ((1 << (bits))-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141
Yabin Cuie69c2452015-02-13 16:21:25 -0800142
143/* Convenience macros.
144 *
145 * These are used to form or modify the bit pattern of a given mutex value
146 */
147
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100148/* Mutex state:
149 *
150 * 0 for unlocked
151 * 1 for locked, no waiters
152 * 2 for locked, maybe waiters
153 */
154#define MUTEX_STATE_SHIFT 0
155#define MUTEX_STATE_LEN 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800156
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100157#define MUTEX_STATE_MASK FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
158#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
159#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
160
Yabin Cui17393b02015-03-21 15:08:25 -0700161#define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match PTHREAD_MUTEX_INITIALIZER */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100162#define MUTEX_STATE_LOCKED_UNCONTENDED 1 /* must be 1 due to atomic dec in unlock operation */
163#define MUTEX_STATE_LOCKED_CONTENDED 2 /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */
164
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100165#define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED)
166#define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED)
167#define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED)
168
169/* return true iff the mutex if locked with no waiters */
170#define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED)
171
172/* return true iff the mutex if locked with maybe waiters */
173#define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED)
174
175/* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */
176#define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED))
177
178/* Mutex counter:
179 *
180 * We need to check for overflow before incrementing, and we also need to
181 * detect when the counter is 0
182 */
183#define MUTEX_COUNTER_SHIFT 2
184#define MUTEX_COUNTER_LEN 11
185#define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN)
186
187#define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK)
188#define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
189
190/* Used to increment the counter directly after overflow has been checked */
Yabin Cui86fc96f2015-01-29 21:50:48 -0800191#define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1, MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN)
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100192
193/* Mutex shared bit flag
194 *
195 * This flag is set to indicate that the mutex is shared among processes.
196 * This changes the futex opcode we use for futex wait/wake operations
197 * (non-shared operations are much faster).
198 */
199#define MUTEX_SHARED_SHIFT 13
200#define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1)
201
202/* Mutex type:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100203 * We support normal, recursive and errorcheck mutexes.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100204 */
205#define MUTEX_TYPE_SHIFT 14
206#define MUTEX_TYPE_LEN 2
207#define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN)
208
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100209#define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN)
210
Yabin Cui17393b02015-03-21 15:08:25 -0700211#define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_NORMAL)
212#define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_RECURSIVE)
213#define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_ERRORCHECK)
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100214
Yabin Cui17393b02015-03-21 15:08:25 -0700215struct pthread_mutex_internal_t {
Yabin Cuie69c2452015-02-13 16:21:25 -0800216 _Atomic(uint16_t) state;
Yabin Cui17393b02015-03-21 15:08:25 -0700217#if defined(__LP64__)
Yabin Cuie69c2452015-02-13 16:21:25 -0800218 uint16_t __pad;
219 atomic_int owner_tid;
220 char __reserved[32];
221#else
222 _Atomic(uint16_t) owner_tid;
Yabin Cui17393b02015-03-21 15:08:25 -0700223#endif
Yabin Cuie69c2452015-02-13 16:21:25 -0800224} __attribute__((aligned(4)));
Yabin Cui86fc96f2015-01-29 21:50:48 -0800225
Yabin Cui17393b02015-03-21 15:08:25 -0700226static_assert(sizeof(pthread_mutex_t) == sizeof(pthread_mutex_internal_t),
227 "pthread_mutex_t should actually be pthread_mutex_internal_t in implementation.");
228
229// For binary compatibility with old version of pthread_mutex_t, we can't use more strict alignment
230// than 4-byte alignment.
231static_assert(alignof(pthread_mutex_t) == 4,
232 "pthread_mutex_t should fulfill the alignment of pthread_mutex_internal_t.");
233
234static inline pthread_mutex_internal_t* __get_internal_mutex(pthread_mutex_t* mutex_interface) {
235 return reinterpret_cast<pthread_mutex_internal_t*>(mutex_interface);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800236}
237
Yabin Cui17393b02015-03-21 15:08:25 -0700238int pthread_mutex_init(pthread_mutex_t* mutex_interface, const pthread_mutexattr_t* attr) {
239 pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
240
241 memset(mutex, 0, sizeof(pthread_mutex_internal_t));
Yabin Cui86fc96f2015-01-29 21:50:48 -0800242
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700243 if (__predict_true(attr == NULL)) {
Yabin Cui17393b02015-03-21 15:08:25 -0700244 atomic_init(&mutex->state, MUTEX_TYPE_BITS_NORMAL);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700245 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700247
Yabin Cuie69c2452015-02-13 16:21:25 -0800248 uint16_t state = 0;
Elliott Hughesdff72032013-12-11 14:54:00 -0800249 if ((*attr & MUTEXATTR_SHARED_MASK) != 0) {
Yabin Cui17393b02015-03-21 15:08:25 -0700250 state |= MUTEX_SHARED_MASK;
Elliott Hughesdff72032013-12-11 14:54:00 -0800251 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700252
253 switch (*attr & MUTEXATTR_TYPE_MASK) {
254 case PTHREAD_MUTEX_NORMAL:
Yabin Cuie69c2452015-02-13 16:21:25 -0800255 state |= MUTEX_TYPE_BITS_NORMAL;
256 break;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700257 case PTHREAD_MUTEX_RECURSIVE:
Yabin Cuie69c2452015-02-13 16:21:25 -0800258 state |= MUTEX_TYPE_BITS_RECURSIVE;
259 break;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700260 case PTHREAD_MUTEX_ERRORCHECK:
Yabin Cuie69c2452015-02-13 16:21:25 -0800261 state |= MUTEX_TYPE_BITS_ERRORCHECK;
262 break;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700263 default:
264 return EINVAL;
265 }
266
Yabin Cui17393b02015-03-21 15:08:25 -0700267 atomic_init(&mutex->state, state);
Yabin Cuie69c2452015-02-13 16:21:25 -0800268 atomic_init(&mutex->owner_tid, 0);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700269 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800270}
271
Yabin Cui17393b02015-03-21 15:08:25 -0700272static inline __always_inline int __pthread_normal_mutex_trylock(pthread_mutex_internal_t* mutex,
Yabin Cuie69c2452015-02-13 16:21:25 -0800273 uint16_t shared) {
274 const uint16_t unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
275 const uint16_t locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800276
Yabin Cuie69c2452015-02-13 16:21:25 -0800277 uint16_t old_state = unlocked;
Yabin Cui17393b02015-03-21 15:08:25 -0700278 if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state,
279 locked_uncontended, memory_order_acquire, memory_order_relaxed))) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800280 return 0;
281 }
282 return EBUSY;
283}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800284
285/*
Yabin Cui86fc96f2015-01-29 21:50:48 -0800286 * Lock a mutex of type NORMAL.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800287 *
288 * As noted above, there are three states:
289 * 0 (unlocked, no contention)
290 * 1 (locked, no contention)
291 * 2 (locked, contention)
292 *
293 * Non-recursive mutexes don't use the thread-id or counter fields, and the
294 * "type" value is zero, so the only bits that will be set are the ones in
295 * the lock state field.
296 */
Yabin Cui17393b02015-03-21 15:08:25 -0700297static inline __always_inline int __pthread_normal_mutex_lock(pthread_mutex_internal_t* mutex,
Yabin Cuie69c2452015-02-13 16:21:25 -0800298 uint16_t shared,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800299 bool use_realtime_clock,
300 const timespec* abs_timeout_or_null) {
Yabin Cui17393b02015-03-21 15:08:25 -0700301 if (__predict_true(__pthread_normal_mutex_trylock(mutex, shared) == 0)) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800302 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303 }
Yabin Cuic9a659c2015-11-05 15:36:08 -0800304 int result = check_timespec(abs_timeout_or_null);
305 if (result != 0) {
306 return result;
307 }
Yabin Cui86fc96f2015-01-29 21:50:48 -0800308
309 ScopedTrace trace("Contending for pthread mutex");
310
Yabin Cuie69c2452015-02-13 16:21:25 -0800311 const uint16_t unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
312 const uint16_t locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800313
Yabin Cui86fc96f2015-01-29 21:50:48 -0800314 // We want to go to sleep until the mutex is available, which requires
315 // promoting it to locked_contended. We need to swap in the new state
Yabin Cui17393b02015-03-21 15:08:25 -0700316 // and then wait until somebody wakes us up.
Yabin Cui86fc96f2015-01-29 21:50:48 -0800317 // An atomic_exchange is used to compete with other threads for the lock.
318 // If it returns unlocked, we have acquired the lock, otherwise another
319 // thread still holds the lock and we should wait again.
320 // If lock is acquired, an acquire fence is needed to make all memory accesses
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800321 // made by other threads visible to the current CPU.
Yabin Cui17393b02015-03-21 15:08:25 -0700322 while (atomic_exchange_explicit(&mutex->state, locked_contended,
Yabin Cui86fc96f2015-01-29 21:50:48 -0800323 memory_order_acquire) != unlocked) {
Yabin Cuic9a659c2015-11-05 15:36:08 -0800324 if (__futex_wait_ex(&mutex->state, shared, locked_contended, use_realtime_clock,
325 abs_timeout_or_null) == -ETIMEDOUT) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800326 return ETIMEDOUT;
327 }
Yabin Cui86fc96f2015-01-29 21:50:48 -0800328 }
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800329 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800330}
331
332/*
Yabin Cuie69c2452015-02-13 16:21:25 -0800333 * Release a normal mutex. The caller is responsible for determining
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334 * that we are in fact the owner of this lock.
335 */
Yabin Cui17393b02015-03-21 15:08:25 -0700336static inline __always_inline void __pthread_normal_mutex_unlock(pthread_mutex_internal_t* mutex,
Yabin Cuie69c2452015-02-13 16:21:25 -0800337 uint16_t shared) {
338 const uint16_t unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
339 const uint16_t locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700340
Yabin Cui86fc96f2015-01-29 21:50:48 -0800341 // We use an atomic_exchange to release the lock. If locked_contended state
342 // is returned, some threads is waiting for the lock and we need to wake up
343 // one of them.
344 // A release fence is required to make previous stores visible to next
345 // lock owner threads.
Yabin Cui17393b02015-03-21 15:08:25 -0700346 if (atomic_exchange_explicit(&mutex->state, unlocked,
Yabin Cui86fc96f2015-01-29 21:50:48 -0800347 memory_order_release) == locked_contended) {
348 // Wake up one waiting thread. We don't know which thread will be
349 // woken or when it'll start executing -- futexes make no guarantees
350 // here. There may not even be a thread waiting.
351 //
352 // The newly-woken thread will replace the unlocked state we just set above
353 // with locked_contended state, which means that when it eventually releases
354 // the mutex it will also call FUTEX_WAKE. This results in one extra wake
355 // call whenever a lock is contended, but let us avoid forgetting anyone
356 // without requiring us to track the number of sleepers.
357 //
358 // It's possible for another thread to sneak in and grab the lock between
359 // the exchange above and the wake call below. If the new thread is "slow"
360 // and holds the lock for a while, we'll wake up a sleeper, which will swap
361 // in locked_uncontended state and then go back to sleep since the lock is
362 // still held. If the new thread is "fast", running to completion before
363 // we call wake, the thread we eventually wake will find an unlocked mutex
364 // and will execute. Either way we have correct behavior and nobody is
365 // orphaned on the wait queue.
Yabin Cui17393b02015-03-21 15:08:25 -0700366 __futex_wake_ex(&mutex->state, shared, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800367 }
368}
369
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800370/* This common inlined function is used to increment the counter of a recursive mutex.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100371 *
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800372 * If the counter overflows, it will return EAGAIN.
373 * Otherwise, it atomically increments the counter and returns 0.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100374 *
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100375 */
Yabin Cui17393b02015-03-21 15:08:25 -0700376static inline __always_inline int __recursive_increment(pthread_mutex_internal_t* mutex,
Yabin Cuie69c2452015-02-13 16:21:25 -0800377 uint16_t old_state) {
Yabin Cui86fc96f2015-01-29 21:50:48 -0800378 // Detect recursive lock overflow and return EAGAIN.
379 // This is safe because only the owner thread can modify the
380 // counter bits in the mutex value.
Yabin Cui17393b02015-03-21 15:08:25 -0700381 if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(old_state)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100382 return EAGAIN;
383 }
384
Yabin Cuie69c2452015-02-13 16:21:25 -0800385 // Other threads are able to change the lower bits (e.g. promoting it to "contended"),
386 // but the mutex counter will not overflow. So we use atomic_fetch_add operation here.
387 // The mutex is still locked by current thread, so we don't need a release fence.
Yabin Cui17393b02015-03-21 15:08:25 -0700388 atomic_fetch_add_explicit(&mutex->state, MUTEX_COUNTER_BITS_ONE, memory_order_relaxed);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800389 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800390}
391
Yabin Cuif7969852015-04-02 17:47:48 -0700392static inline __always_inline int __recursive_or_errorcheck_mutex_wait(
393 pthread_mutex_internal_t* mutex,
394 uint16_t shared,
395 uint16_t old_state,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800396 bool use_realtime_clock,
397 const timespec* abs_timeout) {
Yabin Cuif7969852015-04-02 17:47:48 -0700398// __futex_wait always waits on a 32-bit value. But state is 16-bit. For a normal mutex, the owner_tid
399// field in mutex is not used. On 64-bit devices, the __pad field in mutex is not used.
400// But when a recursive or errorcheck mutex is used on 32-bit devices, we need to add the
401// owner_tid value in the value argument for __futex_wait, otherwise we may always get EAGAIN error.
402
403#if defined(__LP64__)
Yabin Cuic9a659c2015-11-05 15:36:08 -0800404 return __futex_wait_ex(&mutex->state, shared, old_state, use_realtime_clock, abs_timeout);
Yabin Cuif7969852015-04-02 17:47:48 -0700405
406#else
407 // This implementation works only when the layout of pthread_mutex_internal_t matches below expectation.
408 // And it is based on the assumption that Android is always in little-endian devices.
409 static_assert(offsetof(pthread_mutex_internal_t, state) == 0, "");
410 static_assert(offsetof(pthread_mutex_internal_t, owner_tid) == 2, "");
411
412 uint32_t owner_tid = atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed);
Yabin Cuic9a659c2015-11-05 15:36:08 -0800413 return __futex_wait_ex(&mutex->state, shared, (owner_tid << 16) | old_state,
414 use_realtime_clock, abs_timeout);
Yabin Cuif7969852015-04-02 17:47:48 -0700415#endif
416}
417
Yabin Cui17393b02015-03-21 15:08:25 -0700418static int __pthread_mutex_lock_with_timeout(pthread_mutex_internal_t* mutex,
Yabin Cuic9a659c2015-11-05 15:36:08 -0800419 bool use_realtime_clock,
420 const timespec* abs_timeout_or_null) {
Yabin Cuie69c2452015-02-13 16:21:25 -0800421 uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
422 uint16_t mtype = (old_state & MUTEX_TYPE_MASK);
423 uint16_t shared = (old_state & MUTEX_SHARED_MASK);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800424
Yabin Cui86fc96f2015-01-29 21:50:48 -0800425 // Handle common case first.
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700426 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) {
Yabin Cuic9a659c2015-11-05 15:36:08 -0800427 return __pthread_normal_mutex_lock(mutex, shared, use_realtime_clock, abs_timeout_or_null);
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800428 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700429
Yabin Cui86fc96f2015-01-29 21:50:48 -0800430 // Do we already own this recursive or error-check mutex?
Yabin Cuie69c2452015-02-13 16:21:25 -0800431 pid_t tid = __get_thread()->tid;
432 if (tid == atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed)) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800433 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
434 return EDEADLK;
435 }
Yabin Cui17393b02015-03-21 15:08:25 -0700436 return __recursive_increment(mutex, old_state);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800437 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700438
Yabin Cuie69c2452015-02-13 16:21:25 -0800439 const uint16_t unlocked = mtype | shared | MUTEX_STATE_BITS_UNLOCKED;
440 const uint16_t locked_uncontended = mtype | shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
441 const uint16_t locked_contended = mtype | shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700442
Yabin Cui86fc96f2015-01-29 21:50:48 -0800443 // First, if the mutex is unlocked, try to quickly acquire it.
444 // In the optimistic case where this works, set the state to locked_uncontended.
Yabin Cui17393b02015-03-21 15:08:25 -0700445 if (old_state == unlocked) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800446 // If exchanged successfully, an acquire fence is required to make
447 // all memory accesses made by other threads visible to the current CPU.
Yabin Cui17393b02015-03-21 15:08:25 -0700448 if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state,
Yabin Cuie69c2452015-02-13 16:21:25 -0800449 locked_uncontended, memory_order_acquire, memory_order_relaxed))) {
450 atomic_store_explicit(&mutex->owner_tid, tid, memory_order_relaxed);
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100451 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700452 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700453 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100454
Brigid Smitha406ee62014-07-21 15:38:06 -0700455 ScopedTrace trace("Contending for pthread mutex");
456
Yabin Cui86fc96f2015-01-29 21:50:48 -0800457 while (true) {
Yabin Cui17393b02015-03-21 15:08:25 -0700458 if (old_state == unlocked) {
Yabin Cui86fc96f2015-01-29 21:50:48 -0800459 // NOTE: We put the state to locked_contended since we _know_ there
460 // is contention when we are in this loop. This ensures all waiters
461 // will be unlocked.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100462
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800463 // If exchanged successfully, an acquire fence is required to make
464 // all memory accesses made by other threads visible to the current CPU.
Yabin Cui17393b02015-03-21 15:08:25 -0700465 if (__predict_true(atomic_compare_exchange_weak_explicit(&mutex->state,
Yabin Cuie69c2452015-02-13 16:21:25 -0800466 &old_state, locked_contended,
Yabin Cui86fc96f2015-01-29 21:50:48 -0800467 memory_order_acquire,
468 memory_order_relaxed))) {
Yabin Cuie69c2452015-02-13 16:21:25 -0800469 atomic_store_explicit(&mutex->owner_tid, tid, memory_order_relaxed);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800470 return 0;
471 }
472 continue;
Yabin Cui17393b02015-03-21 15:08:25 -0700473 } else if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(old_state)) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800474 // We should set it to locked_contended beforing going to sleep. This can make
Yabin Cui86fc96f2015-01-29 21:50:48 -0800475 // sure waiters will be woken up eventually.
476
Yabin Cui17393b02015-03-21 15:08:25 -0700477 int new_state = MUTEX_STATE_BITS_FLIP_CONTENTION(old_state);
478 if (__predict_false(!atomic_compare_exchange_weak_explicit(&mutex->state,
479 &old_state, new_state,
Yabin Cui86fc96f2015-01-29 21:50:48 -0800480 memory_order_relaxed,
481 memory_order_relaxed))) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100482 continue;
483 }
Yabin Cui17393b02015-03-21 15:08:25 -0700484 old_state = new_state;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100485 }
486
Yabin Cuic9a659c2015-11-05 15:36:08 -0800487 int result = check_timespec(abs_timeout_or_null);
488 if (result != 0) {
489 return result;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800490 }
Yabin Cuic9a659c2015-11-05 15:36:08 -0800491 // We are in locked_contended state, sleep until someone wakes us up.
492 if (__recursive_or_errorcheck_mutex_wait(mutex, shared, old_state, use_realtime_clock,
493 abs_timeout_or_null) == -ETIMEDOUT) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800494 return ETIMEDOUT;
495 }
Yabin Cui17393b02015-03-21 15:08:25 -0700496 old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100497 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800498}
499
Yabin Cui17393b02015-03-21 15:08:25 -0700500int pthread_mutex_lock(pthread_mutex_t* mutex_interface) {
Christopher Ferris511cfd92015-06-09 18:46:15 -0700501#if !defined(__LP64__)
502 if (mutex_interface == NULL) {
503 return EINVAL;
504 }
505#endif
506
Yabin Cui17393b02015-03-21 15:08:25 -0700507 pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800508
Yabin Cuie69c2452015-02-13 16:21:25 -0800509 uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
510 uint16_t mtype = (old_state & MUTEX_TYPE_MASK);
511 uint16_t shared = (old_state & MUTEX_SHARED_MASK);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800512 // Avoid slowing down fast path of normal mutex lock operation.
513 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
Yabin Cui17393b02015-03-21 15:08:25 -0700514 if (__predict_true(__pthread_normal_mutex_trylock(mutex, shared) == 0)) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800515 return 0;
516 }
517 }
Yabin Cuic9a659c2015-11-05 15:36:08 -0800518 return __pthread_mutex_lock_with_timeout(mutex, false, nullptr);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800519}
520
Yabin Cui17393b02015-03-21 15:08:25 -0700521int pthread_mutex_unlock(pthread_mutex_t* mutex_interface) {
Christopher Ferris511cfd92015-06-09 18:46:15 -0700522#if !defined(__LP64__)
523 if (mutex_interface == NULL) {
524 return EINVAL;
525 }
526#endif
527
Yabin Cui17393b02015-03-21 15:08:25 -0700528 pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800529
Yabin Cuie69c2452015-02-13 16:21:25 -0800530 uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
531 uint16_t mtype = (old_state & MUTEX_TYPE_MASK);
532 uint16_t shared = (old_state & MUTEX_SHARED_MASK);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800533
Yabin Cui86fc96f2015-01-29 21:50:48 -0800534 // Handle common case first.
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700535 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
Yabin Cui17393b02015-03-21 15:08:25 -0700536 __pthread_normal_mutex_unlock(mutex, shared);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800537 return 0;
538 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700539
Yabin Cui86fc96f2015-01-29 21:50:48 -0800540 // Do we already own this recursive or error-check mutex?
Yabin Cuie69c2452015-02-13 16:21:25 -0800541 pid_t tid = __get_thread()->tid;
542 if ( tid != atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed) ) {
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700543 return EPERM;
Yabin Cuie69c2452015-02-13 16:21:25 -0800544 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700545
Yabin Cui86fc96f2015-01-29 21:50:48 -0800546 // If the counter is > 0, we can simply decrement it atomically.
547 // Since other threads can mutate the lower state bits (and only the
548 // lower state bits), use a compare_exchange loop to do it.
Yabin Cui17393b02015-03-21 15:08:25 -0700549 if (!MUTEX_COUNTER_BITS_IS_ZERO(old_state)) {
Yabin Cui86fc96f2015-01-29 21:50:48 -0800550 // We still own the mutex, so a release fence is not needed.
Yabin Cui17393b02015-03-21 15:08:25 -0700551 atomic_fetch_sub_explicit(&mutex->state, MUTEX_COUNTER_BITS_ONE, memory_order_relaxed);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800552 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700553 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100554
Yabin Cui86fc96f2015-01-29 21:50:48 -0800555 // The counter is 0, so we'are going to unlock the mutex by resetting its
556 // state to unlocked, we need to perform a atomic_exchange inorder to read
557 // the current state, which will be locked_contended if there may have waiters
558 // to awake.
559 // A release fence is required to make previous stores visible to next
560 // lock owner threads.
Yabin Cuie69c2452015-02-13 16:21:25 -0800561 atomic_store_explicit(&mutex->owner_tid, 0, memory_order_relaxed);
562 const uint16_t unlocked = mtype | shared | MUTEX_STATE_BITS_UNLOCKED;
Yabin Cui17393b02015-03-21 15:08:25 -0700563 old_state = atomic_exchange_explicit(&mutex->state, unlocked, memory_order_release);
564 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(old_state)) {
565 __futex_wake_ex(&mutex->state, shared, 1);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700566 }
Yabin Cui86fc96f2015-01-29 21:50:48 -0800567
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700568 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800569}
570
Yabin Cui17393b02015-03-21 15:08:25 -0700571int pthread_mutex_trylock(pthread_mutex_t* mutex_interface) {
572 pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800573
Yabin Cuie69c2452015-02-13 16:21:25 -0800574 uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
575 uint16_t mtype = (old_state & MUTEX_TYPE_MASK);
576 uint16_t shared = (old_state & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700577
Yabin Cuie69c2452015-02-13 16:21:25 -0800578 const uint16_t unlocked = mtype | shared | MUTEX_STATE_BITS_UNLOCKED;
579 const uint16_t locked_uncontended = mtype | shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800580
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700581 // Handle common case first.
582 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
Yabin Cui17393b02015-03-21 15:08:25 -0700583 return __pthread_normal_mutex_trylock(mutex, shared);
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800584 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700585
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700586 // Do we already own this recursive or error-check mutex?
587 pid_t tid = __get_thread()->tid;
Yabin Cuie69c2452015-02-13 16:21:25 -0800588 if (tid == atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed)) {
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700589 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
590 return EBUSY;
591 }
Yabin Cui17393b02015-03-21 15:08:25 -0700592 return __recursive_increment(mutex, old_state);
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700593 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700594
Yabin Cui86fc96f2015-01-29 21:50:48 -0800595 // Same as pthread_mutex_lock, except that we don't want to wait, and
596 // the only operation that can succeed is a single compare_exchange to acquire the
597 // lock if it is released / not owned by anyone. No need for a complex loop.
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800598 // If exchanged successfully, an acquire fence is required to make
599 // all memory accesses made by other threads visible to the current CPU.
Yabin Cui17393b02015-03-21 15:08:25 -0700600 old_state = unlocked;
Yabin Cuie69c2452015-02-13 16:21:25 -0800601 if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state,
602 locked_uncontended,
Yabin Cui86fc96f2015-01-29 21:50:48 -0800603 memory_order_acquire,
604 memory_order_relaxed))) {
Yabin Cuie69c2452015-02-13 16:21:25 -0800605 atomic_store_explicit(&mutex->owner_tid, tid, memory_order_relaxed);
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100606 return 0;
607 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100608 return EBUSY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800609}
610
Elliott Hughes0e714a52014-03-03 16:42:47 -0800611#if !defined(__LP64__)
Yabin Cui17393b02015-03-21 15:08:25 -0700612extern "C" int pthread_mutex_lock_timeout_np(pthread_mutex_t* mutex_interface, unsigned ms) {
Yabin Cuic9a659c2015-11-05 15:36:08 -0800613 timespec ts;
614 timespec_from_ms(ts, ms);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800615 timespec abs_timeout;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800616 absolute_timespec_from_timespec(abs_timeout, ts, CLOCK_MONOTONIC);
Yabin Cui17393b02015-03-21 15:08:25 -0700617 int error = __pthread_mutex_lock_with_timeout(__get_internal_mutex(mutex_interface),
Yabin Cuic9a659c2015-11-05 15:36:08 -0800618 false, &abs_timeout);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800619 if (error == ETIMEDOUT) {
620 error = EBUSY;
621 }
622 return error;
Elliott Hughes0e714a52014-03-03 16:42:47 -0800623}
624#endif
625
Yabin Cui17393b02015-03-21 15:08:25 -0700626int pthread_mutex_timedlock(pthread_mutex_t* mutex_interface, const timespec* abs_timeout) {
627 return __pthread_mutex_lock_with_timeout(__get_internal_mutex(mutex_interface),
Yabin Cuic9a659c2015-11-05 15:36:08 -0800628 true, abs_timeout);
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700629}
630
Yabin Cui17393b02015-03-21 15:08:25 -0700631int pthread_mutex_destroy(pthread_mutex_t* mutex_interface) {
Yabin Cui86fc96f2015-01-29 21:50:48 -0800632 // Use trylock to ensure that the mutex is valid and not already locked.
Yabin Cui17393b02015-03-21 15:08:25 -0700633 int error = pthread_mutex_trylock(mutex_interface);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800634 if (error != 0) {
635 return error;
636 }
Yabin Cui86fc96f2015-01-29 21:50:48 -0800637 return 0;
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700638}