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