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> |
Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 33 | #include <sys/mman.h> |
Pierre Peiffer | d0c884d | 2012-02-22 16:40:15 +0100 | [diff] [blame] | 34 | #include <unistd.h> |
| 35 | |
Pierre Peiffer | d0c884d | 2012-02-22 16:40:15 +0100 | [diff] [blame] | 36 | #include "pthread_internal.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 37 | |
| 38 | #include "private/bionic_atomic_inline.h" |
| 39 | #include "private/bionic_futex.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 40 | #include "private/bionic_tls.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 | |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 42 | #include "private/bionic_systrace.h" |
| 43 | |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 44 | extern void pthread_debug_mutex_lock_check(pthread_mutex_t *mutex); |
| 45 | extern void pthread_debug_mutex_unlock_check(pthread_mutex_t *mutex); |
| 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 | |
| 84 | #define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */ |
| 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 | |
| 88 | #define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) |
| 89 | #define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) |
| 90 | |
| 91 | #define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED) |
| 92 | #define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED) |
| 93 | #define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED) |
| 94 | |
| 95 | /* return true iff the mutex if locked with no waiters */ |
| 96 | #define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED) |
| 97 | |
| 98 | /* return true iff the mutex if locked with maybe waiters */ |
| 99 | #define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED) |
| 100 | |
| 101 | /* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */ |
| 102 | #define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) |
| 103 | |
| 104 | /* Mutex counter: |
| 105 | * |
| 106 | * We need to check for overflow before incrementing, and we also need to |
| 107 | * detect when the counter is 0 |
| 108 | */ |
| 109 | #define MUTEX_COUNTER_SHIFT 2 |
| 110 | #define MUTEX_COUNTER_LEN 11 |
| 111 | #define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN) |
| 112 | |
| 113 | #define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK) |
| 114 | #define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0) |
| 115 | |
| 116 | /* Used to increment the counter directly after overflow has been checked */ |
| 117 | #define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1,MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN) |
| 118 | |
| 119 | /* Returns true iff the counter is 0 */ |
| 120 | #define MUTEX_COUNTER_BITS_ARE_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0) |
| 121 | |
| 122 | /* Mutex shared bit flag |
| 123 | * |
| 124 | * This flag is set to indicate that the mutex is shared among processes. |
| 125 | * This changes the futex opcode we use for futex wait/wake operations |
| 126 | * (non-shared operations are much faster). |
| 127 | */ |
| 128 | #define MUTEX_SHARED_SHIFT 13 |
| 129 | #define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1) |
| 130 | |
| 131 | /* Mutex type: |
| 132 | * |
| 133 | * We support normal, recursive and errorcheck mutexes. |
| 134 | * |
| 135 | * The constants defined here *cannot* be changed because they must match |
| 136 | * the C library ABI which defines the following initialization values in |
| 137 | * <pthread.h>: |
| 138 | * |
| 139 | * __PTHREAD_MUTEX_INIT_VALUE |
| 140 | * __PTHREAD_RECURSIVE_MUTEX_VALUE |
| 141 | * __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE |
| 142 | */ |
| 143 | #define MUTEX_TYPE_SHIFT 14 |
| 144 | #define MUTEX_TYPE_LEN 2 |
| 145 | #define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN) |
| 146 | |
| 147 | #define MUTEX_TYPE_NORMAL 0 /* Must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */ |
| 148 | #define MUTEX_TYPE_RECURSIVE 1 |
| 149 | #define MUTEX_TYPE_ERRORCHECK 2 |
| 150 | |
| 151 | #define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN) |
| 152 | |
| 153 | #define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(MUTEX_TYPE_NORMAL) |
| 154 | #define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(MUTEX_TYPE_RECURSIVE) |
| 155 | #define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(MUTEX_TYPE_ERRORCHECK) |
| 156 | |
| 157 | /* Mutex owner field: |
| 158 | * |
| 159 | * This is only used for recursive and errorcheck mutexes. It holds the |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 160 | * tid of the owning thread. Note that this works because the Linux |
| 161 | * kernel _only_ uses 16-bit values for tids. |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 162 | * |
| 163 | * More specifically, it will wrap to 10000 when it reaches over 32768 for |
| 164 | * application processes. You can check this by running the following inside |
| 165 | * an adb shell session: |
| 166 | * |
| 167 | OLDPID=$$; |
| 168 | while true; do |
| 169 | NEWPID=$(sh -c 'echo $$') |
| 170 | if [ "$NEWPID" -gt 32768 ]; then |
| 171 | echo "AARGH: new PID $NEWPID is too high!" |
| 172 | exit 1 |
| 173 | fi |
| 174 | if [ "$NEWPID" -lt "$OLDPID" ]; then |
| 175 | echo "****** Wrapping from PID $OLDPID to $NEWPID. *******" |
| 176 | else |
| 177 | echo -n "$NEWPID!" |
| 178 | fi |
| 179 | OLDPID=$NEWPID |
| 180 | done |
| 181 | |
| 182 | * Note that you can run the same example on a desktop Linux system, |
| 183 | * the wrapping will also happen at 32768, but will go back to 300 instead. |
| 184 | */ |
| 185 | #define MUTEX_OWNER_SHIFT 16 |
| 186 | #define MUTEX_OWNER_LEN 16 |
| 187 | |
| 188 | #define MUTEX_OWNER_FROM_BITS(v) FIELD_FROM_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN) |
| 189 | #define MUTEX_OWNER_TO_BITS(v) FIELD_TO_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN) |
| 190 | |
| 191 | /* Convenience macros. |
| 192 | * |
| 193 | * These are used to form or modify the bit pattern of a given mutex value |
| 194 | */ |
| 195 | |
| 196 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 197 | |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 198 | /* a mutex attribute holds the following fields |
| 199 | * |
| 200 | * bits: name description |
| 201 | * 0-3 type type of mutex |
| 202 | * 4 shared process-shared flag |
| 203 | */ |
| 204 | #define MUTEXATTR_TYPE_MASK 0x000f |
| 205 | #define MUTEXATTR_SHARED_MASK 0x0010 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 206 | |
| 207 | |
| 208 | int pthread_mutexattr_init(pthread_mutexattr_t *attr) |
| 209 | { |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 210 | *attr = PTHREAD_MUTEX_DEFAULT; |
| 211 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) |
| 215 | { |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 216 | *attr = -1; |
| 217 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 220 | 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] | 221 | { |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 222 | int type = (*attr & MUTEXATTR_TYPE_MASK); |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 223 | |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 224 | if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK) { |
| 225 | return EINVAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 226 | } |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 227 | |
| 228 | *type_p = type; |
| 229 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) |
| 233 | { |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 234 | if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK ) { |
| 235 | return EINVAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 236 | } |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 237 | |
| 238 | *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type; |
| 239 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* process-shared mutexes are not supported at the moment */ |
| 243 | |
| 244 | int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared) |
| 245 | { |
Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 246 | switch (pshared) { |
| 247 | case PTHREAD_PROCESS_PRIVATE: |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 248 | *attr &= ~MUTEXATTR_SHARED_MASK; |
| 249 | return 0; |
| 250 | |
Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 251 | case PTHREAD_PROCESS_SHARED: |
| 252 | /* our current implementation of pthread actually supports shared |
| 253 | * mutexes but won't cleanup if a process dies with the mutex held. |
| 254 | * Nevertheless, it's better than nothing. Shared mutexes are used |
| 255 | * by surfaceflinger and audioflinger. |
| 256 | */ |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 257 | *attr |= MUTEXATTR_SHARED_MASK; |
Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 258 | return 0; |
| 259 | } |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 260 | return EINVAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 263 | int pthread_mutexattr_getpshared(const pthread_mutexattr_t* attr, int* pshared) { |
Elliott Hughes | 39b644a | 2014-03-04 10:55:39 -0800 | [diff] [blame] | 264 | *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] | 265 | return 0; |
| 266 | } |
| 267 | |
Elliott Hughes | dff7203 | 2013-12-11 14:54:00 -0800 | [diff] [blame] | 268 | int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) { |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 269 | if (__predict_true(attr == NULL)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 270 | mutex->value = MUTEX_TYPE_BITS_NORMAL; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 271 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 272 | } |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 273 | |
Elliott Hughes | dff7203 | 2013-12-11 14:54:00 -0800 | [diff] [blame] | 274 | int value = 0; |
| 275 | if ((*attr & MUTEXATTR_SHARED_MASK) != 0) { |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 276 | value |= MUTEX_SHARED_MASK; |
Elliott Hughes | dff7203 | 2013-12-11 14:54:00 -0800 | [diff] [blame] | 277 | } |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 278 | |
| 279 | switch (*attr & MUTEXATTR_TYPE_MASK) { |
| 280 | case PTHREAD_MUTEX_NORMAL: |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 281 | value |= MUTEX_TYPE_BITS_NORMAL; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 282 | break; |
| 283 | case PTHREAD_MUTEX_RECURSIVE: |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 284 | value |= MUTEX_TYPE_BITS_RECURSIVE; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 285 | break; |
| 286 | case PTHREAD_MUTEX_ERRORCHECK: |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 287 | value |= MUTEX_TYPE_BITS_ERRORCHECK; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 288 | break; |
| 289 | default: |
| 290 | return EINVAL; |
| 291 | } |
| 292 | |
| 293 | mutex->value = value; |
| 294 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 295 | } |
| 296 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * Lock a non-recursive mutex. |
| 300 | * |
| 301 | * As noted above, there are three states: |
| 302 | * 0 (unlocked, no contention) |
| 303 | * 1 (locked, no contention) |
| 304 | * 2 (locked, contention) |
| 305 | * |
| 306 | * Non-recursive mutexes don't use the thread-id or counter fields, and the |
| 307 | * "type" value is zero, so the only bits that will be set are the ones in |
| 308 | * the lock state field. |
| 309 | */ |
Elliott Hughes | 6249960 | 2014-05-28 20:30:40 -0700 | [diff] [blame] | 310 | static inline void _normal_lock(pthread_mutex_t* mutex, int shared) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 311 | /* convenience shortcuts */ |
| 312 | const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED; |
| 313 | const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 314 | /* |
| 315 | * The common case is an unlocked mutex, so we begin by trying to |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 316 | * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED). |
| 317 | * __bionic_cmpxchg() returns 0 if it made the swap successfully. |
| 318 | * If the result is nonzero, this lock is already held by another thread. |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 319 | */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 320 | if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) { |
| 321 | const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 322 | /* |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 323 | * We want to go to sleep until the mutex is available, which |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 324 | * requires promoting it to state 2 (CONTENDED). We need to |
| 325 | * swap in the new state value and then wait until somebody wakes us up. |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 326 | * |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 327 | * __bionic_swap() returns the previous value. We swap 2 in and |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 328 | * see if we got zero back; if so, we have acquired the lock. If |
| 329 | * not, another thread still holds the lock and we wait again. |
| 330 | * |
| 331 | * The second argument to the __futex_wait() call is compared |
| 332 | * against the current value. If it doesn't match, __futex_wait() |
| 333 | * returns immediately (otherwise, it sleeps for a time specified |
| 334 | * by the third argument; 0 means sleep forever). This ensures |
| 335 | * that the mutex is in state 2 when we go to sleep on it, which |
| 336 | * guarantees a wake-up call. |
| 337 | */ |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 338 | |
| 339 | ScopedTrace trace("Contending for pthread mutex"); |
| 340 | |
| 341 | |
Elliott Hughes | 6249960 | 2014-05-28 20:30:40 -0700 | [diff] [blame] | 342 | while (__bionic_swap(locked_contended, &mutex->value) != unlocked) { |
| 343 | __futex_wait_ex(&mutex->value, shared, locked_contended, NULL); |
| 344 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 345 | } |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 346 | ANDROID_MEMBAR_FULL(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Release a non-recursive mutex. The caller is responsible for determining |
| 351 | * that we are in fact the owner of this lock. |
| 352 | */ |
Elliott Hughes | 6249960 | 2014-05-28 20:30:40 -0700 | [diff] [blame] | 353 | static inline void _normal_unlock(pthread_mutex_t* mutex, int shared) { |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 354 | ANDROID_MEMBAR_FULL(); |
| 355 | |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 356 | /* |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 357 | * The mutex state will be 1 or (rarely) 2. We use an atomic decrement |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 358 | * to release the lock. __bionic_atomic_dec() returns the previous value; |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 359 | * if it wasn't 1 we have to do some additional work. |
| 360 | */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 361 | if (__bionic_atomic_dec(&mutex->value) != (shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 362 | /* |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 363 | * Start by releasing the lock. The decrement changed it from |
| 364 | * "contended lock" to "uncontended lock", which means we still |
| 365 | * hold it, and anybody who tries to sneak in will push it back |
| 366 | * to state 2. |
| 367 | * |
| 368 | * Once we set it to zero the lock is up for grabs. We follow |
| 369 | * this with a __futex_wake() to ensure that one of the waiting |
| 370 | * threads has a chance to grab it. |
| 371 | * |
| 372 | * This doesn't cause a race with the swap/wait pair in |
| 373 | * _normal_lock(), because the __futex_wait() call there will |
| 374 | * return immediately if the mutex value isn't 2. |
| 375 | */ |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 376 | mutex->value = shared; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 377 | |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 378 | /* |
| 379 | * Wake up one waiting thread. We don't know which thread will be |
| 380 | * woken or when it'll start executing -- futexes make no guarantees |
| 381 | * here. There may not even be a thread waiting. |
| 382 | * |
| 383 | * The newly-woken thread will replace the 0 we just set above |
| 384 | * with 2, which means that when it eventually releases the mutex |
| 385 | * it will also call FUTEX_WAKE. This results in one extra wake |
| 386 | * call whenever a lock is contended, but lets us avoid forgetting |
| 387 | * anyone without requiring us to track the number of sleepers. |
| 388 | * |
| 389 | * It's possible for another thread to sneak in and grab the lock |
| 390 | * between the zero assignment above and the wake call below. If |
| 391 | * the new thread is "slow" and holds the lock for a while, we'll |
| 392 | * wake up a sleeper, which will swap in a 2 and then go back to |
| 393 | * sleep since the lock is still held. If the new thread is "fast", |
| 394 | * running to completion before we call wake, the thread we |
| 395 | * eventually wake will find an unlocked mutex and will execute. |
| 396 | * Either way we have correct behavior and nobody is orphaned on |
| 397 | * the wait queue. |
| 398 | */ |
David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 399 | __futex_wake_ex(&mutex->value, shared, 1); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 403 | /* This common inlined function is used to increment the counter of an |
| 404 | * errorcheck or recursive mutex. |
| 405 | * |
| 406 | * For errorcheck mutexes, it will return EDEADLK |
| 407 | * If the counter overflows, it will return EAGAIN |
| 408 | * Otherwise, it atomically increments the counter and returns 0 |
| 409 | * after providing an acquire barrier. |
| 410 | * |
| 411 | * mtype is the current mutex type |
| 412 | * mvalue is the current mutex value (already loaded) |
| 413 | * mutex pointers to the mutex. |
| 414 | */ |
Elliott Hughes | 6249960 | 2014-05-28 20:30:40 -0700 | [diff] [blame] | 415 | static inline __always_inline int _recursive_increment(pthread_mutex_t* mutex, int mvalue, int mtype) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 416 | if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) { |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 417 | /* trying to re-lock a mutex we already acquired */ |
| 418 | return EDEADLK; |
| 419 | } |
| 420 | |
| 421 | /* Detect recursive lock overflow and return EAGAIN. |
| 422 | * This is safe because only the owner thread can modify the |
David 'Digit' Turner | b57db75 | 2012-01-24 13:20:38 +0100 | [diff] [blame] | 423 | * counter bits in the mutex value. |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 424 | */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 425 | if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) { |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 426 | return EAGAIN; |
| 427 | } |
| 428 | |
| 429 | /* We own the mutex, but other threads are able to change |
David 'Digit' Turner | b57db75 | 2012-01-24 13:20:38 +0100 | [diff] [blame] | 430 | * the lower bits (e.g. promoting it to "contended"), so we |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 431 | * need to use an atomic cmpxchg loop to update the counter. |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 432 | */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 433 | for (;;) { |
| 434 | /* increment counter, overflow was already checked */ |
| 435 | int newval = mvalue + MUTEX_COUNTER_BITS_ONE; |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 436 | if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 437 | /* mutex is still locked, not need for a memory barrier */ |
| 438 | return 0; |
| 439 | } |
| 440 | /* the value was changed, this happens when another thread changes |
| 441 | * the lower state bits from 1 to 2 to indicate contention. This |
| 442 | * cannot change the counter, so simply reload and try again. |
| 443 | */ |
| 444 | mvalue = mutex->value; |
| 445 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Elliott Hughes | 07f1ded | 2014-05-14 11:35:49 -0700 | [diff] [blame] | 448 | int pthread_mutex_lock(pthread_mutex_t* mutex) { |
Wink Saville | a12c544 | 2013-01-08 15:15:45 -0800 | [diff] [blame] | 449 | int mvalue, mtype, tid, shared; |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 450 | |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 451 | mvalue = mutex->value; |
| 452 | mtype = (mvalue & MUTEX_TYPE_MASK); |
| 453 | shared = (mvalue & MUTEX_SHARED_MASK); |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 454 | |
Brigid Smith | ff03a7a | 2014-05-28 11:45:48 -0700 | [diff] [blame] | 455 | /* Handle non-recursive case first */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 456 | if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) { |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 457 | _normal_lock(mutex, shared); |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 458 | return 0; |
| 459 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 460 | |
| 461 | /* Do we already own this recursive or error-check mutex ? */ |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 462 | tid = __get_thread()->tid; |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 463 | if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) ) |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 464 | return _recursive_increment(mutex, mvalue, mtype); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 465 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 466 | /* Add in shared state to avoid extra 'or' operations below */ |
David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 467 | mtype |= shared; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 468 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 469 | /* First, if the mutex is unlocked, try to quickly acquire it. |
| 470 | * In the optimistic case where this works, set the state to 1 to |
| 471 | * indicate locked with no contention */ |
| 472 | if (mvalue == mtype) { |
| 473 | int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
| 474 | if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) { |
| 475 | ANDROID_MEMBAR_FULL(); |
| 476 | return 0; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 477 | } |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 478 | /* argh, the value changed, reload before entering the loop */ |
| 479 | mvalue = mutex->value; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 480 | } |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 481 | |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 482 | ScopedTrace trace("Contending for pthread mutex"); |
| 483 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 484 | for (;;) { |
| 485 | int newval; |
| 486 | |
| 487 | /* if the mutex is unlocked, its value should be 'mtype' and |
| 488 | * we try to acquire it by setting its owner and state atomically. |
| 489 | * NOTE: We put the state to 2 since we _know_ there is contention |
| 490 | * when we are in this loop. This ensures all waiters will be |
| 491 | * unlocked. |
| 492 | */ |
| 493 | if (mvalue == mtype) { |
| 494 | newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED; |
| 495 | /* TODO: Change this to __bionic_cmpxchg_acquire when we |
| 496 | * implement it to get rid of the explicit memory |
| 497 | * barrier below. |
| 498 | */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 499 | if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 500 | mvalue = mutex->value; |
| 501 | continue; |
| 502 | } |
| 503 | ANDROID_MEMBAR_FULL(); |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | /* the mutex is already locked by another thread, if its state is 1 |
| 508 | * we will change it to 2 to indicate contention. */ |
| 509 | if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) { |
| 510 | newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 511 | if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 512 | mvalue = mutex->value; |
| 513 | continue; |
| 514 | } |
| 515 | mvalue = newval; |
| 516 | } |
| 517 | |
| 518 | /* wait until the mutex is unlocked */ |
| 519 | __futex_wait_ex(&mutex->value, shared, mvalue, NULL); |
| 520 | |
| 521 | mvalue = mutex->value; |
| 522 | } |
| 523 | /* NOTREACHED */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Elliott Hughes | 07f1ded | 2014-05-14 11:35:49 -0700 | [diff] [blame] | 526 | int pthread_mutex_unlock(pthread_mutex_t* mutex) { |
Wink Saville | a12c544 | 2013-01-08 15:15:45 -0800 | [diff] [blame] | 527 | int mvalue, mtype, tid, shared; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 528 | |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 529 | mvalue = mutex->value; |
| 530 | mtype = (mvalue & MUTEX_TYPE_MASK); |
| 531 | shared = (mvalue & MUTEX_SHARED_MASK); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 532 | |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 533 | /* Handle common case first */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 534 | if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) { |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 535 | _normal_unlock(mutex, shared); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 536 | return 0; |
| 537 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 538 | |
| 539 | /* Do we already own this recursive or error-check mutex ? */ |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 540 | tid = __get_thread()->tid; |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 541 | if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) ) |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 542 | return EPERM; |
| 543 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 544 | /* If the counter is > 0, we can simply decrement it atomically. |
| 545 | * Since other threads can mutate the lower state bits (and only the |
| 546 | * lower state bits), use a cmpxchg to do it. |
| 547 | */ |
| 548 | if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) { |
| 549 | for (;;) { |
| 550 | int newval = mvalue - MUTEX_COUNTER_BITS_ONE; |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 551 | if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 552 | /* success: we still own the mutex, so no memory barrier */ |
| 553 | return 0; |
| 554 | } |
| 555 | /* the value changed, so reload and loop */ |
| 556 | mvalue = mutex->value; |
| 557 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 558 | } |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 559 | |
| 560 | /* the counter is 0, so we're going to unlock the mutex by resetting |
| 561 | * its value to 'unlocked'. We need to perform a swap in order |
| 562 | * to read the current state, which will be 2 if there are waiters |
| 563 | * to awake. |
| 564 | * |
| 565 | * TODO: Change this to __bionic_swap_release when we implement it |
| 566 | * to get rid of the explicit memory barrier below. |
| 567 | */ |
| 568 | ANDROID_MEMBAR_FULL(); /* RELEASE BARRIER */ |
| 569 | mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 570 | |
| 571 | /* Wake one waiting thread, if any */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 572 | if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) { |
David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 573 | __futex_wake_ex(&mutex->value, shared, 1); |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 574 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 575 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Elliott Hughes | 07f1ded | 2014-05-14 11:35:49 -0700 | [diff] [blame] | 578 | int pthread_mutex_trylock(pthread_mutex_t* mutex) { |
Wink Saville | a12c544 | 2013-01-08 15:15:45 -0800 | [diff] [blame] | 579 | int mvalue, mtype, tid, shared; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 580 | |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 581 | mvalue = mutex->value; |
| 582 | mtype = (mvalue & MUTEX_TYPE_MASK); |
| 583 | shared = (mvalue & MUTEX_SHARED_MASK); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 584 | |
| 585 | /* Handle common case first */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 586 | if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 587 | { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 588 | if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED, |
| 589 | shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED, |
| 590 | &mutex->value) == 0) { |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 591 | ANDROID_MEMBAR_FULL(); |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 592 | return 0; |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 593 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 594 | |
| 595 | return EBUSY; |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 596 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 597 | |
| 598 | /* Do we already own this recursive or error-check mutex ? */ |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 599 | tid = __get_thread()->tid; |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 600 | if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) ) |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 601 | return _recursive_increment(mutex, mvalue, mtype); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 602 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 603 | /* Same as pthread_mutex_lock, except that we don't want to wait, and |
| 604 | * the only operation that can succeed is a single cmpxchg to acquire the |
| 605 | * lock if it is released / not owned by anyone. No need for a complex loop. |
| 606 | */ |
| 607 | mtype |= shared | MUTEX_STATE_BITS_UNLOCKED; |
| 608 | mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 609 | |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 610 | if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 611 | ANDROID_MEMBAR_FULL(); |
| 612 | return 0; |
| 613 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 614 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 615 | return EBUSY; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 616 | } |
| 617 | |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 618 | static int __pthread_mutex_timedlock(pthread_mutex_t* mutex, const timespec* abs_timeout, clockid_t clock) { |
| 619 | timespec ts; |
| 620 | |
| 621 | int mvalue = mutex->value; |
| 622 | int mtype = (mvalue & MUTEX_TYPE_MASK); |
| 623 | int shared = (mvalue & MUTEX_SHARED_MASK); |
| 624 | |
| 625 | // Handle common case first. |
| 626 | if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) { |
| 627 | const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED; |
| 628 | const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
| 629 | const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; |
| 630 | |
| 631 | // Fast path for uncontended lock. Note: MUTEX_TYPE_BITS_NORMAL is 0. |
| 632 | if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) == 0) { |
| 633 | ANDROID_MEMBAR_FULL(); |
| 634 | return 0; |
David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 635 | } |
David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 636 | |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 637 | ScopedTrace trace("Contending for timed pthread mutex"); |
| 638 | |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 639 | // Loop while needed. |
| 640 | while (__bionic_swap(locked_contended, &mutex->value) != unlocked) { |
| 641 | if (__timespec_from_absolute(&ts, abs_timeout, clock) < 0) { |
| 642 | return ETIMEDOUT; |
| 643 | } |
| 644 | __futex_wait_ex(&mutex->value, shared, locked_contended, &ts); |
| 645 | } |
| 646 | ANDROID_MEMBAR_FULL(); |
| 647 | return 0; |
| 648 | } |
David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 649 | |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 650 | // Do we already own this recursive or error-check mutex? |
| 651 | pid_t tid = __get_thread()->tid; |
| 652 | if (tid == MUTEX_OWNER_FROM_BITS(mvalue)) { |
| 653 | return _recursive_increment(mutex, mvalue, mtype); |
| 654 | } |
David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 655 | |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 656 | // The following implements the same loop as pthread_mutex_lock_impl |
| 657 | // but adds checks to ensure that the operation never exceeds the |
| 658 | // absolute expiration time. |
| 659 | mtype |= shared; |
| 660 | |
| 661 | // First try a quick lock. |
| 662 | if (mvalue == mtype) { |
| 663 | mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
| 664 | if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) { |
| 665 | ANDROID_MEMBAR_FULL(); |
| 666 | return 0; |
| 667 | } |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 668 | mvalue = mutex->value; |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 669 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 670 | |
Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 671 | ScopedTrace trace("Contending for timed pthread mutex"); |
| 672 | |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 673 | while (true) { |
| 674 | // If the value is 'unlocked', try to acquire it directly. |
| 675 | // NOTE: put state to 2 since we know there is contention. |
| 676 | if (mvalue == mtype) { // Unlocked. |
| 677 | mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED; |
| 678 | if (__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0) { |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 679 | ANDROID_MEMBAR_FULL(); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 680 | return 0; |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 681 | } |
| 682 | // The value changed before we could lock it. We need to check |
| 683 | // the time to avoid livelocks, reload the value, then loop again. |
| 684 | if (__timespec_from_absolute(&ts, abs_timeout, clock) < 0) { |
| 685 | return ETIMEDOUT; |
| 686 | } |
| 687 | |
| 688 | mvalue = mutex->value; |
| 689 | continue; |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 690 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 691 | |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 692 | // The value is locked. If 'uncontended', try to switch its state |
| 693 | // to 'contented' to ensure we get woken up later. |
| 694 | if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) { |
| 695 | int newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); |
| 696 | if (__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0) { |
| 697 | // This failed because the value changed, reload it. |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 698 | mvalue = mutex->value; |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 699 | } else { |
| 700 | // This succeeded, update mvalue. |
| 701 | mvalue = newval; |
| 702 | } |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 703 | } |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 704 | |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 705 | // Check time and update 'ts'. |
| 706 | if (__timespec_from_absolute(&ts, abs_timeout, clock) < 0) { |
| 707 | return ETIMEDOUT; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 708 | } |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 709 | |
| 710 | // Only wait to be woken up if the state is '2', otherwise we'll |
| 711 | // simply loop right now. This can happen when the second cmpxchg |
| 712 | // in our loop failed because the mutex was unlocked by another thread. |
| 713 | if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) { |
| 714 | if (__futex_wait_ex(&mutex->value, shared, mvalue, &ts) == -ETIMEDOUT) { |
| 715 | return ETIMEDOUT; |
| 716 | } |
| 717 | mvalue = mutex->value; |
| 718 | } |
| 719 | } |
| 720 | /* NOTREACHED */ |
David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 723 | #if !defined(__LP64__) |
| 724 | extern "C" int pthread_mutex_lock_timeout_np(pthread_mutex_t* mutex, unsigned ms) { |
| 725 | timespec abs_timeout; |
| 726 | clock_gettime(CLOCK_MONOTONIC, &abs_timeout); |
| 727 | abs_timeout.tv_sec += ms / 1000; |
| 728 | abs_timeout.tv_nsec += (ms % 1000) * 1000000; |
| 729 | if (abs_timeout.tv_nsec >= 1000000000) { |
| 730 | abs_timeout.tv_sec++; |
| 731 | abs_timeout.tv_nsec -= 1000000000; |
| 732 | } |
| 733 | |
Elliott Hughes | 07f1ded | 2014-05-14 11:35:49 -0700 | [diff] [blame] | 734 | int error = __pthread_mutex_timedlock(mutex, &abs_timeout, CLOCK_MONOTONIC); |
| 735 | if (error == ETIMEDOUT) { |
| 736 | error = EBUSY; |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 737 | } |
Elliott Hughes | 07f1ded | 2014-05-14 11:35:49 -0700 | [diff] [blame] | 738 | return error; |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 739 | } |
| 740 | #endif |
| 741 | |
| 742 | int pthread_mutex_timedlock(pthread_mutex_t* mutex, const timespec* abs_timeout) { |
| 743 | return __pthread_mutex_timedlock(mutex, abs_timeout, CLOCK_REALTIME); |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Elliott Hughes | 07f1ded | 2014-05-14 11:35:49 -0700 | [diff] [blame] | 746 | int pthread_mutex_destroy(pthread_mutex_t* mutex) { |
| 747 | // Use trylock to ensure that the mutex is valid and not already locked. |
| 748 | int error = pthread_mutex_trylock(mutex); |
| 749 | if (error != 0) { |
| 750 | return error; |
| 751 | } |
| 752 | mutex->value = 0xdead10cc; |
| 753 | return 0; |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 754 | } |