blob: fda0b93a5b8ff2bb6da529b00acaaba45c3d3051 [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 Cui5a00ba72018-01-26 17:32:31 -080034#include <stdlib.h>
Yabin Cui17393b02015-03-21 15:08:25 -070035#include <string.h>
Yabin Cui86fc96f2015-01-29 21:50:48 -080036#include <sys/cdefs.h>
Elliott Hughes84114c82013-07-17 13:33:19 -070037#include <sys/mman.h>
Pierre Peifferd0c884d2012-02-22 16:40:15 +010038#include <unistd.h>
39
Pierre Peifferd0c884d2012-02-22 16:40:15 +010040#include "pthread_internal.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070041
Elliott Hughes04303f52014-09-18 16:11:59 -070042#include "private/bionic_constants.h"
Yabin Cui9651fdf2018-03-14 12:02:21 -070043#include "private/bionic_fortify.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070044#include "private/bionic_futex.h"
Yabin Cui9651fdf2018-03-14 12:02:21 -070045#include "private/bionic_sdk_version.h"
Yabin Cui86fc96f2015-01-29 21:50:48 -080046#include "private/bionic_systrace.h"
Elliott Hughes04303f52014-09-18 16:11:59 -070047#include "private/bionic_time_conversions.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070048#include "private/bionic_tls.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049
Yabin Cuie69c2452015-02-13 16:21:25 -080050/* a mutex attribute holds the following fields
51 *
52 * bits: name description
53 * 0-3 type type of mutex
54 * 4 shared process-shared flag
Yabin Cui6b9c85b2018-01-23 12:56:18 -080055 * 5 protocol whether it is a priority inherit mutex.
Yabin Cuie69c2452015-02-13 16:21:25 -080056 */
57#define MUTEXATTR_TYPE_MASK 0x000f
58#define MUTEXATTR_SHARED_MASK 0x0010
Yabin Cui6b9c85b2018-01-23 12:56:18 -080059#define MUTEXATTR_PROTOCOL_MASK 0x0020
60
61#define MUTEXATTR_PROTOCOL_SHIFT 5
Yabin Cuie69c2452015-02-13 16:21:25 -080062
63int pthread_mutexattr_init(pthread_mutexattr_t *attr)
64{
65 *attr = PTHREAD_MUTEX_DEFAULT;
66 return 0;
67}
68
69int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
70{
71 *attr = -1;
72 return 0;
73}
74
75int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type_p)
76{
77 int type = (*attr & MUTEXATTR_TYPE_MASK);
78
79 if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK) {
80 return EINVAL;
81 }
82
83 *type_p = type;
84 return 0;
85}
86
87int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
88{
89 if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK ) {
90 return EINVAL;
91 }
92
93 *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
94 return 0;
95}
96
97/* process-shared mutexes are not supported at the moment */
98
99int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
100{
101 switch (pshared) {
102 case PTHREAD_PROCESS_PRIVATE:
103 *attr &= ~MUTEXATTR_SHARED_MASK;
104 return 0;
105
106 case PTHREAD_PROCESS_SHARED:
107 /* our current implementation of pthread actually supports shared
108 * mutexes but won't cleanup if a process dies with the mutex held.
109 * Nevertheless, it's better than nothing. Shared mutexes are used
110 * by surfaceflinger and audioflinger.
111 */
112 *attr |= MUTEXATTR_SHARED_MASK;
113 return 0;
114 }
115 return EINVAL;
116}
117
118int pthread_mutexattr_getpshared(const pthread_mutexattr_t* attr, int* pshared) {
119 *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE;
120 return 0;
121}
122
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800123int pthread_mutexattr_setprotocol(pthread_mutexattr_t* attr, int protocol) {
124 if (protocol != PTHREAD_PRIO_NONE && protocol != PTHREAD_PRIO_INHERIT) {
125 return EINVAL;
126 }
127 *attr = (*attr & ~MUTEXATTR_PROTOCOL_MASK) | (protocol << MUTEXATTR_PROTOCOL_SHIFT);
128 return 0;
129}
130
131int pthread_mutexattr_getprotocol(const pthread_mutexattr_t* attr, int* protocol) {
132 *protocol = (*attr & MUTEXATTR_PROTOCOL_MASK) >> MUTEXATTR_PROTOCOL_SHIFT;
133 return 0;
134}
135
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800136// Priority Inheritance mutex implementation
137struct PIMutex {
138 // mutex type, can be 0 (normal), 1 (recursive), 2 (errorcheck), constant during lifetime
139 uint8_t type;
140 // process-shared flag, constant during lifetime
141 bool shared;
142 // <number of times a thread holding a recursive PI mutex> - 1
143 uint16_t counter;
144 // owner_tid is read/written by both userspace code and kernel code. It includes three fields:
145 // FUTEX_WAITERS, FUTEX_OWNER_DIED and FUTEX_TID_MASK.
146 atomic_int owner_tid;
147};
148
149static inline __always_inline int PIMutexTryLock(PIMutex& mutex) {
150 pid_t tid = __get_thread()->tid;
151 // Handle common case first.
152 int old_owner = 0;
153 if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex.owner_tid,
154 &old_owner, tid,
155 memory_order_acquire,
156 memory_order_relaxed))) {
157 return 0;
158 }
159 if (tid == (old_owner & FUTEX_TID_MASK)) {
160 // We already own this mutex.
161 if (mutex.type == PTHREAD_MUTEX_NORMAL) {
162 return EBUSY;
163 }
164 if (mutex.type == PTHREAD_MUTEX_ERRORCHECK) {
165 return EDEADLK;
166 }
167 if (mutex.counter == 0xffff) {
168 return EAGAIN;
169 }
170 mutex.counter++;
171 return 0;
172 }
173 return EBUSY;
174}
175
Yabin Cui9651fdf2018-03-14 12:02:21 -0700176// Inlining this function in pthread_mutex_lock() adds the cost of stack frame instructions on
Yabin Cui5a00ba72018-01-26 17:32:31 -0800177// ARM/ARM64, which increases at most 20 percent overhead. So make it noinline.
178static int __attribute__((noinline)) PIMutexTimedLock(PIMutex& mutex,
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800179 bool use_realtime_clock,
Yabin Cui5a00ba72018-01-26 17:32:31 -0800180 const timespec* abs_timeout) {
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800181 int ret = PIMutexTryLock(mutex);
182 if (__predict_true(ret == 0)) {
183 return 0;
184 }
185 if (ret == EBUSY) {
Yabin Cui5a00ba72018-01-26 17:32:31 -0800186 ScopedTrace trace("Contending for pthread mutex");
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800187 ret = -__futex_pi_lock_ex(&mutex.owner_tid, mutex.shared, use_realtime_clock, abs_timeout);
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800188 }
189 return ret;
190}
191
192static int PIMutexUnlock(PIMutex& mutex) {
193 pid_t tid = __get_thread()->tid;
194 int old_owner = tid;
195 // Handle common case first.
196 if (__predict_true(mutex.type == PTHREAD_MUTEX_NORMAL)) {
197 if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex.owner_tid,
198 &old_owner, 0,
199 memory_order_release,
200 memory_order_relaxed))) {
201 return 0;
202 }
203 }
204
205 if (tid != (old_owner & FUTEX_TID_MASK)) {
206 // The mutex can only be unlocked by the thread who owns it.
207 return EPERM;
208 }
209 if (mutex.type == PTHREAD_MUTEX_RECURSIVE) {
210 if (mutex.counter != 0u) {
211 --mutex.counter;
212 return 0;
213 }
214 }
215 if (old_owner == tid) {
216 // No thread is waiting.
217 if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex.owner_tid,
218 &old_owner, 0,
219 memory_order_release,
220 memory_order_relaxed))) {
221 return 0;
222 }
223 }
224 return -__futex_pi_unlock(&mutex.owner_tid, mutex.shared);
225}
226
227static int PIMutexDestroy(PIMutex& mutex) {
228 // The mutex should be in unlocked state (owner_tid == 0) when destroyed.
229 // Store 0xffffffff to make the mutex unusable.
230 int old_owner = 0;
231 if (atomic_compare_exchange_strong_explicit(&mutex.owner_tid, &old_owner, 0xffffffff,
232 memory_order_relaxed, memory_order_relaxed)) {
233 return 0;
234 }
235 return EBUSY;
236}
Yabin Cui5a00ba72018-01-26 17:32:31 -0800237
238#if !defined(__LP64__)
239
240namespace PIMutexAllocator {
241// pthread_mutex_t has only 4 bytes in 32-bit programs, which are not enough to hold PIMutex.
242// So we use malloc to allocate PIMutexes and use 16-bit of pthread_mutex_t as indexes to find
243// the allocated PIMutexes. This allows at most 65536 PI mutexes.
244// When calling operations like pthread_mutex_lock/unlock, the 16-bit index is mapped to the
245// corresponding PIMutex. To make the map operation fast, we use a lockless mapping method:
246// Once a PIMutex is allocated, all the data used to map index to the PIMutex isn't changed until
247// it is destroyed.
248// Below are the data structures:
249// // struct Node contains a PIMutex.
250// typedef Node NodeArray[256];
251// typedef NodeArray* NodeArrayP;
252// NodeArrayP nodes[256];
253//
254// A 16-bit index is mapped to Node as below:
255// (*nodes[index >> 8])[index & 0xff]
256//
257// Also use a free list to allow O(1) finding recycled PIMutexes.
258
259union Node {
260 PIMutex mutex;
261 int next_free_id; // If not -1, refer to the next node in the free PIMutex list.
262};
263typedef Node NodeArray[256];
264typedef NodeArray* NodeArrayP;
265
266// lock_ protects below items.
267static Lock lock;
268static NodeArrayP* nodes;
269static int next_to_alloc_id;
270static int first_free_id = -1; // If not -1, refer to the first node in the free PIMutex list.
271
272static inline __always_inline Node& IdToNode(int id) {
273 return (*nodes[id >> 8])[id & 0xff];
274}
275
276static inline __always_inline PIMutex& IdToPIMutex(int id) {
277 return IdToNode(id).mutex;
278}
279
280static int AllocIdLocked() {
281 if (first_free_id != -1) {
282 int result = first_free_id;
283 first_free_id = IdToNode(result).next_free_id;
284 return result;
285 }
286 if (next_to_alloc_id >= 0x10000) {
287 return -1;
288 }
289 int array_pos = next_to_alloc_id >> 8;
290 int node_pos = next_to_alloc_id & 0xff;
291 if (node_pos == 0) {
292 if (array_pos == 0) {
293 nodes = static_cast<NodeArray**>(calloc(256, sizeof(NodeArray*)));
294 if (nodes == nullptr) {
295 return -1;
296 }
297 }
298 nodes[array_pos] = static_cast<NodeArray*>(malloc(sizeof(NodeArray)));
299 if (nodes[array_pos] == nullptr) {
300 return -1;
301 }
302 }
303 return next_to_alloc_id++;
304}
305
306// If succeed, return an id referring to a PIMutex, otherwise return -1.
307// A valid id is in range [0, 0xffff].
308static int AllocId() {
309 lock.lock();
310 int result = AllocIdLocked();
311 lock.unlock();
312 if (result != -1) {
313 memset(&IdToPIMutex(result), 0, sizeof(PIMutex));
314 }
315 return result;
316}
317
318static void FreeId(int id) {
319 lock.lock();
320 IdToNode(id).next_free_id = first_free_id;
321 first_free_id = id;
322 lock.unlock();
323}
324
325} // namespace PIMutexAllocator
326
327#endif // !defined(__LP64__)
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800328
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800329
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100330/* Convenience macro, creates a mask of 'bits' bits that starts from
331 * the 'shift'-th least significant bit in a 32-bit word.
332 *
333 * Examples: FIELD_MASK(0,4) -> 0xf
334 * FIELD_MASK(16,9) -> 0x1ff0000
335 */
336#define FIELD_MASK(shift,bits) (((1 << (bits))-1) << (shift))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800337
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100338/* This one is used to create a bit pattern from a given field value */
339#define FIELD_TO_BITS(val,shift,bits) (((val) & ((1 << (bits))-1)) << (shift))
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100340
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100341/* And this one does the opposite, i.e. extract a field's value from a bit pattern */
342#define FIELD_FROM_BITS(val,shift,bits) (((val) >> (shift)) & ((1 << (bits))-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800343
Yabin Cuie69c2452015-02-13 16:21:25 -0800344/* Convenience macros.
345 *
346 * These are used to form or modify the bit pattern of a given mutex value
347 */
348
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100349/* Mutex state:
350 *
351 * 0 for unlocked
352 * 1 for locked, no waiters
353 * 2 for locked, maybe waiters
354 */
355#define MUTEX_STATE_SHIFT 0
356#define MUTEX_STATE_LEN 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800357
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100358#define MUTEX_STATE_MASK FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
359#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
360#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
361
Yabin Cui17393b02015-03-21 15:08:25 -0700362#define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match PTHREAD_MUTEX_INITIALIZER */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100363#define MUTEX_STATE_LOCKED_UNCONTENDED 1 /* must be 1 due to atomic dec in unlock operation */
364#define MUTEX_STATE_LOCKED_CONTENDED 2 /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */
365
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100366#define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED)
367#define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED)
368#define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED)
369
Yabin Cui0307eee2015-11-16 20:19:31 -0800370// Return true iff the mutex is unlocked.
371#define MUTEX_STATE_BITS_IS_UNLOCKED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_UNLOCKED)
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100372
Yabin Cui0307eee2015-11-16 20:19:31 -0800373// Return true iff the mutex is locked with no waiters.
374#define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED)
375
376// return true iff the mutex is locked with maybe waiters.
377#define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED)
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100378
379/* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */
380#define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED))
381
382/* Mutex counter:
383 *
384 * We need to check for overflow before incrementing, and we also need to
385 * detect when the counter is 0
386 */
387#define MUTEX_COUNTER_SHIFT 2
388#define MUTEX_COUNTER_LEN 11
389#define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN)
390
391#define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK)
392#define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
393
394/* Used to increment the counter directly after overflow has been checked */
Yabin Cui86fc96f2015-01-29 21:50:48 -0800395#define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1, MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN)
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100396
397/* Mutex shared bit flag
398 *
399 * This flag is set to indicate that the mutex is shared among processes.
400 * This changes the futex opcode we use for futex wait/wake operations
401 * (non-shared operations are much faster).
402 */
403#define MUTEX_SHARED_SHIFT 13
404#define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1)
405
406/* Mutex type:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100407 * We support normal, recursive and errorcheck mutexes.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100408 */
409#define MUTEX_TYPE_SHIFT 14
410#define MUTEX_TYPE_LEN 2
411#define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN)
412
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100413#define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN)
414
Yabin Cui17393b02015-03-21 15:08:25 -0700415#define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_NORMAL)
416#define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_RECURSIVE)
417#define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_ERRORCHECK)
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800418// Use a special mutex type to mark priority inheritance mutexes.
Yabin Cui9651fdf2018-03-14 12:02:21 -0700419#define PI_MUTEX_STATE MUTEX_TYPE_TO_BITS(3)
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100420
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800421// For a PI mutex, it includes below fields:
422// Atomic(uint16_t) state;
Yabin Cui5a00ba72018-01-26 17:32:31 -0800423// PIMutex pi_mutex; // uint16_t pi_mutex_id in 32-bit programs
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800424//
425// state holds the following fields:
426//
427// bits: name description
428// 15-14 type mutex type, should be 3
Yabin Cui9651fdf2018-03-14 12:02:21 -0700429// 13-0 padding should be 0
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800430//
431// pi_mutex holds the state of a PI mutex.
Yabin Cui5a00ba72018-01-26 17:32:31 -0800432// pi_mutex_id holds an integer to find the state of a PI mutex.
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800433//
434// For a Non-PI mutex, it includes below fields:
435// Atomic(uint16_t) state;
436// atomic_int owner_tid; // Atomic(uint16_t) in 32-bit programs
437//
438// state holds the following fields:
439//
440// bits: name description
441// 15-14 type mutex type, can be 0 (normal), 1 (recursive), 2 (errorcheck)
442// 13 shared process-shared flag
443// 12-2 counter <number of times a thread holding a recursive Non-PI mutex> - 1
444// 1-0 state lock state (0, 1 or 2)
445//
446// bits 15-13 are constant during the lifetime of the mutex.
447//
448// owner_tid is used only in recursive and errorcheck Non-PI mutexes to hold the mutex owner
449// thread id.
450//
451// PI mutexes and Non-PI mutexes are distinguished by checking type field in state.
Yabin Cui17393b02015-03-21 15:08:25 -0700452#if defined(__LP64__)
Yabin Cui5a00ba72018-01-26 17:32:31 -0800453struct pthread_mutex_internal_t {
454 _Atomic(uint16_t) state;
455 uint16_t __pad;
456 union {
457 atomic_int owner_tid;
458 PIMutex pi_mutex;
459 };
460 char __reserved[28];
461
462 PIMutex& ToPIMutex() {
463 return pi_mutex;
464 }
465
466 void FreePIMutex() {
467 }
Yabin Cuie69c2452015-02-13 16:21:25 -0800468} __attribute__((aligned(4)));
Yabin Cui86fc96f2015-01-29 21:50:48 -0800469
Yabin Cui5a00ba72018-01-26 17:32:31 -0800470#else
471struct pthread_mutex_internal_t {
472 _Atomic(uint16_t) state;
473 union {
474 _Atomic(uint16_t) owner_tid;
475 uint16_t pi_mutex_id;
476 };
477
478 PIMutex& ToPIMutex() {
479 return PIMutexAllocator::IdToPIMutex(pi_mutex_id);
480 }
481
482 void FreePIMutex() {
483 PIMutexAllocator::FreeId(pi_mutex_id);
484 }
485} __attribute__((aligned(4)));
486#endif
487
Yabin Cui17393b02015-03-21 15:08:25 -0700488static_assert(sizeof(pthread_mutex_t) == sizeof(pthread_mutex_internal_t),
489 "pthread_mutex_t should actually be pthread_mutex_internal_t in implementation.");
490
491// For binary compatibility with old version of pthread_mutex_t, we can't use more strict alignment
492// than 4-byte alignment.
493static_assert(alignof(pthread_mutex_t) == 4,
494 "pthread_mutex_t should fulfill the alignment of pthread_mutex_internal_t.");
495
496static inline pthread_mutex_internal_t* __get_internal_mutex(pthread_mutex_t* mutex_interface) {
497 return reinterpret_cast<pthread_mutex_internal_t*>(mutex_interface);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800498}
499
Yabin Cui17393b02015-03-21 15:08:25 -0700500int pthread_mutex_init(pthread_mutex_t* mutex_interface, const pthread_mutexattr_t* attr) {
501 pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
502
503 memset(mutex, 0, sizeof(pthread_mutex_internal_t));
Yabin Cui86fc96f2015-01-29 21:50:48 -0800504
Yi Kong32bc0fc2018-08-02 17:31:13 -0700505 if (__predict_true(attr == nullptr)) {
Yabin Cui17393b02015-03-21 15:08:25 -0700506 atomic_init(&mutex->state, MUTEX_TYPE_BITS_NORMAL);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700507 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800508 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700509
Yabin Cuie69c2452015-02-13 16:21:25 -0800510 uint16_t state = 0;
Elliott Hughesdff72032013-12-11 14:54:00 -0800511 if ((*attr & MUTEXATTR_SHARED_MASK) != 0) {
Yabin Cui17393b02015-03-21 15:08:25 -0700512 state |= MUTEX_SHARED_MASK;
Elliott Hughesdff72032013-12-11 14:54:00 -0800513 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700514
515 switch (*attr & MUTEXATTR_TYPE_MASK) {
516 case PTHREAD_MUTEX_NORMAL:
Yabin Cuie69c2452015-02-13 16:21:25 -0800517 state |= MUTEX_TYPE_BITS_NORMAL;
518 break;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700519 case PTHREAD_MUTEX_RECURSIVE:
Yabin Cuie69c2452015-02-13 16:21:25 -0800520 state |= MUTEX_TYPE_BITS_RECURSIVE;
521 break;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700522 case PTHREAD_MUTEX_ERRORCHECK:
Yabin Cuie69c2452015-02-13 16:21:25 -0800523 state |= MUTEX_TYPE_BITS_ERRORCHECK;
524 break;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700525 default:
526 return EINVAL;
527 }
528
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800529 if (((*attr & MUTEXATTR_PROTOCOL_MASK) >> MUTEXATTR_PROTOCOL_SHIFT) == PTHREAD_PRIO_INHERIT) {
Yabin Cui5a00ba72018-01-26 17:32:31 -0800530#if !defined(__LP64__)
531 if (state & MUTEX_SHARED_MASK) {
532 return EINVAL;
533 }
534 int id = PIMutexAllocator::AllocId();
535 if (id == -1) {
536 return ENOMEM;
537 }
538 mutex->pi_mutex_id = id;
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800539#endif
Yabin Cui9651fdf2018-03-14 12:02:21 -0700540 atomic_init(&mutex->state, PI_MUTEX_STATE);
Yabin Cui5a00ba72018-01-26 17:32:31 -0800541 PIMutex& pi_mutex = mutex->ToPIMutex();
542 pi_mutex.type = *attr & MUTEXATTR_TYPE_MASK;
543 pi_mutex.shared = (*attr & MUTEXATTR_SHARED_MASK) != 0;
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800544 } else {
545 atomic_init(&mutex->state, state);
546 atomic_init(&mutex->owner_tid, 0);
547 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700548 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800549}
550
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800551// namespace for Non-PI mutex routines.
552namespace NonPI {
553
554static inline __always_inline int NormalMutexTryLock(pthread_mutex_internal_t* mutex,
555 uint16_t shared) {
Yabin Cuie69c2452015-02-13 16:21:25 -0800556 const uint16_t unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
557 const uint16_t locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800558
Yabin Cuie69c2452015-02-13 16:21:25 -0800559 uint16_t old_state = unlocked;
Yabin Cui17393b02015-03-21 15:08:25 -0700560 if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state,
561 locked_uncontended, memory_order_acquire, memory_order_relaxed))) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800562 return 0;
563 }
564 return EBUSY;
565}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800566
567/*
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800568 * Lock a normal Non-PI mutex.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800569 *
570 * As noted above, there are three states:
571 * 0 (unlocked, no contention)
572 * 1 (locked, no contention)
573 * 2 (locked, contention)
574 *
575 * Non-recursive mutexes don't use the thread-id or counter fields, and the
576 * "type" value is zero, so the only bits that will be set are the ones in
577 * the lock state field.
578 */
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800579static inline __always_inline int NormalMutexLock(pthread_mutex_internal_t* mutex,
580 uint16_t shared,
581 bool use_realtime_clock,
582 const timespec* abs_timeout_or_null) {
583 if (__predict_true(NormalMutexTryLock(mutex, shared) == 0)) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800584 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800585 }
Elliott Hughesdd586f22015-12-16 15:15:58 -0800586 int result = check_timespec(abs_timeout_or_null, true);
Yabin Cuic9a659c2015-11-05 15:36:08 -0800587 if (result != 0) {
588 return result;
589 }
Yabin Cui86fc96f2015-01-29 21:50:48 -0800590
591 ScopedTrace trace("Contending for pthread mutex");
592
Yabin Cuie69c2452015-02-13 16:21:25 -0800593 const uint16_t unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
594 const uint16_t locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800595
Yabin Cui86fc96f2015-01-29 21:50:48 -0800596 // We want to go to sleep until the mutex is available, which requires
597 // promoting it to locked_contended. We need to swap in the new state
Yabin Cui17393b02015-03-21 15:08:25 -0700598 // and then wait until somebody wakes us up.
Yabin Cui86fc96f2015-01-29 21:50:48 -0800599 // An atomic_exchange is used to compete with other threads for the lock.
600 // If it returns unlocked, we have acquired the lock, otherwise another
601 // thread still holds the lock and we should wait again.
602 // If lock is acquired, an acquire fence is needed to make all memory accesses
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800603 // made by other threads visible to the current CPU.
Yabin Cui17393b02015-03-21 15:08:25 -0700604 while (atomic_exchange_explicit(&mutex->state, locked_contended,
Yabin Cui86fc96f2015-01-29 21:50:48 -0800605 memory_order_acquire) != unlocked) {
Yabin Cuic9a659c2015-11-05 15:36:08 -0800606 if (__futex_wait_ex(&mutex->state, shared, locked_contended, use_realtime_clock,
607 abs_timeout_or_null) == -ETIMEDOUT) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800608 return ETIMEDOUT;
609 }
Yabin Cui86fc96f2015-01-29 21:50:48 -0800610 }
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800611 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800612}
613
614/*
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800615 * Release a normal Non-PI mutex. The caller is responsible for determining
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800616 * that we are in fact the owner of this lock.
617 */
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800618static inline __always_inline void NormalMutexUnlock(pthread_mutex_internal_t* mutex,
619 uint16_t shared) {
Yabin Cuie69c2452015-02-13 16:21:25 -0800620 const uint16_t unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
621 const uint16_t locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700622
Yabin Cui86fc96f2015-01-29 21:50:48 -0800623 // We use an atomic_exchange to release the lock. If locked_contended state
624 // is returned, some threads is waiting for the lock and we need to wake up
625 // one of them.
626 // A release fence is required to make previous stores visible to next
627 // lock owner threads.
Yabin Cui17393b02015-03-21 15:08:25 -0700628 if (atomic_exchange_explicit(&mutex->state, unlocked,
Yabin Cui86fc96f2015-01-29 21:50:48 -0800629 memory_order_release) == locked_contended) {
630 // Wake up one waiting thread. We don't know which thread will be
631 // woken or when it'll start executing -- futexes make no guarantees
632 // here. There may not even be a thread waiting.
633 //
634 // The newly-woken thread will replace the unlocked state we just set above
635 // with locked_contended state, which means that when it eventually releases
636 // the mutex it will also call FUTEX_WAKE. This results in one extra wake
637 // call whenever a lock is contended, but let us avoid forgetting anyone
638 // without requiring us to track the number of sleepers.
639 //
640 // It's possible for another thread to sneak in and grab the lock between
641 // the exchange above and the wake call below. If the new thread is "slow"
642 // and holds the lock for a while, we'll wake up a sleeper, which will swap
643 // in locked_uncontended state and then go back to sleep since the lock is
644 // still held. If the new thread is "fast", running to completion before
645 // we call wake, the thread we eventually wake will find an unlocked mutex
646 // and will execute. Either way we have correct behavior and nobody is
647 // orphaned on the wait queue.
Yabin Cui17393b02015-03-21 15:08:25 -0700648 __futex_wake_ex(&mutex->state, shared, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800649 }
650}
651
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800652/* This common inlined function is used to increment the counter of a recursive Non-PI mutex.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100653 *
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800654 * If the counter overflows, it will return EAGAIN.
655 * Otherwise, it atomically increments the counter and returns 0.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100656 *
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100657 */
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800658static inline __always_inline int RecursiveIncrement(pthread_mutex_internal_t* mutex,
659 uint16_t old_state) {
Yabin Cui86fc96f2015-01-29 21:50:48 -0800660 // Detect recursive lock overflow and return EAGAIN.
661 // This is safe because only the owner thread can modify the
662 // counter bits in the mutex value.
Yabin Cui17393b02015-03-21 15:08:25 -0700663 if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(old_state)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100664 return EAGAIN;
665 }
666
Yabin Cuie69c2452015-02-13 16:21:25 -0800667 // Other threads are able to change the lower bits (e.g. promoting it to "contended"),
668 // but the mutex counter will not overflow. So we use atomic_fetch_add operation here.
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800669 // The mutex is already locked by current thread, so we don't need an acquire fence.
Yabin Cui17393b02015-03-21 15:08:25 -0700670 atomic_fetch_add_explicit(&mutex->state, MUTEX_COUNTER_BITS_ONE, memory_order_relaxed);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800671 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800672}
673
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800674// Wait on a recursive or errorcheck Non-PI mutex.
675static inline __always_inline int RecursiveOrErrorcheckMutexWait(pthread_mutex_internal_t* mutex,
676 uint16_t shared,
677 uint16_t old_state,
678 bool use_realtime_clock,
679 const timespec* abs_timeout) {
Yabin Cuif7969852015-04-02 17:47:48 -0700680// __futex_wait always waits on a 32-bit value. But state is 16-bit. For a normal mutex, the owner_tid
681// field in mutex is not used. On 64-bit devices, the __pad field in mutex is not used.
682// But when a recursive or errorcheck mutex is used on 32-bit devices, we need to add the
683// owner_tid value in the value argument for __futex_wait, otherwise we may always get EAGAIN error.
684
685#if defined(__LP64__)
Yabin Cuic9a659c2015-11-05 15:36:08 -0800686 return __futex_wait_ex(&mutex->state, shared, old_state, use_realtime_clock, abs_timeout);
Yabin Cuif7969852015-04-02 17:47:48 -0700687
688#else
689 // This implementation works only when the layout of pthread_mutex_internal_t matches below expectation.
690 // And it is based on the assumption that Android is always in little-endian devices.
691 static_assert(offsetof(pthread_mutex_internal_t, state) == 0, "");
692 static_assert(offsetof(pthread_mutex_internal_t, owner_tid) == 2, "");
693
694 uint32_t owner_tid = atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed);
Yabin Cuic9a659c2015-11-05 15:36:08 -0800695 return __futex_wait_ex(&mutex->state, shared, (owner_tid << 16) | old_state,
696 use_realtime_clock, abs_timeout);
Yabin Cuif7969852015-04-02 17:47:48 -0700697#endif
698}
699
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800700// Lock a Non-PI mutex.
701static int MutexLockWithTimeout(pthread_mutex_internal_t* mutex, bool use_realtime_clock,
702 const timespec* abs_timeout_or_null) {
Yabin Cuie69c2452015-02-13 16:21:25 -0800703 uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
704 uint16_t mtype = (old_state & MUTEX_TYPE_MASK);
705 uint16_t shared = (old_state & MUTEX_SHARED_MASK);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800706
Yabin Cui86fc96f2015-01-29 21:50:48 -0800707 // Handle common case first.
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700708 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) {
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800709 return NormalMutexLock(mutex, shared, use_realtime_clock, abs_timeout_or_null);
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800710 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700711
Yabin Cui86fc96f2015-01-29 21:50:48 -0800712 // Do we already own this recursive or error-check mutex?
Yabin Cuie69c2452015-02-13 16:21:25 -0800713 pid_t tid = __get_thread()->tid;
714 if (tid == atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed)) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800715 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
716 return EDEADLK;
717 }
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800718 return RecursiveIncrement(mutex, old_state);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800719 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700720
Yabin Cuie69c2452015-02-13 16:21:25 -0800721 const uint16_t unlocked = mtype | shared | MUTEX_STATE_BITS_UNLOCKED;
722 const uint16_t locked_uncontended = mtype | shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
723 const uint16_t locked_contended = mtype | shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700724
Yabin Cui86fc96f2015-01-29 21:50:48 -0800725 // First, if the mutex is unlocked, try to quickly acquire it.
726 // In the optimistic case where this works, set the state to locked_uncontended.
Yabin Cui17393b02015-03-21 15:08:25 -0700727 if (old_state == unlocked) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800728 // If exchanged successfully, an acquire fence is required to make
729 // all memory accesses made by other threads visible to the current CPU.
Yabin Cui17393b02015-03-21 15:08:25 -0700730 if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state,
Yabin Cuie69c2452015-02-13 16:21:25 -0800731 locked_uncontended, memory_order_acquire, memory_order_relaxed))) {
732 atomic_store_explicit(&mutex->owner_tid, tid, memory_order_relaxed);
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100733 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700734 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700735 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100736
Brigid Smitha406ee62014-07-21 15:38:06 -0700737 ScopedTrace trace("Contending for pthread mutex");
738
Yabin Cui86fc96f2015-01-29 21:50:48 -0800739 while (true) {
Yabin Cui17393b02015-03-21 15:08:25 -0700740 if (old_state == unlocked) {
Yabin Cui86fc96f2015-01-29 21:50:48 -0800741 // NOTE: We put the state to locked_contended since we _know_ there
742 // is contention when we are in this loop. This ensures all waiters
743 // will be unlocked.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100744
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800745 // If exchanged successfully, an acquire fence is required to make
746 // all memory accesses made by other threads visible to the current CPU.
Yabin Cui17393b02015-03-21 15:08:25 -0700747 if (__predict_true(atomic_compare_exchange_weak_explicit(&mutex->state,
Yabin Cuie69c2452015-02-13 16:21:25 -0800748 &old_state, locked_contended,
Yabin Cui86fc96f2015-01-29 21:50:48 -0800749 memory_order_acquire,
750 memory_order_relaxed))) {
Yabin Cuie69c2452015-02-13 16:21:25 -0800751 atomic_store_explicit(&mutex->owner_tid, tid, memory_order_relaxed);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800752 return 0;
753 }
754 continue;
Yabin Cui17393b02015-03-21 15:08:25 -0700755 } else if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(old_state)) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800756 // We should set it to locked_contended beforing going to sleep. This can make
Yabin Cui86fc96f2015-01-29 21:50:48 -0800757 // sure waiters will be woken up eventually.
758
Yabin Cui17393b02015-03-21 15:08:25 -0700759 int new_state = MUTEX_STATE_BITS_FLIP_CONTENTION(old_state);
760 if (__predict_false(!atomic_compare_exchange_weak_explicit(&mutex->state,
761 &old_state, new_state,
Yabin Cui86fc96f2015-01-29 21:50:48 -0800762 memory_order_relaxed,
763 memory_order_relaxed))) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100764 continue;
765 }
Yabin Cui17393b02015-03-21 15:08:25 -0700766 old_state = new_state;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100767 }
768
Elliott Hughesdd586f22015-12-16 15:15:58 -0800769 int result = check_timespec(abs_timeout_or_null, true);
Yabin Cuic9a659c2015-11-05 15:36:08 -0800770 if (result != 0) {
771 return result;
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800772 }
Yabin Cuic9a659c2015-11-05 15:36:08 -0800773 // We are in locked_contended state, sleep until someone wakes us up.
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800774 if (RecursiveOrErrorcheckMutexWait(mutex, shared, old_state, use_realtime_clock,
775 abs_timeout_or_null) == -ETIMEDOUT) {
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800776 return ETIMEDOUT;
777 }
Yabin Cui17393b02015-03-21 15:08:25 -0700778 old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100779 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800780}
781
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800782} // namespace NonPI
783
Yabin Cui9651fdf2018-03-14 12:02:21 -0700784static inline __always_inline bool IsMutexDestroyed(uint16_t mutex_state) {
785 return mutex_state == 0xffff;
786}
787
788// Inlining this function in pthread_mutex_lock() adds the cost of stack frame instructions on
789// ARM64. So make it noinline.
790static int __attribute__((noinline)) HandleUsingDestroyedMutex(pthread_mutex_t* mutex,
791 const char* function_name) {
792 if (bionic_get_application_target_sdk_version() >= __ANDROID_API_P__) {
793 __fortify_fatal("%s called on a destroyed mutex (%p)", function_name, mutex);
794 }
795 return EBUSY;
796}
797
Yabin Cui17393b02015-03-21 15:08:25 -0700798int pthread_mutex_lock(pthread_mutex_t* mutex_interface) {
Christopher Ferris511cfd92015-06-09 18:46:15 -0700799#if !defined(__LP64__)
Dan Albertbaa2a972015-08-13 16:58:50 -0700800 // Some apps depend on being able to pass NULL as a mutex and get EINVAL
801 // back. Don't need to worry about it for LP64 since the ABI is brand new,
802 // but keep compatibility for LP32. http://b/19995172.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700803 if (mutex_interface == nullptr) {
Christopher Ferris511cfd92015-06-09 18:46:15 -0700804 return EINVAL;
805 }
806#endif
807
Yabin Cui17393b02015-03-21 15:08:25 -0700808 pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
Yabin Cuie69c2452015-02-13 16:21:25 -0800809 uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
810 uint16_t mtype = (old_state & MUTEX_TYPE_MASK);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800811 // Avoid slowing down fast path of normal mutex lock operation.
812 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
Yabin Cui5a00ba72018-01-26 17:32:31 -0800813 uint16_t shared = (old_state & MUTEX_SHARED_MASK);
814 if (__predict_true(NonPI::NormalMutexTryLock(mutex, shared) == 0)) {
815 return 0;
816 }
Yabin Cui9651fdf2018-03-14 12:02:21 -0700817 }
818 if (old_state == PI_MUTEX_STATE) {
Yabin Cui5a00ba72018-01-26 17:32:31 -0800819 PIMutex& m = mutex->ToPIMutex();
820 // Handle common case first.
821 if (__predict_true(PIMutexTryLock(m) == 0)) {
822 return 0;
823 }
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800824 return PIMutexTimedLock(mutex->ToPIMutex(), false, nullptr);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800825 }
Yabin Cui9651fdf2018-03-14 12:02:21 -0700826 if (__predict_false(IsMutexDestroyed(old_state))) {
827 return HandleUsingDestroyedMutex(mutex_interface, __FUNCTION__);
828 }
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800829 return NonPI::MutexLockWithTimeout(mutex, false, nullptr);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800830}
831
Yabin Cui17393b02015-03-21 15:08:25 -0700832int pthread_mutex_unlock(pthread_mutex_t* mutex_interface) {
Christopher Ferris511cfd92015-06-09 18:46:15 -0700833#if !defined(__LP64__)
Dan Albertbaa2a972015-08-13 16:58:50 -0700834 // Some apps depend on being able to pass NULL as a mutex and get EINVAL
835 // back. Don't need to worry about it for LP64 since the ABI is brand new,
836 // but keep compatibility for LP32. http://b/19995172.
Yi Kong32bc0fc2018-08-02 17:31:13 -0700837 if (mutex_interface == nullptr) {
Christopher Ferris511cfd92015-06-09 18:46:15 -0700838 return EINVAL;
839 }
840#endif
841
Yabin Cui17393b02015-03-21 15:08:25 -0700842 pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
Yabin Cuie69c2452015-02-13 16:21:25 -0800843 uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
844 uint16_t mtype = (old_state & MUTEX_TYPE_MASK);
845 uint16_t shared = (old_state & MUTEX_SHARED_MASK);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800846
Yabin Cui86fc96f2015-01-29 21:50:48 -0800847 // Handle common case first.
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700848 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800849 NonPI::NormalMutexUnlock(mutex, shared);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800850 return 0;
851 }
Yabin Cui9651fdf2018-03-14 12:02:21 -0700852 if (old_state == PI_MUTEX_STATE) {
Yabin Cui5a00ba72018-01-26 17:32:31 -0800853 return PIMutexUnlock(mutex->ToPIMutex());
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800854 }
Yabin Cui9651fdf2018-03-14 12:02:21 -0700855 if (__predict_false(IsMutexDestroyed(old_state))) {
856 return HandleUsingDestroyedMutex(mutex_interface, __FUNCTION__);
857 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700858
Yabin Cui86fc96f2015-01-29 21:50:48 -0800859 // Do we already own this recursive or error-check mutex?
Yabin Cuie69c2452015-02-13 16:21:25 -0800860 pid_t tid = __get_thread()->tid;
861 if ( tid != atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed) ) {
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700862 return EPERM;
Yabin Cuie69c2452015-02-13 16:21:25 -0800863 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700864
Yabin Cui86fc96f2015-01-29 21:50:48 -0800865 // If the counter is > 0, we can simply decrement it atomically.
866 // Since other threads can mutate the lower state bits (and only the
867 // lower state bits), use a compare_exchange loop to do it.
Yabin Cui17393b02015-03-21 15:08:25 -0700868 if (!MUTEX_COUNTER_BITS_IS_ZERO(old_state)) {
Yabin Cui86fc96f2015-01-29 21:50:48 -0800869 // We still own the mutex, so a release fence is not needed.
Yabin Cui17393b02015-03-21 15:08:25 -0700870 atomic_fetch_sub_explicit(&mutex->state, MUTEX_COUNTER_BITS_ONE, memory_order_relaxed);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800871 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700872 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100873
Yabin Cui86fc96f2015-01-29 21:50:48 -0800874 // The counter is 0, so we'are going to unlock the mutex by resetting its
875 // state to unlocked, we need to perform a atomic_exchange inorder to read
876 // the current state, which will be locked_contended if there may have waiters
877 // to awake.
878 // A release fence is required to make previous stores visible to next
879 // lock owner threads.
Yabin Cuie69c2452015-02-13 16:21:25 -0800880 atomic_store_explicit(&mutex->owner_tid, 0, memory_order_relaxed);
881 const uint16_t unlocked = mtype | shared | MUTEX_STATE_BITS_UNLOCKED;
Yabin Cui17393b02015-03-21 15:08:25 -0700882 old_state = atomic_exchange_explicit(&mutex->state, unlocked, memory_order_release);
883 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(old_state)) {
884 __futex_wake_ex(&mutex->state, shared, 1);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700885 }
Yabin Cui86fc96f2015-01-29 21:50:48 -0800886
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700887 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800888}
889
Yabin Cui17393b02015-03-21 15:08:25 -0700890int pthread_mutex_trylock(pthread_mutex_t* mutex_interface) {
891 pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800892
Yabin Cuie69c2452015-02-13 16:21:25 -0800893 uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
894 uint16_t mtype = (old_state & MUTEX_TYPE_MASK);
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800895
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700896 // Handle common case first.
897 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800898 uint16_t shared = (old_state & MUTEX_SHARED_MASK);
899 return NonPI::NormalMutexTryLock(mutex, shared);
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800900 }
Yabin Cui9651fdf2018-03-14 12:02:21 -0700901 if (old_state == PI_MUTEX_STATE) {
Yabin Cui5a00ba72018-01-26 17:32:31 -0800902 return PIMutexTryLock(mutex->ToPIMutex());
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800903 }
Yabin Cui9651fdf2018-03-14 12:02:21 -0700904 if (__predict_false(IsMutexDestroyed(old_state))) {
905 return HandleUsingDestroyedMutex(mutex_interface, __FUNCTION__);
906 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700907
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700908 // Do we already own this recursive or error-check mutex?
909 pid_t tid = __get_thread()->tid;
Yabin Cuie69c2452015-02-13 16:21:25 -0800910 if (tid == atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed)) {
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700911 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
912 return EBUSY;
913 }
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800914 return NonPI::RecursiveIncrement(mutex, old_state);
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700915 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700916
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800917 uint16_t shared = (old_state & MUTEX_SHARED_MASK);
918 const uint16_t unlocked = mtype | shared | MUTEX_STATE_BITS_UNLOCKED;
919 const uint16_t locked_uncontended = mtype | shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
920
Yabin Cui86fc96f2015-01-29 21:50:48 -0800921 // Same as pthread_mutex_lock, except that we don't want to wait, and
922 // the only operation that can succeed is a single compare_exchange to acquire the
923 // lock if it is released / not owned by anyone. No need for a complex loop.
Yabin Cui5b8e7cd2015-03-04 17:36:59 -0800924 // If exchanged successfully, an acquire fence is required to make
925 // all memory accesses made by other threads visible to the current CPU.
Yabin Cui17393b02015-03-21 15:08:25 -0700926 old_state = unlocked;
Yabin Cuie69c2452015-02-13 16:21:25 -0800927 if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state,
928 locked_uncontended,
Yabin Cui86fc96f2015-01-29 21:50:48 -0800929 memory_order_acquire,
930 memory_order_relaxed))) {
Yabin Cuie69c2452015-02-13 16:21:25 -0800931 atomic_store_explicit(&mutex->owner_tid, tid, memory_order_relaxed);
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100932 return 0;
933 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100934 return EBUSY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800935}
936
Elliott Hughes0e714a52014-03-03 16:42:47 -0800937#if !defined(__LP64__)
Yabin Cui17393b02015-03-21 15:08:25 -0700938extern "C" int pthread_mutex_lock_timeout_np(pthread_mutex_t* mutex_interface, unsigned ms) {
Yabin Cuic9a659c2015-11-05 15:36:08 -0800939 timespec ts;
940 timespec_from_ms(ts, ms);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800941 timespec abs_timeout;
Yabin Cuic9a659c2015-11-05 15:36:08 -0800942 absolute_timespec_from_timespec(abs_timeout, ts, CLOCK_MONOTONIC);
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800943 int error = NonPI::MutexLockWithTimeout(__get_internal_mutex(mutex_interface), false,
944 &abs_timeout);
Yabin Cui86fc96f2015-01-29 21:50:48 -0800945 if (error == ETIMEDOUT) {
946 error = EBUSY;
947 }
948 return error;
Elliott Hughes0e714a52014-03-03 16:42:47 -0800949}
950#endif
951
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800952static int __pthread_mutex_timedlock(pthread_mutex_t* mutex_interface, bool use_realtime_clock,
953 const timespec* abs_timeout, const char* function) {
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800954 pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
955 uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
956 uint16_t mtype = (old_state & MUTEX_TYPE_MASK);
957 // Handle common case first.
958 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
959 uint16_t shared = (old_state & MUTEX_SHARED_MASK);
960 if (__predict_true(NonPI::NormalMutexTryLock(mutex, shared) == 0)) {
961 return 0;
962 }
963 }
Yabin Cui9651fdf2018-03-14 12:02:21 -0700964 if (old_state == PI_MUTEX_STATE) {
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800965 return PIMutexTimedLock(mutex->ToPIMutex(), use_realtime_clock, abs_timeout);
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800966 }
Yabin Cui9651fdf2018-03-14 12:02:21 -0700967 if (__predict_false(IsMutexDestroyed(old_state))) {
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800968 return HandleUsingDestroyedMutex(mutex_interface, function);
Yabin Cui9651fdf2018-03-14 12:02:21 -0700969 }
Tom Cherryc6b5bcd2018-03-05 14:14:44 -0800970 return NonPI::MutexLockWithTimeout(mutex, use_realtime_clock, abs_timeout);
971}
972
973int pthread_mutex_timedlock(pthread_mutex_t* mutex_interface, const struct timespec* abs_timeout) {
974 return __pthread_mutex_timedlock(mutex_interface, true, abs_timeout, __FUNCTION__);
975}
976
977int pthread_mutex_timedlock_monotonic_np(pthread_mutex_t* mutex_interface,
978 const struct timespec* abs_timeout) {
979 return __pthread_mutex_timedlock(mutex_interface, false, abs_timeout, __FUNCTION__);
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700980}
981
Yabin Cui17393b02015-03-21 15:08:25 -0700982int pthread_mutex_destroy(pthread_mutex_t* mutex_interface) {
Yabin Cui0307eee2015-11-16 20:19:31 -0800983 pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface);
984 uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed);
Yabin Cui9651fdf2018-03-14 12:02:21 -0700985 if (__predict_false(IsMutexDestroyed(old_state))) {
986 return HandleUsingDestroyedMutex(mutex_interface, __FUNCTION__);
Yabin Cui2dec3d72018-02-02 15:45:24 -0800987 }
Yabin Cui9651fdf2018-03-14 12:02:21 -0700988 if (old_state == PI_MUTEX_STATE) {
Yabin Cui5a00ba72018-01-26 17:32:31 -0800989 int result = PIMutexDestroy(mutex->ToPIMutex());
990 if (result == 0) {
991 mutex->FreePIMutex();
Yabin Cui2dec3d72018-02-02 15:45:24 -0800992 atomic_store(&mutex->state, 0xffff);
Yabin Cui5a00ba72018-01-26 17:32:31 -0800993 }
994 return result;
Yabin Cui6b9c85b2018-01-23 12:56:18 -0800995 }
Yabin Cui0307eee2015-11-16 20:19:31 -0800996 // Store 0xffff to make the mutex unusable. Although POSIX standard says it is undefined
997 // behavior to destroy a locked mutex, we prefer not to change mutex->state in that situation.
998 if (MUTEX_STATE_BITS_IS_UNLOCKED(old_state) &&
999 atomic_compare_exchange_strong_explicit(&mutex->state, &old_state, 0xffff,
1000 memory_order_relaxed, memory_order_relaxed)) {
1001 return 0;
Yabin Cui86fc96f2015-01-29 21:50:48 -08001002 }
Yabin Cui0307eee2015-11-16 20:19:31 -08001003 return EBUSY;
Mathias Agopian7c0c3792011-09-05 23:54:55 -07001004}