The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
Pierre Peiffer | d0c884d | 2012-02-22 16:40:15 +0100 | [diff] [blame] | 28 | |
Elliott Hughes | 6f94de3 | 2013-02-12 06:06:22 +0000 | [diff] [blame] | 29 | #include <pthread.h> |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 30 | |
| 31 | #include <errno.h> |
| 32 | #include <limits.h> |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 33 | #include <stdatomic.h> |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 34 | #include <string.h> |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 35 | #include <sys/cdefs.h> |
Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 36 | #include <sys/mman.h> |
Pierre Peiffer | d0c884d | 2012-02-22 16:40:15 +0100 | [diff] [blame] | 37 | #include <unistd.h> |
| 38 | |
Pierre Peiffer | d0c884d | 2012-02-22 16:40:15 +0100 | [diff] [blame] | 39 | #include "pthread_internal.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 40 | |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 41 | #include "private/bionic_constants.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 42 | #include "private/bionic_futex.h" |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 43 | #include "private/bionic_systrace.h" |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 44 | #include "private/bionic_time_conversions.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 45 | #include "private/bionic_tls.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 46 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | /* a mutex is implemented as a 32-bit integer holding the following fields |
| 48 | * |
| 49 | * bits: name description |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 50 | * 31-16 tid owner thread's tid (recursive and errorcheck only) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 51 | * 15-14 type mutex type |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 52 | * 13 shared process-shared flag |
| 53 | * 12-2 counter counter of recursive mutexes |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 54 | * 1-0 state lock state (0, 1 or 2) |
| 55 | */ |
| 56 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 57 | /* Convenience macro, creates a mask of 'bits' bits that starts from |
| 58 | * the 'shift'-th least significant bit in a 32-bit word. |
| 59 | * |
| 60 | * Examples: FIELD_MASK(0,4) -> 0xf |
| 61 | * FIELD_MASK(16,9) -> 0x1ff0000 |
| 62 | */ |
| 63 | #define FIELD_MASK(shift,bits) (((1 << (bits))-1) << (shift)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 65 | /* This one is used to create a bit pattern from a given field value */ |
| 66 | #define FIELD_TO_BITS(val,shift,bits) (((val) & ((1 << (bits))-1)) << (shift)) |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 67 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 68 | /* And this one does the opposite, i.e. extract a field's value from a bit pattern */ |
| 69 | #define FIELD_FROM_BITS(val,shift,bits) (((val) >> (shift)) & ((1 << (bits))-1)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 70 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 71 | /* Mutex state: |
| 72 | * |
| 73 | * 0 for unlocked |
| 74 | * 1 for locked, no waiters |
| 75 | * 2 for locked, maybe waiters |
| 76 | */ |
| 77 | #define MUTEX_STATE_SHIFT 0 |
| 78 | #define MUTEX_STATE_LEN 2 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 79 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 80 | #define MUTEX_STATE_MASK FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) |
| 81 | #define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) |
| 82 | #define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) |
| 83 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 84 | #define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match PTHREAD_MUTEX_INITIALIZER */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 85 | #define MUTEX_STATE_LOCKED_UNCONTENDED 1 /* must be 1 due to atomic dec in unlock operation */ |
| 86 | #define MUTEX_STATE_LOCKED_CONTENDED 2 /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */ |
| 87 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 88 | #define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED) |
| 89 | #define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED) |
| 90 | #define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED) |
| 91 | |
| 92 | /* return true iff the mutex if locked with no waiters */ |
| 93 | #define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED) |
| 94 | |
| 95 | /* return true iff the mutex if locked with maybe waiters */ |
| 96 | #define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED) |
| 97 | |
| 98 | /* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */ |
| 99 | #define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) |
| 100 | |
| 101 | /* Mutex counter: |
| 102 | * |
| 103 | * We need to check for overflow before incrementing, and we also need to |
| 104 | * detect when the counter is 0 |
| 105 | */ |
| 106 | #define MUTEX_COUNTER_SHIFT 2 |
| 107 | #define MUTEX_COUNTER_LEN 11 |
| 108 | #define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN) |
| 109 | |
| 110 | #define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK) |
| 111 | #define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0) |
| 112 | |
| 113 | /* Used to increment the counter directly after overflow has been checked */ |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 114 | #define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1, MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN) |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 115 | |
| 116 | /* Mutex shared bit flag |
| 117 | * |
| 118 | * This flag is set to indicate that the mutex is shared among processes. |
| 119 | * This changes the futex opcode we use for futex wait/wake operations |
| 120 | * (non-shared operations are much faster). |
| 121 | */ |
| 122 | #define MUTEX_SHARED_SHIFT 13 |
| 123 | #define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1) |
| 124 | |
| 125 | /* Mutex type: |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 126 | * We support normal, recursive and errorcheck mutexes. |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 127 | */ |
| 128 | #define MUTEX_TYPE_SHIFT 14 |
| 129 | #define MUTEX_TYPE_LEN 2 |
| 130 | #define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN) |
| 131 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 132 | #define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN) |
| 133 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 134 | #define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_NORMAL) |
| 135 | #define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_RECURSIVE) |
| 136 | #define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_ERRORCHECK) |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 137 | |
| 138 | /* Mutex owner field: |
| 139 | * |
| 140 | * This is only used for recursive and errorcheck mutexes. It holds the |
Yabin Cui | 140f367 | 2015-02-03 10:32:00 -0800 | [diff] [blame] | 141 | * tid of the owning thread. We use 16 bits to represent tid here, |
| 142 | * so the highest tid is 65535. There is a test to check /proc/sys/kernel/pid_max |
| 143 | * to make sure it will not exceed our limit. |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 144 | */ |
| 145 | #define MUTEX_OWNER_SHIFT 16 |
| 146 | #define MUTEX_OWNER_LEN 16 |
| 147 | |
| 148 | #define MUTEX_OWNER_FROM_BITS(v) FIELD_FROM_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN) |
| 149 | #define MUTEX_OWNER_TO_BITS(v) FIELD_TO_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN) |
| 150 | |
| 151 | /* Convenience macros. |
| 152 | * |
| 153 | * These are used to form or modify the bit pattern of a given mutex value |
| 154 | */ |
| 155 | |
| 156 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 157 | |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 158 | /* a mutex attribute holds the following fields |
| 159 | * |
| 160 | * bits: name description |
| 161 | * 0-3 type type of mutex |
| 162 | * 4 shared process-shared flag |
| 163 | */ |
| 164 | #define MUTEXATTR_TYPE_MASK 0x000f |
| 165 | #define MUTEXATTR_SHARED_MASK 0x0010 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 166 | |
| 167 | |
| 168 | int pthread_mutexattr_init(pthread_mutexattr_t *attr) |
| 169 | { |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 170 | *attr = PTHREAD_MUTEX_DEFAULT; |
| 171 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) |
| 175 | { |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 176 | *attr = -1; |
| 177 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 180 | int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type_p) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 181 | { |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 182 | int type = (*attr & MUTEXATTR_TYPE_MASK); |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 183 | |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 184 | if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK) { |
| 185 | return EINVAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 186 | } |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 187 | |
| 188 | *type_p = type; |
| 189 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) |
| 193 | { |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 194 | if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK ) { |
| 195 | return EINVAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 196 | } |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 197 | |
| 198 | *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type; |
| 199 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* process-shared mutexes are not supported at the moment */ |
| 203 | |
| 204 | int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared) |
| 205 | { |
Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 206 | switch (pshared) { |
| 207 | case PTHREAD_PROCESS_PRIVATE: |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 208 | *attr &= ~MUTEXATTR_SHARED_MASK; |
| 209 | return 0; |
| 210 | |
Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 211 | case PTHREAD_PROCESS_SHARED: |
| 212 | /* our current implementation of pthread actually supports shared |
| 213 | * mutexes but won't cleanup if a process dies with the mutex held. |
| 214 | * Nevertheless, it's better than nothing. Shared mutexes are used |
| 215 | * by surfaceflinger and audioflinger. |
| 216 | */ |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 217 | *attr |= MUTEXATTR_SHARED_MASK; |
Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 218 | return 0; |
| 219 | } |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 220 | return EINVAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 223 | int pthread_mutexattr_getpshared(const pthread_mutexattr_t* attr, int* pshared) { |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 224 | *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 228 | struct pthread_mutex_internal_t { |
| 229 | atomic_int state; |
| 230 | #if defined(__LP64__) |
| 231 | char __reserved[36]; |
| 232 | #endif |
| 233 | }; |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 234 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 235 | static_assert(sizeof(pthread_mutex_t) == sizeof(pthread_mutex_internal_t), |
| 236 | "pthread_mutex_t should actually be pthread_mutex_internal_t in implementation."); |
| 237 | |
| 238 | // For binary compatibility with old version of pthread_mutex_t, we can't use more strict alignment |
| 239 | // than 4-byte alignment. |
| 240 | static_assert(alignof(pthread_mutex_t) == 4, |
| 241 | "pthread_mutex_t should fulfill the alignment of pthread_mutex_internal_t."); |
| 242 | |
| 243 | static inline pthread_mutex_internal_t* __get_internal_mutex(pthread_mutex_t* mutex_interface) { |
| 244 | return reinterpret_cast<pthread_mutex_internal_t*>(mutex_interface); |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 247 | int pthread_mutex_init(pthread_mutex_t* mutex_interface, const pthread_mutexattr_t* attr) { |
| 248 | pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); |
| 249 | |
| 250 | memset(mutex, 0, sizeof(pthread_mutex_internal_t)); |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 251 | |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 252 | if (__predict_true(attr == NULL)) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 253 | atomic_init(&mutex->state, MUTEX_TYPE_BITS_NORMAL); |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 254 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 255 | } |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 256 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 257 | int state = 0; |
Elliott Hughes | dff7203 | 2013-12-11 14:54:00 -0800 | [diff] [blame] | 258 | if ((*attr & MUTEXATTR_SHARED_MASK) != 0) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 259 | state |= MUTEX_SHARED_MASK; |
Elliott Hughes | dff7203 | 2013-12-11 14:54:00 -0800 | [diff] [blame] | 260 | } |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 261 | |
| 262 | switch (*attr & MUTEXATTR_TYPE_MASK) { |
| 263 | case PTHREAD_MUTEX_NORMAL: |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 264 | state |= MUTEX_TYPE_BITS_NORMAL; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 265 | break; |
| 266 | case PTHREAD_MUTEX_RECURSIVE: |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 267 | state |= MUTEX_TYPE_BITS_RECURSIVE; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 268 | break; |
| 269 | case PTHREAD_MUTEX_ERRORCHECK: |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 270 | state |= MUTEX_TYPE_BITS_ERRORCHECK; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 271 | break; |
| 272 | default: |
| 273 | return EINVAL; |
| 274 | } |
| 275 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 276 | atomic_init(&mutex->state, state); |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 277 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 278 | } |
| 279 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 280 | static inline __always_inline int __pthread_normal_mutex_trylock(pthread_mutex_internal_t* mutex, |
| 281 | int shared) { |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 282 | const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED; |
| 283 | const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
| 284 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 285 | int old_state = unlocked; |
| 286 | if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state, |
| 287 | locked_uncontended, memory_order_acquire, memory_order_relaxed))) { |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | return EBUSY; |
| 291 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 292 | |
| 293 | /* |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 294 | * Lock a mutex of type NORMAL. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 295 | * |
| 296 | * As noted above, there are three states: |
| 297 | * 0 (unlocked, no contention) |
| 298 | * 1 (locked, no contention) |
| 299 | * 2 (locked, contention) |
| 300 | * |
| 301 | * Non-recursive mutexes don't use the thread-id or counter fields, and the |
| 302 | * "type" value is zero, so the only bits that will be set are the ones in |
| 303 | * the lock state field. |
| 304 | */ |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 305 | static inline __always_inline int __pthread_normal_mutex_lock(pthread_mutex_internal_t* mutex, |
| 306 | int shared, |
| 307 | const timespec* abs_timeout_or_null, |
| 308 | clockid_t clock) { |
| 309 | if (__predict_true(__pthread_normal_mutex_trylock(mutex, shared) == 0)) { |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 310 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 311 | } |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 312 | |
| 313 | ScopedTrace trace("Contending for pthread mutex"); |
| 314 | |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 315 | const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED; |
| 316 | const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; |
| 317 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 318 | // We want to go to sleep until the mutex is available, which requires |
| 319 | // promoting it to locked_contended. We need to swap in the new state |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 320 | // and then wait until somebody wakes us up. |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 321 | // An atomic_exchange is used to compete with other threads for the lock. |
| 322 | // If it returns unlocked, we have acquired the lock, otherwise another |
| 323 | // thread still holds the lock and we should wait again. |
| 324 | // If lock is acquired, an acquire fence is needed to make all memory accesses |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 325 | // made by other threads visible to the current CPU. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 326 | while (atomic_exchange_explicit(&mutex->state, locked_contended, |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 327 | memory_order_acquire) != unlocked) { |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 328 | timespec ts; |
| 329 | timespec* rel_timeout = NULL; |
| 330 | if (abs_timeout_or_null != NULL) { |
| 331 | rel_timeout = &ts; |
| 332 | if (!timespec_from_absolute_timespec(*rel_timeout, *abs_timeout_or_null, clock)) { |
| 333 | return ETIMEDOUT; |
| 334 | } |
| 335 | } |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 336 | if (__futex_wait_ex(&mutex->state, shared, locked_contended, rel_timeout) == -ETIMEDOUT) { |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 337 | return ETIMEDOUT; |
| 338 | } |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 339 | } |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 340 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 344 | * Release a mutex of type NORMAL. The caller is responsible for determining |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 345 | * that we are in fact the owner of this lock. |
| 346 | */ |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 347 | static inline __always_inline void __pthread_normal_mutex_unlock(pthread_mutex_internal_t* mutex, |
| 348 | int shared) { |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 349 | const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED; |
| 350 | const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 351 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 352 | // We use an atomic_exchange to release the lock. If locked_contended state |
| 353 | // is returned, some threads is waiting for the lock and we need to wake up |
| 354 | // one of them. |
| 355 | // A release fence is required to make previous stores visible to next |
| 356 | // lock owner threads. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 357 | if (atomic_exchange_explicit(&mutex->state, unlocked, |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 358 | memory_order_release) == locked_contended) { |
| 359 | // Wake up one waiting thread. We don't know which thread will be |
| 360 | // woken or when it'll start executing -- futexes make no guarantees |
| 361 | // here. There may not even be a thread waiting. |
| 362 | // |
| 363 | // The newly-woken thread will replace the unlocked state we just set above |
| 364 | // with locked_contended state, which means that when it eventually releases |
| 365 | // the mutex it will also call FUTEX_WAKE. This results in one extra wake |
| 366 | // call whenever a lock is contended, but let us avoid forgetting anyone |
| 367 | // without requiring us to track the number of sleepers. |
| 368 | // |
| 369 | // It's possible for another thread to sneak in and grab the lock between |
| 370 | // the exchange above and the wake call below. If the new thread is "slow" |
| 371 | // and holds the lock for a while, we'll wake up a sleeper, which will swap |
| 372 | // in locked_uncontended state and then go back to sleep since the lock is |
| 373 | // still held. If the new thread is "fast", running to completion before |
| 374 | // we call wake, the thread we eventually wake will find an unlocked mutex |
| 375 | // and will execute. Either way we have correct behavior and nobody is |
| 376 | // orphaned on the wait queue. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 377 | __futex_wake_ex(&mutex->state, shared, 1); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 381 | /* This common inlined function is used to increment the counter of a recursive mutex. |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 382 | * |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 383 | * If the counter overflows, it will return EAGAIN. |
| 384 | * Otherwise, it atomically increments the counter and returns 0. |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 385 | * |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 386 | */ |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 387 | static inline __always_inline int __recursive_increment(pthread_mutex_internal_t* mutex, |
| 388 | int old_state) { |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 389 | // Detect recursive lock overflow and return EAGAIN. |
| 390 | // This is safe because only the owner thread can modify the |
| 391 | // counter bits in the mutex value. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 392 | if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(old_state)) { |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 393 | return EAGAIN; |
| 394 | } |
| 395 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 396 | // We own the mutex, but other threads are able to change the lower bits |
| 397 | // (e.g. promoting it to "contended"), so we need to use an atomic exchange |
| 398 | // loop to update the counter. The counter will not overflow in the loop, |
| 399 | // as only the owner thread can change it. |
| 400 | // The mutex is still locked, so we don't need a release fence. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 401 | atomic_fetch_add_explicit(&mutex->state, MUTEX_COUNTER_BITS_ONE, memory_order_relaxed); |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 402 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 405 | static int __pthread_mutex_lock_with_timeout(pthread_mutex_internal_t* mutex, |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 406 | const timespec* abs_timeout_or_null, clockid_t clock) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 407 | int old_state, mtype, tid, shared; |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 408 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 409 | old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); |
| 410 | mtype = (old_state & MUTEX_TYPE_MASK); |
| 411 | shared = (old_state & MUTEX_SHARED_MASK); |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 412 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 413 | // Handle common case first. |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 414 | if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 415 | return __pthread_normal_mutex_lock(mutex, shared, abs_timeout_or_null, clock); |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 416 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 417 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 418 | // Do we already own this recursive or error-check mutex? |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 419 | tid = __get_thread()->tid; |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 420 | if (tid == MUTEX_OWNER_FROM_BITS(old_state)) { |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 421 | if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) { |
| 422 | return EDEADLK; |
| 423 | } |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 424 | return __recursive_increment(mutex, old_state); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 425 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 426 | |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 427 | const int unlocked = mtype | shared | MUTEX_STATE_BITS_UNLOCKED; |
| 428 | const int locked_uncontended = mtype | shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
| 429 | const int locked_contended = mtype | shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 430 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 431 | // First, if the mutex is unlocked, try to quickly acquire it. |
| 432 | // In the optimistic case where this works, set the state to locked_uncontended. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 433 | if (old_state == unlocked) { |
| 434 | int new_state = MUTEX_OWNER_TO_BITS(tid) | locked_uncontended; |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 435 | // If exchanged successfully, an acquire fence is required to make |
| 436 | // all memory accesses made by other threads visible to the current CPU. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 437 | if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state, |
| 438 | new_state, memory_order_acquire, memory_order_relaxed))) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 439 | return 0; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 440 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 441 | } |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 442 | |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 443 | ScopedTrace trace("Contending for pthread mutex"); |
| 444 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 445 | while (true) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 446 | if (old_state == unlocked) { |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 447 | // NOTE: We put the state to locked_contended since we _know_ there |
| 448 | // is contention when we are in this loop. This ensures all waiters |
| 449 | // will be unlocked. |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 450 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 451 | int new_state = MUTEX_OWNER_TO_BITS(tid) | locked_contended; |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 452 | // If exchanged successfully, an acquire fence is required to make |
| 453 | // all memory accesses made by other threads visible to the current CPU. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 454 | if (__predict_true(atomic_compare_exchange_weak_explicit(&mutex->state, |
| 455 | &old_state, new_state, |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 456 | memory_order_acquire, |
| 457 | memory_order_relaxed))) { |
| 458 | return 0; |
| 459 | } |
| 460 | continue; |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 461 | } else if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(old_state)) { |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 462 | // We should set it to locked_contended beforing going to sleep. This can make |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 463 | // sure waiters will be woken up eventually. |
| 464 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 465 | int new_state = MUTEX_STATE_BITS_FLIP_CONTENTION(old_state); |
| 466 | if (__predict_false(!atomic_compare_exchange_weak_explicit(&mutex->state, |
| 467 | &old_state, new_state, |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 468 | memory_order_relaxed, |
| 469 | memory_order_relaxed))) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 470 | continue; |
| 471 | } |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 472 | old_state = new_state; |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 473 | } |
| 474 | |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 475 | // We are in locked_contended state, sleep until someone wakes us up. |
| 476 | timespec ts; |
| 477 | timespec* rel_timeout = NULL; |
| 478 | if (abs_timeout_or_null != NULL) { |
| 479 | rel_timeout = &ts; |
| 480 | if (!timespec_from_absolute_timespec(*rel_timeout, *abs_timeout_or_null, clock)) { |
| 481 | return ETIMEDOUT; |
| 482 | } |
| 483 | } |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 484 | if (__futex_wait_ex(&mutex->state, shared, old_state, rel_timeout) == -ETIMEDOUT) { |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 485 | return ETIMEDOUT; |
| 486 | } |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 487 | old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 488 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 491 | int pthread_mutex_lock(pthread_mutex_t* mutex_interface) { |
| 492 | pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 493 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 494 | int old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); |
| 495 | int mtype = (old_state & MUTEX_TYPE_MASK); |
| 496 | int shared = (old_state & MUTEX_SHARED_MASK); |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 497 | // Avoid slowing down fast path of normal mutex lock operation. |
| 498 | if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 499 | if (__predict_true(__pthread_normal_mutex_trylock(mutex, shared) == 0)) { |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | } |
| 503 | return __pthread_mutex_lock_with_timeout(mutex, NULL, 0); |
| 504 | } |
| 505 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 506 | int pthread_mutex_unlock(pthread_mutex_t* mutex_interface) { |
| 507 | pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 508 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 509 | int old_state, mtype, tid, shared; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 510 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 511 | old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); |
| 512 | mtype = (old_state & MUTEX_TYPE_MASK); |
| 513 | shared = (old_state & MUTEX_SHARED_MASK); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 514 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 515 | // Handle common case first. |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 516 | if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 517 | __pthread_normal_mutex_unlock(mutex, shared); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 518 | return 0; |
| 519 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 520 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 521 | // Do we already own this recursive or error-check mutex? |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 522 | tid = __get_thread()->tid; |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 523 | if ( tid != MUTEX_OWNER_FROM_BITS(old_state) ) |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 524 | return EPERM; |
| 525 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 526 | // If the counter is > 0, we can simply decrement it atomically. |
| 527 | // Since other threads can mutate the lower state bits (and only the |
| 528 | // lower state bits), use a compare_exchange loop to do it. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 529 | if (!MUTEX_COUNTER_BITS_IS_ZERO(old_state)) { |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 530 | // We still own the mutex, so a release fence is not needed. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 531 | atomic_fetch_sub_explicit(&mutex->state, MUTEX_COUNTER_BITS_ONE, memory_order_relaxed); |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 532 | return 0; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 533 | } |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 534 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 535 | // The counter is 0, so we'are going to unlock the mutex by resetting its |
| 536 | // state to unlocked, we need to perform a atomic_exchange inorder to read |
| 537 | // the current state, which will be locked_contended if there may have waiters |
| 538 | // to awake. |
| 539 | // A release fence is required to make previous stores visible to next |
| 540 | // lock owner threads. |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 541 | const int unlocked = mtype | shared | MUTEX_STATE_BITS_UNLOCKED; |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 542 | old_state = atomic_exchange_explicit(&mutex->state, unlocked, memory_order_release); |
| 543 | if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(old_state)) { |
| 544 | __futex_wake_ex(&mutex->state, shared, 1); |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 545 | } |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 546 | |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 547 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 550 | int pthread_mutex_trylock(pthread_mutex_t* mutex_interface) { |
| 551 | pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 552 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 553 | int old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); |
| 554 | int mtype = (old_state & MUTEX_TYPE_MASK); |
| 555 | int shared = (old_state & MUTEX_SHARED_MASK); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 556 | |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 557 | const int unlocked = mtype | shared | MUTEX_STATE_BITS_UNLOCKED; |
| 558 | const int locked_uncontended = mtype | shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
| 559 | |
Elliott Hughes | 5b1111a | 2014-10-24 19:33:11 -0700 | [diff] [blame] | 560 | // Handle common case first. |
| 561 | if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) { |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 562 | return __pthread_normal_mutex_trylock(mutex, shared); |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 563 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 564 | |
Elliott Hughes | 5b1111a | 2014-10-24 19:33:11 -0700 | [diff] [blame] | 565 | // Do we already own this recursive or error-check mutex? |
| 566 | pid_t tid = __get_thread()->tid; |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 567 | if (tid == MUTEX_OWNER_FROM_BITS(old_state)) { |
Elliott Hughes | 5b1111a | 2014-10-24 19:33:11 -0700 | [diff] [blame] | 568 | if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) { |
| 569 | return EBUSY; |
| 570 | } |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 571 | return __recursive_increment(mutex, old_state); |
Elliott Hughes | 5b1111a | 2014-10-24 19:33:11 -0700 | [diff] [blame] | 572 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 573 | |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 574 | // Same as pthread_mutex_lock, except that we don't want to wait, and |
| 575 | // the only operation that can succeed is a single compare_exchange to acquire the |
| 576 | // lock if it is released / not owned by anyone. No need for a complex loop. |
Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 577 | // If exchanged successfully, an acquire fence is required to make |
| 578 | // all memory accesses made by other threads visible to the current CPU. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 579 | old_state = unlocked; |
| 580 | int new_state = MUTEX_OWNER_TO_BITS(tid) | locked_uncontended; |
| 581 | if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state, new_state, |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 582 | memory_order_acquire, |
| 583 | memory_order_relaxed))) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 584 | return 0; |
| 585 | } |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 586 | return EBUSY; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 589 | #if !defined(__LP64__) |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 590 | extern "C" int pthread_mutex_lock_timeout_np(pthread_mutex_t* mutex_interface, unsigned ms) { |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 591 | timespec abs_timeout; |
| 592 | clock_gettime(CLOCK_MONOTONIC, &abs_timeout); |
| 593 | abs_timeout.tv_sec += ms / 1000; |
| 594 | abs_timeout.tv_nsec += (ms % 1000) * 1000000; |
| 595 | if (abs_timeout.tv_nsec >= NS_PER_S) { |
| 596 | abs_timeout.tv_sec++; |
| 597 | abs_timeout.tv_nsec -= NS_PER_S; |
| 598 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 599 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 600 | int error = __pthread_mutex_lock_with_timeout(__get_internal_mutex(mutex_interface), |
| 601 | &abs_timeout, CLOCK_MONOTONIC); |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 602 | if (error == ETIMEDOUT) { |
| 603 | error = EBUSY; |
| 604 | } |
| 605 | return error; |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 606 | } |
| 607 | #endif |
| 608 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 609 | int pthread_mutex_timedlock(pthread_mutex_t* mutex_interface, const timespec* abs_timeout) { |
| 610 | return __pthread_mutex_lock_with_timeout(__get_internal_mutex(mutex_interface), |
| 611 | abs_timeout, CLOCK_REALTIME); |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 614 | int pthread_mutex_destroy(pthread_mutex_t* mutex_interface) { |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 615 | // Use trylock to ensure that the mutex is valid and not already locked. |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 616 | int error = pthread_mutex_trylock(mutex_interface); |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 617 | if (error != 0) { |
| 618 | return error; |
| 619 | } |
| 620 | |
Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame^] | 621 | pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); |
| 622 | atomic_store_explicit(&mutex->state, 0xdead10cc, memory_order_relaxed); |
Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 623 | return 0; |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 624 | } |