| 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" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 41 | #include "private/bionic_tls.h" | 
|  | 42 | #include "private/thread_private.h" | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | { | 
|  | 210 | if (attr) { | 
|  | 211 | *attr = PTHREAD_MUTEX_DEFAULT; | 
|  | 212 | return 0; | 
|  | 213 | } else { | 
|  | 214 | return EINVAL; | 
|  | 215 | } | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) | 
|  | 219 | { | 
|  | 220 | if (attr) { | 
|  | 221 | *attr = -1; | 
|  | 222 | return 0; | 
|  | 223 | } else { | 
|  | 224 | return EINVAL; | 
|  | 225 | } | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type) | 
|  | 229 | { | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 230 | if (attr) { | 
|  | 231 | int  atype = (*attr & MUTEXATTR_TYPE_MASK); | 
|  | 232 |  | 
|  | 233 | if (atype >= PTHREAD_MUTEX_NORMAL && | 
|  | 234 | atype <= PTHREAD_MUTEX_ERRORCHECK) { | 
|  | 235 | *type = atype; | 
|  | 236 | return 0; | 
|  | 237 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 238 | } | 
|  | 239 | return EINVAL; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) | 
|  | 243 | { | 
|  | 244 | if (attr && type >= PTHREAD_MUTEX_NORMAL && | 
|  | 245 | type <= PTHREAD_MUTEX_ERRORCHECK ) { | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 246 | *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 247 | return 0; | 
|  | 248 | } | 
|  | 249 | return EINVAL; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | /* process-shared mutexes are not supported at the moment */ | 
|  | 253 |  | 
|  | 254 | int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int  pshared) | 
|  | 255 | { | 
|  | 256 | if (!attr) | 
|  | 257 | return EINVAL; | 
|  | 258 |  | 
| Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 259 | switch (pshared) { | 
|  | 260 | case PTHREAD_PROCESS_PRIVATE: | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 261 | *attr &= ~MUTEXATTR_SHARED_MASK; | 
|  | 262 | return 0; | 
|  | 263 |  | 
| Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 264 | case PTHREAD_PROCESS_SHARED: | 
|  | 265 | /* our current implementation of pthread actually supports shared | 
|  | 266 | * mutexes but won't cleanup if a process dies with the mutex held. | 
|  | 267 | * Nevertheless, it's better than nothing. Shared mutexes are used | 
|  | 268 | * by surfaceflinger and audioflinger. | 
|  | 269 | */ | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 270 | *attr |= MUTEXATTR_SHARED_MASK; | 
| Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 271 | return 0; | 
|  | 272 | } | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 273 | return EINVAL; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 274 | } | 
|  | 275 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 276 | int pthread_mutexattr_getpshared(const pthread_mutexattr_t* attr, int* pshared) { | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 277 | if (!attr || !pshared) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 278 | return EINVAL; | 
|  | 279 |  | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 280 | *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED | 
|  | 281 | : PTHREAD_PROCESS_PRIVATE; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 282 | return 0; | 
|  | 283 | } | 
|  | 284 |  | 
| Elliott Hughes | dff7203 | 2013-12-11 14:54:00 -0800 | [diff] [blame] | 285 | 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] | 286 | if (__predict_true(attr == NULL)) { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 287 | mutex->value = MUTEX_TYPE_BITS_NORMAL; | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 288 | return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 289 | } | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 290 |  | 
| Elliott Hughes | dff7203 | 2013-12-11 14:54:00 -0800 | [diff] [blame] | 291 | int value = 0; | 
|  | 292 | if ((*attr & MUTEXATTR_SHARED_MASK) != 0) { | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 293 | value |= MUTEX_SHARED_MASK; | 
| Elliott Hughes | dff7203 | 2013-12-11 14:54:00 -0800 | [diff] [blame] | 294 | } | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 295 |  | 
|  | 296 | switch (*attr & MUTEXATTR_TYPE_MASK) { | 
|  | 297 | case PTHREAD_MUTEX_NORMAL: | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 298 | value |= MUTEX_TYPE_BITS_NORMAL; | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 299 | break; | 
|  | 300 | case PTHREAD_MUTEX_RECURSIVE: | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 301 | value |= MUTEX_TYPE_BITS_RECURSIVE; | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 302 | break; | 
|  | 303 | case PTHREAD_MUTEX_ERRORCHECK: | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 304 | value |= MUTEX_TYPE_BITS_ERRORCHECK; | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 305 | break; | 
|  | 306 | default: | 
|  | 307 | return EINVAL; | 
|  | 308 | } | 
|  | 309 |  | 
|  | 310 | mutex->value = value; | 
|  | 311 | return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 312 | } | 
|  | 313 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 314 |  | 
|  | 315 | /* | 
|  | 316 | * Lock a non-recursive mutex. | 
|  | 317 | * | 
|  | 318 | * As noted above, there are three states: | 
|  | 319 | *   0 (unlocked, no contention) | 
|  | 320 | *   1 (locked, no contention) | 
|  | 321 | *   2 (locked, contention) | 
|  | 322 | * | 
|  | 323 | * Non-recursive mutexes don't use the thread-id or counter fields, and the | 
|  | 324 | * "type" value is zero, so the only bits that will be set are the ones in | 
|  | 325 | * the lock state field. | 
|  | 326 | */ | 
|  | 327 | static __inline__ void | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 328 | _normal_lock(pthread_mutex_t*  mutex, int shared) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 329 | { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 330 | /* convenience shortcuts */ | 
|  | 331 | const int unlocked           = shared | MUTEX_STATE_BITS_UNLOCKED; | 
|  | 332 | const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 333 | /* | 
|  | 334 | * 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] | 335 | * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED). | 
|  | 336 | * __bionic_cmpxchg() returns 0 if it made the swap successfully. | 
|  | 337 | * 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] | 338 | */ | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 339 | if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) { | 
|  | 340 | 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] | 341 | /* | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 342 | * 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] | 343 | * requires promoting it to state 2 (CONTENDED). We need to | 
|  | 344 | * 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] | 345 | * | 
| David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 346 | * __bionic_swap() returns the previous value.  We swap 2 in and | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 347 | * see if we got zero back; if so, we have acquired the lock.  If | 
|  | 348 | * not, another thread still holds the lock and we wait again. | 
|  | 349 | * | 
|  | 350 | * The second argument to the __futex_wait() call is compared | 
|  | 351 | * against the current value.  If it doesn't match, __futex_wait() | 
|  | 352 | * returns immediately (otherwise, it sleeps for a time specified | 
|  | 353 | * by the third argument; 0 means sleep forever).  This ensures | 
|  | 354 | * that the mutex is in state 2 when we go to sleep on it, which | 
|  | 355 | * guarantees a wake-up call. | 
|  | 356 | */ | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 357 | while (__bionic_swap(locked_contended, &mutex->value) != unlocked) | 
|  | 358 | __futex_wait_ex(&mutex->value, shared, locked_contended, 0); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 359 | } | 
| Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 360 | ANDROID_MEMBAR_FULL(); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 361 | } | 
|  | 362 |  | 
|  | 363 | /* | 
|  | 364 | * Release a non-recursive mutex.  The caller is responsible for determining | 
|  | 365 | * that we are in fact the owner of this lock. | 
|  | 366 | */ | 
|  | 367 | static __inline__ void | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 368 | _normal_unlock(pthread_mutex_t*  mutex, int shared) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 369 | { | 
| Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 370 | ANDROID_MEMBAR_FULL(); | 
|  | 371 |  | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 372 | /* | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 373 | * 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] | 374 | * to release the lock.  __bionic_atomic_dec() returns the previous value; | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 375 | * if it wasn't 1 we have to do some additional work. | 
|  | 376 | */ | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 377 | 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] | 378 | /* | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 379 | * Start by releasing the lock.  The decrement changed it from | 
|  | 380 | * "contended lock" to "uncontended lock", which means we still | 
|  | 381 | * hold it, and anybody who tries to sneak in will push it back | 
|  | 382 | * to state 2. | 
|  | 383 | * | 
|  | 384 | * Once we set it to zero the lock is up for grabs.  We follow | 
|  | 385 | * this with a __futex_wake() to ensure that one of the waiting | 
|  | 386 | * threads has a chance to grab it. | 
|  | 387 | * | 
|  | 388 | * This doesn't cause a race with the swap/wait pair in | 
|  | 389 | * _normal_lock(), because the __futex_wait() call there will | 
|  | 390 | * return immediately if the mutex value isn't 2. | 
|  | 391 | */ | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 392 | mutex->value = shared; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 393 |  | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 394 | /* | 
|  | 395 | * Wake up one waiting thread.  We don't know which thread will be | 
|  | 396 | * woken or when it'll start executing -- futexes make no guarantees | 
|  | 397 | * here.  There may not even be a thread waiting. | 
|  | 398 | * | 
|  | 399 | * The newly-woken thread will replace the 0 we just set above | 
|  | 400 | * with 2, which means that when it eventually releases the mutex | 
|  | 401 | * it will also call FUTEX_WAKE.  This results in one extra wake | 
|  | 402 | * call whenever a lock is contended, but lets us avoid forgetting | 
|  | 403 | * anyone without requiring us to track the number of sleepers. | 
|  | 404 | * | 
|  | 405 | * It's possible for another thread to sneak in and grab the lock | 
|  | 406 | * between the zero assignment above and the wake call below.  If | 
|  | 407 | * the new thread is "slow" and holds the lock for a while, we'll | 
|  | 408 | * wake up a sleeper, which will swap in a 2 and then go back to | 
|  | 409 | * sleep since the lock is still held.  If the new thread is "fast", | 
|  | 410 | * running to completion before we call wake, the thread we | 
|  | 411 | * eventually wake will find an unlocked mutex and will execute. | 
|  | 412 | * Either way we have correct behavior and nobody is orphaned on | 
|  | 413 | * the wait queue. | 
|  | 414 | */ | 
| David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 415 | __futex_wake_ex(&mutex->value, shared, 1); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 416 | } | 
|  | 417 | } | 
|  | 418 |  | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 419 | /* This common inlined function is used to increment the counter of an | 
|  | 420 | * errorcheck or recursive mutex. | 
|  | 421 | * | 
|  | 422 | * For errorcheck mutexes, it will return EDEADLK | 
|  | 423 | * If the counter overflows, it will return EAGAIN | 
|  | 424 | * Otherwise, it atomically increments the counter and returns 0 | 
|  | 425 | * after providing an acquire barrier. | 
|  | 426 | * | 
|  | 427 | * mtype is the current mutex type | 
|  | 428 | * mvalue is the current mutex value (already loaded) | 
|  | 429 | * mutex pointers to the mutex. | 
|  | 430 | */ | 
|  | 431 | static __inline__ __attribute__((always_inline)) int | 
|  | 432 | _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] | 433 | { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 434 | if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) { | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 435 | /* trying to re-lock a mutex we already acquired */ | 
|  | 436 | return EDEADLK; | 
|  | 437 | } | 
|  | 438 |  | 
|  | 439 | /* Detect recursive lock overflow and return EAGAIN. | 
|  | 440 | * This is safe because only the owner thread can modify the | 
| David 'Digit' Turner | b57db75 | 2012-01-24 13:20:38 +0100 | [diff] [blame] | 441 | * counter bits in the mutex value. | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 442 | */ | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 443 | if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) { | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 444 | return EAGAIN; | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | /* We own the mutex, but other threads are able to change | 
| David 'Digit' Turner | b57db75 | 2012-01-24 13:20:38 +0100 | [diff] [blame] | 448 | * the lower bits (e.g. promoting it to "contended"), so we | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 449 | * need to use an atomic cmpxchg loop to update the counter. | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 450 | */ | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 451 | for (;;) { | 
|  | 452 | /* increment counter, overflow was already checked */ | 
|  | 453 | int newval = mvalue + MUTEX_COUNTER_BITS_ONE; | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 454 | if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 455 | /* mutex is still locked, not need for a memory barrier */ | 
|  | 456 | return 0; | 
|  | 457 | } | 
|  | 458 | /* the value was changed, this happens when another thread changes | 
|  | 459 | * the lower state bits from 1 to 2 to indicate contention. This | 
|  | 460 | * cannot change the counter, so simply reload and try again. | 
|  | 461 | */ | 
|  | 462 | mvalue = mutex->value; | 
|  | 463 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 464 | } | 
|  | 465 |  | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 466 | __LIBC_HIDDEN__ | 
|  | 467 | int pthread_mutex_lock_impl(pthread_mutex_t *mutex) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 468 | { | 
| Wink Saville | a12c544 | 2013-01-08 15:15:45 -0800 | [diff] [blame] | 469 | int mvalue, mtype, tid, shared; | 
| David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 470 |  | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 471 | mvalue = mutex->value; | 
|  | 472 | mtype = (mvalue & MUTEX_TYPE_MASK); | 
|  | 473 | shared = (mvalue & MUTEX_SHARED_MASK); | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 474 |  | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 475 | /* Handle normal case first */ | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 476 | if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) { | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 477 | _normal_lock(mutex, shared); | 
| David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 478 | return 0; | 
|  | 479 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 480 |  | 
|  | 481 | /* Do we already own this recursive or error-check mutex ? */ | 
| Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 482 | tid = __get_thread()->tid; | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 483 | if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) ) | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 484 | return _recursive_increment(mutex, mvalue, mtype); | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 485 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 486 | /* Add in shared state to avoid extra 'or' operations below */ | 
| David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 487 | mtype |= shared; | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 488 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 489 | /* First, if the mutex is unlocked, try to quickly acquire it. | 
|  | 490 | * In the optimistic case where this works, set the state to 1 to | 
|  | 491 | * indicate locked with no contention */ | 
|  | 492 | if (mvalue == mtype) { | 
|  | 493 | int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; | 
|  | 494 | if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) { | 
|  | 495 | ANDROID_MEMBAR_FULL(); | 
|  | 496 | return 0; | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 497 | } | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 498 | /* argh, the value changed, reload before entering the loop */ | 
|  | 499 | mvalue = mutex->value; | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 500 | } | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 501 |  | 
|  | 502 | for (;;) { | 
|  | 503 | int newval; | 
|  | 504 |  | 
|  | 505 | /* if the mutex is unlocked, its value should be 'mtype' and | 
|  | 506 | * we try to acquire it by setting its owner and state atomically. | 
|  | 507 | * NOTE: We put the state to 2 since we _know_ there is contention | 
|  | 508 | * when we are in this loop. This ensures all waiters will be | 
|  | 509 | * unlocked. | 
|  | 510 | */ | 
|  | 511 | if (mvalue == mtype) { | 
|  | 512 | newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED; | 
|  | 513 | /* TODO: Change this to __bionic_cmpxchg_acquire when we | 
|  | 514 | *        implement it to get rid of the explicit memory | 
|  | 515 | *        barrier below. | 
|  | 516 | */ | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 517 | if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 518 | mvalue = mutex->value; | 
|  | 519 | continue; | 
|  | 520 | } | 
|  | 521 | ANDROID_MEMBAR_FULL(); | 
|  | 522 | return 0; | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | /* the mutex is already locked by another thread, if its state is 1 | 
|  | 526 | * we will change it to 2 to indicate contention. */ | 
|  | 527 | if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) { | 
|  | 528 | newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */ | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 529 | if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 530 | mvalue = mutex->value; | 
|  | 531 | continue; | 
|  | 532 | } | 
|  | 533 | mvalue = newval; | 
|  | 534 | } | 
|  | 535 |  | 
|  | 536 | /* wait until the mutex is unlocked */ | 
|  | 537 | __futex_wait_ex(&mutex->value, shared, mvalue, NULL); | 
|  | 538 |  | 
|  | 539 | mvalue = mutex->value; | 
|  | 540 | } | 
|  | 541 | /* NOTREACHED */ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 542 | } | 
|  | 543 |  | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 544 | int pthread_mutex_lock(pthread_mutex_t *mutex) | 
|  | 545 | { | 
|  | 546 | int err = pthread_mutex_lock_impl(mutex); | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 547 | if (PTHREAD_DEBUG_ENABLED) { | 
|  | 548 | if (!err) { | 
|  | 549 | pthread_debug_mutex_lock_check(mutex); | 
|  | 550 | } | 
|  | 551 | } | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 552 | return err; | 
|  | 553 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 554 |  | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 555 | __LIBC_HIDDEN__ | 
|  | 556 | int pthread_mutex_unlock_impl(pthread_mutex_t *mutex) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 557 | { | 
| Wink Saville | a12c544 | 2013-01-08 15:15:45 -0800 | [diff] [blame] | 558 | int mvalue, mtype, tid, shared; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 559 |  | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 560 | mvalue = mutex->value; | 
|  | 561 | mtype  = (mvalue & MUTEX_TYPE_MASK); | 
|  | 562 | shared = (mvalue & MUTEX_SHARED_MASK); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 563 |  | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 564 | /* Handle common case first */ | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 565 | if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) { | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 566 | _normal_unlock(mutex, shared); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 567 | return 0; | 
|  | 568 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 569 |  | 
|  | 570 | /* Do we already own this recursive or error-check mutex ? */ | 
| Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 571 | tid = __get_thread()->tid; | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 572 | if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) ) | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 573 | return EPERM; | 
|  | 574 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 575 | /* If the counter is > 0, we can simply decrement it atomically. | 
|  | 576 | * Since other threads can mutate the lower state bits (and only the | 
|  | 577 | * lower state bits), use a cmpxchg to do it. | 
|  | 578 | */ | 
|  | 579 | if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) { | 
|  | 580 | for (;;) { | 
|  | 581 | int newval = mvalue - MUTEX_COUNTER_BITS_ONE; | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 582 | if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 583 | /* success: we still own the mutex, so no memory barrier */ | 
|  | 584 | return 0; | 
|  | 585 | } | 
|  | 586 | /* the value changed, so reload and loop */ | 
|  | 587 | mvalue = mutex->value; | 
|  | 588 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 589 | } | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 590 |  | 
|  | 591 | /* the counter is 0, so we're going to unlock the mutex by resetting | 
|  | 592 | * its value to 'unlocked'. We need to perform a swap in order | 
|  | 593 | * to read the current state, which will be 2 if there are waiters | 
|  | 594 | * to awake. | 
|  | 595 | * | 
|  | 596 | * TODO: Change this to __bionic_swap_release when we implement it | 
|  | 597 | *        to get rid of the explicit memory barrier below. | 
|  | 598 | */ | 
|  | 599 | ANDROID_MEMBAR_FULL();  /* RELEASE BARRIER */ | 
|  | 600 | mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value); | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 601 |  | 
|  | 602 | /* Wake one waiting thread, if any */ | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 603 | if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) { | 
| David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 604 | __futex_wake_ex(&mutex->value, shared, 1); | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 605 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 606 | return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 607 | } | 
|  | 608 |  | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 609 | int pthread_mutex_unlock(pthread_mutex_t *mutex) | 
|  | 610 | { | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 611 | if (PTHREAD_DEBUG_ENABLED) { | 
|  | 612 | pthread_debug_mutex_unlock_check(mutex); | 
|  | 613 | } | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 614 | return pthread_mutex_unlock_impl(mutex); | 
|  | 615 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 616 |  | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 617 | __LIBC_HIDDEN__ | 
|  | 618 | int pthread_mutex_trylock_impl(pthread_mutex_t *mutex) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 619 | { | 
| Wink Saville | a12c544 | 2013-01-08 15:15:45 -0800 | [diff] [blame] | 620 | int mvalue, mtype, tid, shared; | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 621 |  | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 622 | mvalue = mutex->value; | 
|  | 623 | mtype  = (mvalue & MUTEX_TYPE_MASK); | 
|  | 624 | shared = (mvalue & MUTEX_SHARED_MASK); | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 625 |  | 
|  | 626 | /* Handle common case first */ | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 627 | if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 628 | { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 629 | if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED, | 
|  | 630 | shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED, | 
|  | 631 | &mutex->value) == 0) { | 
| Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 632 | ANDROID_MEMBAR_FULL(); | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 633 | return 0; | 
| Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 634 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 635 |  | 
|  | 636 | return EBUSY; | 
| David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 637 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 638 |  | 
|  | 639 | /* Do we already own this recursive or error-check mutex ? */ | 
| Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 640 | tid = __get_thread()->tid; | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 641 | if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) ) | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 642 | return _recursive_increment(mutex, mvalue, mtype); | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 643 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 644 | /* Same as pthread_mutex_lock, except that we don't want to wait, and | 
|  | 645 | * the only operation that can succeed is a single cmpxchg to acquire the | 
|  | 646 | * lock if it is released / not owned by anyone. No need for a complex loop. | 
|  | 647 | */ | 
|  | 648 | mtype |= shared | MUTEX_STATE_BITS_UNLOCKED; | 
|  | 649 | 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] | 650 |  | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 651 | if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 652 | ANDROID_MEMBAR_FULL(); | 
|  | 653 | return 0; | 
|  | 654 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 655 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 656 | return EBUSY; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 657 | } | 
|  | 658 |  | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 659 | int pthread_mutex_trylock(pthread_mutex_t *mutex) | 
|  | 660 | { | 
|  | 661 | int err = pthread_mutex_trylock_impl(mutex); | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 662 | if (PTHREAD_DEBUG_ENABLED) { | 
|  | 663 | if (!err) { | 
|  | 664 | pthread_debug_mutex_lock_check(mutex); | 
|  | 665 | } | 
|  | 666 | } | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 667 | return err; | 
|  | 668 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 669 |  | 
| David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 670 | /* initialize 'abstime' to the current time according to 'clock' plus 'msecs' | 
|  | 671 | * milliseconds. | 
|  | 672 | */ | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 673 | 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] | 674 | clock_gettime(clock, abstime); | 
|  | 675 | abstime->tv_sec  += msecs/1000; | 
|  | 676 | abstime->tv_nsec += (msecs%1000)*1000000; | 
|  | 677 | if (abstime->tv_nsec >= 1000000000) { | 
|  | 678 | abstime->tv_sec++; | 
|  | 679 | abstime->tv_nsec -= 1000000000; | 
|  | 680 | } | 
|  | 681 | } | 
|  | 682 |  | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 683 | __LIBC_HIDDEN__ | 
|  | 684 | 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] | 685 | { | 
|  | 686 | clockid_t        clock = CLOCK_MONOTONIC; | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 687 | timespec  abstime; | 
|  | 688 | timespec  ts; | 
| Wink Saville | a12c544 | 2013-01-08 15:15:45 -0800 | [diff] [blame] | 689 | int               mvalue, mtype, tid, shared; | 
| David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 690 |  | 
|  | 691 | /* compute absolute expiration time */ | 
|  | 692 | __timespec_to_relative_msec(&abstime, msecs, clock); | 
|  | 693 |  | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 694 | mvalue = mutex->value; | 
|  | 695 | mtype  = (mvalue & MUTEX_TYPE_MASK); | 
|  | 696 | shared = (mvalue & MUTEX_SHARED_MASK); | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 697 |  | 
|  | 698 | /* Handle common case first */ | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 699 | if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) | 
| David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 700 | { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 701 | const int unlocked           = shared | MUTEX_STATE_BITS_UNLOCKED; | 
|  | 702 | const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; | 
|  | 703 | const int locked_contended   = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; | 
|  | 704 |  | 
|  | 705 | /* fast path for uncontended lock. Note: MUTEX_TYPE_BITS_NORMAL is 0 */ | 
|  | 706 | if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) == 0) { | 
| Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 707 | ANDROID_MEMBAR_FULL(); | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 708 | return 0; | 
| Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 709 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 710 |  | 
|  | 711 | /* loop while needed */ | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 712 | while (__bionic_swap(locked_contended, &mutex->value) != unlocked) { | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 713 | if (__timespec_to_absolute(&ts, &abstime, clock) < 0) | 
|  | 714 | return EBUSY; | 
|  | 715 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 716 | __futex_wait_ex(&mutex->value, shared, locked_contended, &ts); | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 717 | } | 
| Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 718 | ANDROID_MEMBAR_FULL(); | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 719 | return 0; | 
| David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 720 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 721 |  | 
|  | 722 | /* Do we already own this recursive or error-check mutex ? */ | 
| Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 723 | tid = __get_thread()->tid; | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 724 | if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) ) | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 725 | return _recursive_increment(mutex, mvalue, mtype); | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 726 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 727 | /* the following implements the same loop than pthread_mutex_lock_impl | 
|  | 728 | * but adds checks to ensure that the operation never exceeds the | 
|  | 729 | * absolute expiration time. | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 730 | */ | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 731 | mtype |= shared; | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 732 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 733 | /* first try a quick lock */ | 
|  | 734 | if (mvalue == mtype) { | 
|  | 735 | mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 736 | if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 737 | ANDROID_MEMBAR_FULL(); | 
|  | 738 | return 0; | 
|  | 739 | } | 
|  | 740 | mvalue = mutex->value; | 
|  | 741 | } | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 742 |  | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 743 | for (;;) { | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 744 | timespec ts; | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 745 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 746 | /* if the value is 'unlocked', try to acquire it directly */ | 
|  | 747 | /* NOTE: put state to 2 since we know there is contention */ | 
|  | 748 | if (mvalue == mtype) /* unlocked */ { | 
|  | 749 | mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED; | 
|  | 750 | if (__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0) { | 
|  | 751 | ANDROID_MEMBAR_FULL(); | 
|  | 752 | return 0; | 
|  | 753 | } | 
|  | 754 | /* the value changed before we could lock it. We need to check | 
|  | 755 | * the time to avoid livelocks, reload the value, then loop again. */ | 
|  | 756 | if (__timespec_to_absolute(&ts, &abstime, clock) < 0) | 
|  | 757 | return EBUSY; | 
|  | 758 |  | 
|  | 759 | mvalue = mutex->value; | 
|  | 760 | continue; | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 761 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 762 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 763 | /* The value is locked. If 'uncontended', try to switch its state | 
|  | 764 | * to 'contented' to ensure we get woken up later. */ | 
|  | 765 | if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) { | 
|  | 766 | int newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); | 
|  | 767 | if (__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0) { | 
|  | 768 | /* this failed because the value changed, reload it */ | 
|  | 769 | mvalue = mutex->value; | 
|  | 770 | } else { | 
|  | 771 | /* this succeeded, update mvalue */ | 
|  | 772 | mvalue = newval; | 
|  | 773 | } | 
|  | 774 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 775 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 776 | /* check time and update 'ts' */ | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 777 | if (__timespec_to_absolute(&ts, &abstime, clock) < 0) | 
|  | 778 | return EBUSY; | 
|  | 779 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 780 | /* Only wait to be woken up if the state is '2', otherwise we'll | 
|  | 781 | * simply loop right now. This can happen when the second cmpxchg | 
|  | 782 | * in our loop failed because the mutex was unlocked by another | 
|  | 783 | * thread. | 
|  | 784 | */ | 
|  | 785 | if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) { | 
| Elliott Hughes | 9e79af3 | 2013-12-18 10:05:42 -0800 | [diff] [blame] | 786 | if (__futex_wait_ex(&mutex->value, shared, mvalue, &ts) == -ETIMEDOUT) { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 787 | return EBUSY; | 
|  | 788 | } | 
|  | 789 | mvalue = mutex->value; | 
|  | 790 | } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 791 | } | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 792 | /* NOTREACHED */ | 
| David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 793 | } | 
|  | 794 |  | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 795 | int pthread_mutex_lock_timeout_np(pthread_mutex_t *mutex, unsigned msecs) | 
|  | 796 | { | 
|  | 797 | int err = pthread_mutex_lock_timeout_np_impl(mutex, msecs); | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 798 | if (PTHREAD_DEBUG_ENABLED) { | 
|  | 799 | if (!err) { | 
|  | 800 | pthread_debug_mutex_lock_check(mutex); | 
|  | 801 | } | 
|  | 802 | } | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 803 | return err; | 
|  | 804 | } | 
|  | 805 |  | 
|  | 806 | int pthread_mutex_destroy(pthread_mutex_t *mutex) | 
|  | 807 | { | 
|  | 808 | int ret; | 
|  | 809 |  | 
|  | 810 | /* use trylock to ensure that the mutex value is | 
|  | 811 | * valid and is not already locked. */ | 
|  | 812 | ret = pthread_mutex_trylock_impl(mutex); | 
|  | 813 | if (ret != 0) | 
|  | 814 | return ret; | 
|  | 815 |  | 
|  | 816 | mutex->value = 0xdead10cc; | 
|  | 817 | return 0; | 
|  | 818 | } |