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 | |
Serban Constantinescu | 7f70c9b | 2013-10-13 22:07:26 +0100 | [diff] [blame] | 48 | extern void _exit_with_stack_teardown(void * stackBase, size_t stackSize, int status); |
Elliott Hughes | c4c6e19 | 2013-10-08 14:48:05 -0700 | [diff] [blame] | 49 | extern void _exit_thread(int status); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 50 | |
David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 51 | int __futex_wake_ex(volatile void *ftx, int pshared, int val) |
| 52 | { |
| 53 | return __futex_syscall3(ftx, pshared ? FUTEX_WAKE : FUTEX_WAKE_PRIVATE, val); |
| 54 | } |
| 55 | |
| 56 | int __futex_wait_ex(volatile void *ftx, int pshared, int val, const struct timespec *timeout) |
| 57 | { |
| 58 | return __futex_syscall4(ftx, pshared ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE, val, timeout); |
| 59 | } |
| 60 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 61 | /* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions |
| 62 | * and thread cancelation |
| 63 | */ |
| 64 | |
| 65 | void __pthread_cleanup_push( __pthread_cleanup_t* c, |
| 66 | __pthread_cleanup_func_t routine, |
| 67 | void* arg ) |
| 68 | { |
| 69 | pthread_internal_t* thread = __get_thread(); |
| 70 | |
| 71 | c->__cleanup_routine = routine; |
| 72 | c->__cleanup_arg = arg; |
| 73 | c->__cleanup_prev = thread->cleanup_stack; |
| 74 | thread->cleanup_stack = c; |
| 75 | } |
| 76 | |
| 77 | void __pthread_cleanup_pop( __pthread_cleanup_t* c, int execute ) |
| 78 | { |
| 79 | pthread_internal_t* thread = __get_thread(); |
| 80 | |
| 81 | thread->cleanup_stack = c->__cleanup_prev; |
| 82 | if (execute) |
| 83 | c->__cleanup_routine(c->__cleanup_arg); |
| 84 | } |
| 85 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 86 | void pthread_exit(void * retval) |
| 87 | { |
| 88 | pthread_internal_t* thread = __get_thread(); |
| 89 | void* stack_base = thread->attr.stack_base; |
Serban Constantinescu | 7f70c9b | 2013-10-13 22:07:26 +0100 | [diff] [blame] | 90 | size_t stack_size = thread->attr.stack_size; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 91 | int user_stack = (thread->attr.flags & PTHREAD_ATTR_FLAG_USER_STACK) != 0; |
Jack Ren | e480fc8 | 2011-09-21 12:44:11 +0200 | [diff] [blame] | 92 | sigset_t mask; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 93 | |
| 94 | // call the cleanup handlers first |
| 95 | while (thread->cleanup_stack) { |
| 96 | __pthread_cleanup_t* c = thread->cleanup_stack; |
| 97 | thread->cleanup_stack = c->__cleanup_prev; |
| 98 | c->__cleanup_routine(c->__cleanup_arg); |
| 99 | } |
| 100 | |
| 101 | // call the TLS destructors, it is important to do that before removing this |
| 102 | // thread from the global list. this will ensure that if someone else deletes |
| 103 | // a TLS key, the corresponding value will be set to NULL in this thread's TLS |
| 104 | // space (see pthread_key_delete) |
| 105 | pthread_key_clean_all(); |
| 106 | |
Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 107 | if (thread->alternate_signal_stack != NULL) { |
| 108 | // Tell the kernel to stop using the alternate signal stack. |
| 109 | stack_t ss; |
| 110 | ss.ss_sp = NULL; |
| 111 | ss.ss_flags = SS_DISABLE; |
| 112 | sigaltstack(&ss, NULL); |
| 113 | |
| 114 | // Free it. |
| 115 | munmap(thread->alternate_signal_stack, SIGSTKSZ); |
| 116 | thread->alternate_signal_stack = NULL; |
| 117 | } |
| 118 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 119 | // if the thread is detached, destroy the pthread_internal_t |
Sergey Melnikov | 10ce969 | 2012-10-26 14:06:43 +0400 | [diff] [blame] | 120 | // otherwise, keep it in memory and signal any joiners. |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 121 | pthread_mutex_lock(&gThreadListLock); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 122 | if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 123 | _pthread_internal_remove_locked(thread); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 124 | } else { |
Bjorn Andersson | 0753dc6 | 2012-05-03 17:12:39 -0700 | [diff] [blame] | 125 | /* make sure that the thread struct doesn't have stale pointers to a stack that |
| 126 | * will be unmapped after the exit call below. |
| 127 | */ |
| 128 | if (!user_stack) { |
| 129 | thread->attr.stack_base = NULL; |
| 130 | thread->attr.stack_size = 0; |
| 131 | thread->tls = NULL; |
| 132 | } |
| 133 | |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 134 | /* Indicate that the thread has exited for joining threads. */ |
| 135 | thread->attr.flags |= PTHREAD_ATTR_FLAG_ZOMBIE; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 136 | thread->return_value = retval; |
msg555 | 0f020d1 | 2013-06-06 14:59:28 -0400 | [diff] [blame] | 137 | |
| 138 | /* Signal the joining thread if present. */ |
| 139 | if (thread->attr.flags & PTHREAD_ATTR_FLAG_JOINED) { |
| 140 | pthread_cond_signal(&thread->join_cond); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 141 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 142 | } |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 143 | pthread_mutex_unlock(&gThreadListLock); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 144 | |
Jack Ren | e480fc8 | 2011-09-21 12:44:11 +0200 | [diff] [blame] | 145 | sigfillset(&mask); |
| 146 | sigdelset(&mask, SIGSEGV); |
Elliott Hughes | c4c6e19 | 2013-10-08 14:48:05 -0700 | [diff] [blame] | 147 | sigprocmask(SIG_SETMASK, &mask, NULL); |
Jack Ren | e480fc8 | 2011-09-21 12:44:11 +0200 | [diff] [blame] | 148 | |
Elliott Hughes | a97cc5b | 2013-10-07 10:20:24 -0700 | [diff] [blame] | 149 | if (user_stack) { |
Elliott Hughes | c4c6e19 | 2013-10-08 14:48:05 -0700 | [diff] [blame] | 150 | // Cleaning up this thread's stack is the creator's responsibility, not ours. |
| 151 | _exit_thread(0); |
Elliott Hughes | a97cc5b | 2013-10-07 10:20:24 -0700 | [diff] [blame] | 152 | } else { |
| 153 | // We need to munmap the stack we're running on before calling exit. |
| 154 | // That's not something we can do in C. |
Elliott Hughes | c4c6e19 | 2013-10-08 14:48:05 -0700 | [diff] [blame] | 155 | _exit_with_stack_teardown(stack_base, stack_size, 0); |
Elliott Hughes | a97cc5b | 2013-10-07 10:20:24 -0700 | [diff] [blame] | 156 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 157 | } |
| 158 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 159 | /* a mutex is implemented as a 32-bit integer holding the following fields |
| 160 | * |
| 161 | * bits: name description |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 162 | * 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] | 163 | * 15-14 type mutex type |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 164 | * 13 shared process-shared flag |
| 165 | * 12-2 counter counter of recursive mutexes |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 166 | * 1-0 state lock state (0, 1 or 2) |
| 167 | */ |
| 168 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 169 | /* Convenience macro, creates a mask of 'bits' bits that starts from |
| 170 | * the 'shift'-th least significant bit in a 32-bit word. |
| 171 | * |
| 172 | * Examples: FIELD_MASK(0,4) -> 0xf |
| 173 | * FIELD_MASK(16,9) -> 0x1ff0000 |
| 174 | */ |
| 175 | #define FIELD_MASK(shift,bits) (((1 << (bits))-1) << (shift)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 176 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 177 | /* This one is used to create a bit pattern from a given field value */ |
| 178 | #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] | 179 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 180 | /* And this one does the opposite, i.e. extract a field's value from a bit pattern */ |
| 181 | #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] | 182 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 183 | /* Mutex state: |
| 184 | * |
| 185 | * 0 for unlocked |
| 186 | * 1 for locked, no waiters |
| 187 | * 2 for locked, maybe waiters |
| 188 | */ |
| 189 | #define MUTEX_STATE_SHIFT 0 |
| 190 | #define MUTEX_STATE_LEN 2 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 191 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 192 | #define MUTEX_STATE_MASK FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) |
| 193 | #define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) |
| 194 | #define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) |
| 195 | |
| 196 | #define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */ |
| 197 | #define MUTEX_STATE_LOCKED_UNCONTENDED 1 /* must be 1 due to atomic dec in unlock operation */ |
| 198 | #define MUTEX_STATE_LOCKED_CONTENDED 2 /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */ |
| 199 | |
| 200 | #define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) |
| 201 | #define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) |
| 202 | |
| 203 | #define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED) |
| 204 | #define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED) |
| 205 | #define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED) |
| 206 | |
| 207 | /* return true iff the mutex if locked with no waiters */ |
| 208 | #define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED) |
| 209 | |
| 210 | /* return true iff the mutex if locked with maybe waiters */ |
| 211 | #define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED) |
| 212 | |
| 213 | /* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */ |
| 214 | #define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) |
| 215 | |
| 216 | /* Mutex counter: |
| 217 | * |
| 218 | * We need to check for overflow before incrementing, and we also need to |
| 219 | * detect when the counter is 0 |
| 220 | */ |
| 221 | #define MUTEX_COUNTER_SHIFT 2 |
| 222 | #define MUTEX_COUNTER_LEN 11 |
| 223 | #define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN) |
| 224 | |
| 225 | #define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK) |
| 226 | #define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0) |
| 227 | |
| 228 | /* Used to increment the counter directly after overflow has been checked */ |
| 229 | #define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1,MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN) |
| 230 | |
| 231 | /* Returns true iff the counter is 0 */ |
| 232 | #define MUTEX_COUNTER_BITS_ARE_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0) |
| 233 | |
| 234 | /* Mutex shared bit flag |
| 235 | * |
| 236 | * This flag is set to indicate that the mutex is shared among processes. |
| 237 | * This changes the futex opcode we use for futex wait/wake operations |
| 238 | * (non-shared operations are much faster). |
| 239 | */ |
| 240 | #define MUTEX_SHARED_SHIFT 13 |
| 241 | #define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1) |
| 242 | |
| 243 | /* Mutex type: |
| 244 | * |
| 245 | * We support normal, recursive and errorcheck mutexes. |
| 246 | * |
| 247 | * The constants defined here *cannot* be changed because they must match |
| 248 | * the C library ABI which defines the following initialization values in |
| 249 | * <pthread.h>: |
| 250 | * |
| 251 | * __PTHREAD_MUTEX_INIT_VALUE |
| 252 | * __PTHREAD_RECURSIVE_MUTEX_VALUE |
| 253 | * __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE |
| 254 | */ |
| 255 | #define MUTEX_TYPE_SHIFT 14 |
| 256 | #define MUTEX_TYPE_LEN 2 |
| 257 | #define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN) |
| 258 | |
| 259 | #define MUTEX_TYPE_NORMAL 0 /* Must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */ |
| 260 | #define MUTEX_TYPE_RECURSIVE 1 |
| 261 | #define MUTEX_TYPE_ERRORCHECK 2 |
| 262 | |
| 263 | #define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN) |
| 264 | |
| 265 | #define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(MUTEX_TYPE_NORMAL) |
| 266 | #define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(MUTEX_TYPE_RECURSIVE) |
| 267 | #define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(MUTEX_TYPE_ERRORCHECK) |
| 268 | |
| 269 | /* Mutex owner field: |
| 270 | * |
| 271 | * This is only used for recursive and errorcheck mutexes. It holds the |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 272 | * tid of the owning thread. Note that this works because the Linux |
| 273 | * kernel _only_ uses 16-bit values for tids. |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 274 | * |
| 275 | * More specifically, it will wrap to 10000 when it reaches over 32768 for |
| 276 | * application processes. You can check this by running the following inside |
| 277 | * an adb shell session: |
| 278 | * |
| 279 | OLDPID=$$; |
| 280 | while true; do |
| 281 | NEWPID=$(sh -c 'echo $$') |
| 282 | if [ "$NEWPID" -gt 32768 ]; then |
| 283 | echo "AARGH: new PID $NEWPID is too high!" |
| 284 | exit 1 |
| 285 | fi |
| 286 | if [ "$NEWPID" -lt "$OLDPID" ]; then |
| 287 | echo "****** Wrapping from PID $OLDPID to $NEWPID. *******" |
| 288 | else |
| 289 | echo -n "$NEWPID!" |
| 290 | fi |
| 291 | OLDPID=$NEWPID |
| 292 | done |
| 293 | |
| 294 | * Note that you can run the same example on a desktop Linux system, |
| 295 | * the wrapping will also happen at 32768, but will go back to 300 instead. |
| 296 | */ |
| 297 | #define MUTEX_OWNER_SHIFT 16 |
| 298 | #define MUTEX_OWNER_LEN 16 |
| 299 | |
| 300 | #define MUTEX_OWNER_FROM_BITS(v) FIELD_FROM_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN) |
| 301 | #define MUTEX_OWNER_TO_BITS(v) FIELD_TO_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN) |
| 302 | |
| 303 | /* Convenience macros. |
| 304 | * |
| 305 | * These are used to form or modify the bit pattern of a given mutex value |
| 306 | */ |
| 307 | |
| 308 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 309 | |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 310 | /* a mutex attribute holds the following fields |
| 311 | * |
| 312 | * bits: name description |
| 313 | * 0-3 type type of mutex |
| 314 | * 4 shared process-shared flag |
| 315 | */ |
| 316 | #define MUTEXATTR_TYPE_MASK 0x000f |
| 317 | #define MUTEXATTR_SHARED_MASK 0x0010 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 318 | |
| 319 | |
| 320 | int pthread_mutexattr_init(pthread_mutexattr_t *attr) |
| 321 | { |
| 322 | if (attr) { |
| 323 | *attr = PTHREAD_MUTEX_DEFAULT; |
| 324 | return 0; |
| 325 | } else { |
| 326 | return EINVAL; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) |
| 331 | { |
| 332 | if (attr) { |
| 333 | *attr = -1; |
| 334 | return 0; |
| 335 | } else { |
| 336 | return EINVAL; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type) |
| 341 | { |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 342 | if (attr) { |
| 343 | int atype = (*attr & MUTEXATTR_TYPE_MASK); |
| 344 | |
| 345 | if (atype >= PTHREAD_MUTEX_NORMAL && |
| 346 | atype <= PTHREAD_MUTEX_ERRORCHECK) { |
| 347 | *type = atype; |
| 348 | return 0; |
| 349 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 350 | } |
| 351 | return EINVAL; |
| 352 | } |
| 353 | |
| 354 | int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) |
| 355 | { |
| 356 | if (attr && type >= PTHREAD_MUTEX_NORMAL && |
| 357 | type <= PTHREAD_MUTEX_ERRORCHECK ) { |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 358 | *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | return EINVAL; |
| 362 | } |
| 363 | |
| 364 | /* process-shared mutexes are not supported at the moment */ |
| 365 | |
| 366 | int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared) |
| 367 | { |
| 368 | if (!attr) |
| 369 | return EINVAL; |
| 370 | |
Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 371 | switch (pshared) { |
| 372 | case PTHREAD_PROCESS_PRIVATE: |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 373 | *attr &= ~MUTEXATTR_SHARED_MASK; |
| 374 | return 0; |
| 375 | |
Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 376 | case PTHREAD_PROCESS_SHARED: |
| 377 | /* our current implementation of pthread actually supports shared |
| 378 | * mutexes but won't cleanup if a process dies with the mutex held. |
| 379 | * Nevertheless, it's better than nothing. Shared mutexes are used |
| 380 | * by surfaceflinger and audioflinger. |
| 381 | */ |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 382 | *attr |= MUTEXATTR_SHARED_MASK; |
Mathias Agopian | b768116 | 2009-07-13 22:00:33 -0700 | [diff] [blame] | 383 | return 0; |
| 384 | } |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 385 | return EINVAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared) |
| 389 | { |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 390 | if (!attr || !pshared) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 391 | return EINVAL; |
| 392 | |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 393 | *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED |
| 394 | : PTHREAD_PROCESS_PRIVATE; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | int pthread_mutex_init(pthread_mutex_t *mutex, |
| 399 | const pthread_mutexattr_t *attr) |
| 400 | { |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 401 | int value = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 402 | |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 403 | if (mutex == NULL) |
| 404 | return EINVAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 405 | |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 406 | if (__predict_true(attr == NULL)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 407 | mutex->value = MUTEX_TYPE_BITS_NORMAL; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 408 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 409 | } |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 410 | |
| 411 | if ((*attr & MUTEXATTR_SHARED_MASK) != 0) |
| 412 | value |= MUTEX_SHARED_MASK; |
| 413 | |
| 414 | switch (*attr & MUTEXATTR_TYPE_MASK) { |
| 415 | case PTHREAD_MUTEX_NORMAL: |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 416 | value |= MUTEX_TYPE_BITS_NORMAL; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 417 | break; |
| 418 | case PTHREAD_MUTEX_RECURSIVE: |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 419 | value |= MUTEX_TYPE_BITS_RECURSIVE; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 420 | break; |
| 421 | case PTHREAD_MUTEX_ERRORCHECK: |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 422 | value |= MUTEX_TYPE_BITS_ERRORCHECK; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 423 | break; |
| 424 | default: |
| 425 | return EINVAL; |
| 426 | } |
| 427 | |
| 428 | mutex->value = value; |
| 429 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 430 | } |
| 431 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * Lock a non-recursive mutex. |
| 435 | * |
| 436 | * As noted above, there are three states: |
| 437 | * 0 (unlocked, no contention) |
| 438 | * 1 (locked, no contention) |
| 439 | * 2 (locked, contention) |
| 440 | * |
| 441 | * Non-recursive mutexes don't use the thread-id or counter fields, and the |
| 442 | * "type" value is zero, so the only bits that will be set are the ones in |
| 443 | * the lock state field. |
| 444 | */ |
| 445 | static __inline__ void |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 446 | _normal_lock(pthread_mutex_t* mutex, int shared) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 447 | { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 448 | /* convenience shortcuts */ |
| 449 | const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED; |
| 450 | const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 451 | /* |
| 452 | * 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] | 453 | * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED). |
| 454 | * __bionic_cmpxchg() returns 0 if it made the swap successfully. |
| 455 | * 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] | 456 | */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 457 | if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) { |
| 458 | 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] | 459 | /* |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 460 | * 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] | 461 | * requires promoting it to state 2 (CONTENDED). We need to |
| 462 | * 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] | 463 | * |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 464 | * __bionic_swap() returns the previous value. We swap 2 in and |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 465 | * see if we got zero back; if so, we have acquired the lock. If |
| 466 | * not, another thread still holds the lock and we wait again. |
| 467 | * |
| 468 | * The second argument to the __futex_wait() call is compared |
| 469 | * against the current value. If it doesn't match, __futex_wait() |
| 470 | * returns immediately (otherwise, it sleeps for a time specified |
| 471 | * by the third argument; 0 means sleep forever). This ensures |
| 472 | * that the mutex is in state 2 when we go to sleep on it, which |
| 473 | * guarantees a wake-up call. |
| 474 | */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 475 | while (__bionic_swap(locked_contended, &mutex->value) != unlocked) |
| 476 | __futex_wait_ex(&mutex->value, shared, locked_contended, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 477 | } |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 478 | ANDROID_MEMBAR_FULL(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Release a non-recursive mutex. The caller is responsible for determining |
| 483 | * that we are in fact the owner of this lock. |
| 484 | */ |
| 485 | static __inline__ void |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 486 | _normal_unlock(pthread_mutex_t* mutex, int shared) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 487 | { |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 488 | ANDROID_MEMBAR_FULL(); |
| 489 | |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 490 | /* |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 491 | * 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] | 492 | * to release the lock. __bionic_atomic_dec() returns the previous value; |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 493 | * if it wasn't 1 we have to do some additional work. |
| 494 | */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 495 | 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] | 496 | /* |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 497 | * Start by releasing the lock. The decrement changed it from |
| 498 | * "contended lock" to "uncontended lock", which means we still |
| 499 | * hold it, and anybody who tries to sneak in will push it back |
| 500 | * to state 2. |
| 501 | * |
| 502 | * Once we set it to zero the lock is up for grabs. We follow |
| 503 | * this with a __futex_wake() to ensure that one of the waiting |
| 504 | * threads has a chance to grab it. |
| 505 | * |
| 506 | * This doesn't cause a race with the swap/wait pair in |
| 507 | * _normal_lock(), because the __futex_wait() call there will |
| 508 | * return immediately if the mutex value isn't 2. |
| 509 | */ |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 510 | mutex->value = shared; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 511 | |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 512 | /* |
| 513 | * Wake up one waiting thread. We don't know which thread will be |
| 514 | * woken or when it'll start executing -- futexes make no guarantees |
| 515 | * here. There may not even be a thread waiting. |
| 516 | * |
| 517 | * The newly-woken thread will replace the 0 we just set above |
| 518 | * with 2, which means that when it eventually releases the mutex |
| 519 | * it will also call FUTEX_WAKE. This results in one extra wake |
| 520 | * call whenever a lock is contended, but lets us avoid forgetting |
| 521 | * anyone without requiring us to track the number of sleepers. |
| 522 | * |
| 523 | * It's possible for another thread to sneak in and grab the lock |
| 524 | * between the zero assignment above and the wake call below. If |
| 525 | * the new thread is "slow" and holds the lock for a while, we'll |
| 526 | * wake up a sleeper, which will swap in a 2 and then go back to |
| 527 | * sleep since the lock is still held. If the new thread is "fast", |
| 528 | * running to completion before we call wake, the thread we |
| 529 | * eventually wake will find an unlocked mutex and will execute. |
| 530 | * Either way we have correct behavior and nobody is orphaned on |
| 531 | * the wait queue. |
| 532 | */ |
David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 533 | __futex_wake_ex(&mutex->value, shared, 1); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 537 | /* This common inlined function is used to increment the counter of an |
| 538 | * errorcheck or recursive mutex. |
| 539 | * |
| 540 | * For errorcheck mutexes, it will return EDEADLK |
| 541 | * If the counter overflows, it will return EAGAIN |
| 542 | * Otherwise, it atomically increments the counter and returns 0 |
| 543 | * after providing an acquire barrier. |
| 544 | * |
| 545 | * mtype is the current mutex type |
| 546 | * mvalue is the current mutex value (already loaded) |
| 547 | * mutex pointers to the mutex. |
| 548 | */ |
| 549 | static __inline__ __attribute__((always_inline)) int |
| 550 | _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] | 551 | { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 552 | if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) { |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 553 | /* trying to re-lock a mutex we already acquired */ |
| 554 | return EDEADLK; |
| 555 | } |
| 556 | |
| 557 | /* Detect recursive lock overflow and return EAGAIN. |
| 558 | * This is safe because only the owner thread can modify the |
David 'Digit' Turner | b57db75 | 2012-01-24 13:20:38 +0100 | [diff] [blame] | 559 | * counter bits in the mutex value. |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 560 | */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 561 | if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) { |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 562 | return EAGAIN; |
| 563 | } |
| 564 | |
| 565 | /* We own the mutex, but other threads are able to change |
David 'Digit' Turner | b57db75 | 2012-01-24 13:20:38 +0100 | [diff] [blame] | 566 | * the lower bits (e.g. promoting it to "contended"), so we |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 567 | * need to use an atomic cmpxchg loop to update the counter. |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 568 | */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 569 | for (;;) { |
| 570 | /* increment counter, overflow was already checked */ |
| 571 | int newval = mvalue + MUTEX_COUNTER_BITS_ONE; |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 572 | if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 573 | /* mutex is still locked, not need for a memory barrier */ |
| 574 | return 0; |
| 575 | } |
| 576 | /* the value was changed, this happens when another thread changes |
| 577 | * the lower state bits from 1 to 2 to indicate contention. This |
| 578 | * cannot change the counter, so simply reload and try again. |
| 579 | */ |
| 580 | mvalue = mutex->value; |
| 581 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 584 | __LIBC_HIDDEN__ |
| 585 | int pthread_mutex_lock_impl(pthread_mutex_t *mutex) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 586 | { |
Wink Saville | a12c544 | 2013-01-08 15:15:45 -0800 | [diff] [blame] | 587 | int mvalue, mtype, tid, shared; |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 588 | |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 589 | if (__predict_false(mutex == NULL)) |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 590 | return EINVAL; |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 591 | |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 592 | mvalue = mutex->value; |
| 593 | mtype = (mvalue & MUTEX_TYPE_MASK); |
| 594 | shared = (mvalue & MUTEX_SHARED_MASK); |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 595 | |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 596 | /* Handle normal case first */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 597 | if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) { |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 598 | _normal_lock(mutex, shared); |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 599 | return 0; |
| 600 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 601 | |
| 602 | /* Do we already own this recursive or error-check mutex ? */ |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 603 | tid = __get_thread()->tid; |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 604 | if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) ) |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 605 | return _recursive_increment(mutex, mvalue, mtype); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 606 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 607 | /* Add in shared state to avoid extra 'or' operations below */ |
David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 608 | mtype |= shared; |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 609 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 610 | /* First, if the mutex is unlocked, try to quickly acquire it. |
| 611 | * In the optimistic case where this works, set the state to 1 to |
| 612 | * indicate locked with no contention */ |
| 613 | if (mvalue == mtype) { |
| 614 | int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
| 615 | if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) { |
| 616 | ANDROID_MEMBAR_FULL(); |
| 617 | return 0; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 618 | } |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 619 | /* argh, the value changed, reload before entering the loop */ |
| 620 | mvalue = mutex->value; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 621 | } |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 622 | |
| 623 | for (;;) { |
| 624 | int newval; |
| 625 | |
| 626 | /* if the mutex is unlocked, its value should be 'mtype' and |
| 627 | * we try to acquire it by setting its owner and state atomically. |
| 628 | * NOTE: We put the state to 2 since we _know_ there is contention |
| 629 | * when we are in this loop. This ensures all waiters will be |
| 630 | * unlocked. |
| 631 | */ |
| 632 | if (mvalue == mtype) { |
| 633 | newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED; |
| 634 | /* TODO: Change this to __bionic_cmpxchg_acquire when we |
| 635 | * implement it to get rid of the explicit memory |
| 636 | * barrier below. |
| 637 | */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 638 | if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 639 | mvalue = mutex->value; |
| 640 | continue; |
| 641 | } |
| 642 | ANDROID_MEMBAR_FULL(); |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | /* the mutex is already locked by another thread, if its state is 1 |
| 647 | * we will change it to 2 to indicate contention. */ |
| 648 | if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) { |
| 649 | newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 650 | if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 651 | mvalue = mutex->value; |
| 652 | continue; |
| 653 | } |
| 654 | mvalue = newval; |
| 655 | } |
| 656 | |
| 657 | /* wait until the mutex is unlocked */ |
| 658 | __futex_wait_ex(&mutex->value, shared, mvalue, NULL); |
| 659 | |
| 660 | mvalue = mutex->value; |
| 661 | } |
| 662 | /* NOTREACHED */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 665 | int pthread_mutex_lock(pthread_mutex_t *mutex) |
| 666 | { |
| 667 | int err = pthread_mutex_lock_impl(mutex); |
| 668 | #ifdef PTHREAD_DEBUG |
| 669 | if (PTHREAD_DEBUG_ENABLED) { |
| 670 | if (!err) { |
| 671 | pthread_debug_mutex_lock_check(mutex); |
| 672 | } |
| 673 | } |
| 674 | #endif |
| 675 | return err; |
| 676 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 677 | |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 678 | __LIBC_HIDDEN__ |
| 679 | int pthread_mutex_unlock_impl(pthread_mutex_t *mutex) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 680 | { |
Wink Saville | a12c544 | 2013-01-08 15:15:45 -0800 | [diff] [blame] | 681 | int mvalue, mtype, tid, shared; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 682 | |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 683 | if (__predict_false(mutex == NULL)) |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 684 | return EINVAL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 685 | |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 686 | mvalue = mutex->value; |
| 687 | mtype = (mvalue & MUTEX_TYPE_MASK); |
| 688 | shared = (mvalue & MUTEX_SHARED_MASK); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 689 | |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 690 | /* Handle common case first */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 691 | if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) { |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 692 | _normal_unlock(mutex, shared); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 693 | return 0; |
| 694 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 695 | |
| 696 | /* Do we already own this recursive or error-check mutex ? */ |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 697 | tid = __get_thread()->tid; |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 698 | if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) ) |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 699 | return EPERM; |
| 700 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 701 | /* If the counter is > 0, we can simply decrement it atomically. |
| 702 | * Since other threads can mutate the lower state bits (and only the |
| 703 | * lower state bits), use a cmpxchg to do it. |
| 704 | */ |
| 705 | if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) { |
| 706 | for (;;) { |
| 707 | int newval = mvalue - MUTEX_COUNTER_BITS_ONE; |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 708 | if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 709 | /* success: we still own the mutex, so no memory barrier */ |
| 710 | return 0; |
| 711 | } |
| 712 | /* the value changed, so reload and loop */ |
| 713 | mvalue = mutex->value; |
| 714 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 715 | } |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 716 | |
| 717 | /* the counter is 0, so we're going to unlock the mutex by resetting |
| 718 | * its value to 'unlocked'. We need to perform a swap in order |
| 719 | * to read the current state, which will be 2 if there are waiters |
| 720 | * to awake. |
| 721 | * |
| 722 | * TODO: Change this to __bionic_swap_release when we implement it |
| 723 | * to get rid of the explicit memory barrier below. |
| 724 | */ |
| 725 | ANDROID_MEMBAR_FULL(); /* RELEASE BARRIER */ |
| 726 | mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 727 | |
| 728 | /* Wake one waiting thread, if any */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 729 | if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) { |
David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 730 | __futex_wake_ex(&mutex->value, shared, 1); |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 731 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 732 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 733 | } |
| 734 | |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 735 | int pthread_mutex_unlock(pthread_mutex_t *mutex) |
| 736 | { |
| 737 | #ifdef PTHREAD_DEBUG |
| 738 | if (PTHREAD_DEBUG_ENABLED) { |
| 739 | pthread_debug_mutex_unlock_check(mutex); |
| 740 | } |
| 741 | #endif |
| 742 | return pthread_mutex_unlock_impl(mutex); |
| 743 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 744 | |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 745 | __LIBC_HIDDEN__ |
| 746 | int pthread_mutex_trylock_impl(pthread_mutex_t *mutex) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 747 | { |
Wink Saville | a12c544 | 2013-01-08 15:15:45 -0800 | [diff] [blame] | 748 | int mvalue, mtype, tid, shared; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 749 | |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 750 | if (__predict_false(mutex == NULL)) |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 751 | return EINVAL; |
| 752 | |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 753 | mvalue = mutex->value; |
| 754 | mtype = (mvalue & MUTEX_TYPE_MASK); |
| 755 | shared = (mvalue & MUTEX_SHARED_MASK); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 756 | |
| 757 | /* Handle common case first */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 758 | if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 759 | { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 760 | if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED, |
| 761 | shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED, |
| 762 | &mutex->value) == 0) { |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 763 | ANDROID_MEMBAR_FULL(); |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 764 | return 0; |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 765 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 766 | |
| 767 | return EBUSY; |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 768 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 769 | |
| 770 | /* Do we already own this recursive or error-check mutex ? */ |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 771 | tid = __get_thread()->tid; |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 772 | if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) ) |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 773 | return _recursive_increment(mutex, mvalue, mtype); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 774 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 775 | /* Same as pthread_mutex_lock, except that we don't want to wait, and |
| 776 | * the only operation that can succeed is a single cmpxchg to acquire the |
| 777 | * lock if it is released / not owned by anyone. No need for a complex loop. |
| 778 | */ |
| 779 | mtype |= shared | MUTEX_STATE_BITS_UNLOCKED; |
| 780 | 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] | 781 | |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 782 | if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 783 | ANDROID_MEMBAR_FULL(); |
| 784 | return 0; |
| 785 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 786 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 787 | return EBUSY; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 788 | } |
| 789 | |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 790 | int pthread_mutex_trylock(pthread_mutex_t *mutex) |
| 791 | { |
| 792 | int err = pthread_mutex_trylock_impl(mutex); |
| 793 | #ifdef PTHREAD_DEBUG |
| 794 | if (PTHREAD_DEBUG_ENABLED) { |
| 795 | if (!err) { |
| 796 | pthread_debug_mutex_lock_check(mutex); |
| 797 | } |
| 798 | } |
| 799 | #endif |
| 800 | return err; |
| 801 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 802 | |
David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 803 | /* initialize 'ts' with the difference between 'abstime' and the current time |
| 804 | * according to 'clock'. Returns -1 if abstime already expired, or 0 otherwise. |
| 805 | */ |
| 806 | static int |
| 807 | __timespec_to_absolute(struct timespec* ts, const struct timespec* abstime, clockid_t clock) |
| 808 | { |
| 809 | clock_gettime(clock, ts); |
| 810 | ts->tv_sec = abstime->tv_sec - ts->tv_sec; |
| 811 | ts->tv_nsec = abstime->tv_nsec - ts->tv_nsec; |
| 812 | if (ts->tv_nsec < 0) { |
| 813 | ts->tv_sec--; |
| 814 | ts->tv_nsec += 1000000000; |
| 815 | } |
David 'Digit' Turner | bc10cd2 | 2009-09-23 15:56:50 -0700 | [diff] [blame] | 816 | if ((ts->tv_nsec < 0) || (ts->tv_sec < 0)) |
David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 817 | return -1; |
| 818 | |
| 819 | return 0; |
| 820 | } |
| 821 | |
| 822 | /* initialize 'abstime' to the current time according to 'clock' plus 'msecs' |
| 823 | * milliseconds. |
| 824 | */ |
| 825 | static void |
| 826 | __timespec_to_relative_msec(struct timespec* abstime, unsigned msecs, clockid_t clock) |
| 827 | { |
| 828 | clock_gettime(clock, abstime); |
| 829 | abstime->tv_sec += msecs/1000; |
| 830 | abstime->tv_nsec += (msecs%1000)*1000000; |
| 831 | if (abstime->tv_nsec >= 1000000000) { |
| 832 | abstime->tv_sec++; |
| 833 | abstime->tv_nsec -= 1000000000; |
| 834 | } |
| 835 | } |
| 836 | |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 837 | __LIBC_HIDDEN__ |
| 838 | 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] | 839 | { |
| 840 | clockid_t clock = CLOCK_MONOTONIC; |
| 841 | struct timespec abstime; |
| 842 | struct timespec ts; |
Wink Saville | a12c544 | 2013-01-08 15:15:45 -0800 | [diff] [blame] | 843 | int mvalue, mtype, tid, shared; |
David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 844 | |
| 845 | /* compute absolute expiration time */ |
| 846 | __timespec_to_relative_msec(&abstime, msecs, clock); |
| 847 | |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 848 | if (__predict_false(mutex == NULL)) |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 849 | return EINVAL; |
| 850 | |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 851 | mvalue = mutex->value; |
| 852 | mtype = (mvalue & MUTEX_TYPE_MASK); |
| 853 | shared = (mvalue & MUTEX_SHARED_MASK); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 854 | |
| 855 | /* Handle common case first */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 856 | if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 857 | { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 858 | const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED; |
| 859 | const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
| 860 | const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; |
| 861 | |
| 862 | /* fast path for uncontended lock. Note: MUTEX_TYPE_BITS_NORMAL is 0 */ |
| 863 | if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) == 0) { |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 864 | ANDROID_MEMBAR_FULL(); |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 865 | return 0; |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 866 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 867 | |
| 868 | /* loop while needed */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 869 | while (__bionic_swap(locked_contended, &mutex->value) != unlocked) { |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 870 | if (__timespec_to_absolute(&ts, &abstime, clock) < 0) |
| 871 | return EBUSY; |
| 872 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 873 | __futex_wait_ex(&mutex->value, shared, locked_contended, &ts); |
Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 874 | } |
Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 875 | ANDROID_MEMBAR_FULL(); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 876 | return 0; |
David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 877 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 878 | |
| 879 | /* Do we already own this recursive or error-check mutex ? */ |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 880 | tid = __get_thread()->tid; |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 881 | if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) ) |
David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 882 | return _recursive_increment(mutex, mvalue, mtype); |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 883 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 884 | /* the following implements the same loop than pthread_mutex_lock_impl |
| 885 | * but adds checks to ensure that the operation never exceeds the |
| 886 | * absolute expiration time. |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 887 | */ |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 888 | mtype |= shared; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 889 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 890 | /* first try a quick lock */ |
| 891 | if (mvalue == mtype) { |
| 892 | mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 893 | if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 894 | ANDROID_MEMBAR_FULL(); |
| 895 | return 0; |
| 896 | } |
| 897 | mvalue = mutex->value; |
| 898 | } |
David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 899 | |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 900 | for (;;) { |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 901 | struct timespec ts; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 902 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 903 | /* if the value is 'unlocked', try to acquire it directly */ |
| 904 | /* NOTE: put state to 2 since we know there is contention */ |
| 905 | if (mvalue == mtype) /* unlocked */ { |
| 906 | mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED; |
| 907 | if (__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0) { |
| 908 | ANDROID_MEMBAR_FULL(); |
| 909 | return 0; |
| 910 | } |
| 911 | /* the value changed before we could lock it. We need to check |
| 912 | * the time to avoid livelocks, reload the value, then loop again. */ |
| 913 | if (__timespec_to_absolute(&ts, &abstime, clock) < 0) |
| 914 | return EBUSY; |
| 915 | |
| 916 | mvalue = mutex->value; |
| 917 | continue; |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 918 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 919 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 920 | /* The value is locked. If 'uncontended', try to switch its state |
| 921 | * to 'contented' to ensure we get woken up later. */ |
| 922 | if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) { |
| 923 | int newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); |
| 924 | if (__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0) { |
| 925 | /* this failed because the value changed, reload it */ |
| 926 | mvalue = mutex->value; |
| 927 | } else { |
| 928 | /* this succeeded, update mvalue */ |
| 929 | mvalue = newval; |
| 930 | } |
| 931 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 932 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 933 | /* check time and update 'ts' */ |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 934 | if (__timespec_to_absolute(&ts, &abstime, clock) < 0) |
| 935 | return EBUSY; |
| 936 | |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 937 | /* Only wait to be woken up if the state is '2', otherwise we'll |
| 938 | * simply loop right now. This can happen when the second cmpxchg |
| 939 | * in our loop failed because the mutex was unlocked by another |
| 940 | * thread. |
| 941 | */ |
| 942 | if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) { |
| 943 | if (__futex_wait_ex(&mutex->value, shared, mvalue, &ts) == ETIMEDOUT) { |
| 944 | return EBUSY; |
| 945 | } |
| 946 | mvalue = mutex->value; |
| 947 | } |
David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 948 | } |
David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 949 | /* NOTREACHED */ |
David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 952 | int pthread_mutex_lock_timeout_np(pthread_mutex_t *mutex, unsigned msecs) |
| 953 | { |
| 954 | int err = pthread_mutex_lock_timeout_np_impl(mutex, msecs); |
| 955 | #ifdef PTHREAD_DEBUG |
| 956 | if (PTHREAD_DEBUG_ENABLED) { |
| 957 | if (!err) { |
| 958 | pthread_debug_mutex_lock_check(mutex); |
| 959 | } |
| 960 | } |
| 961 | #endif |
| 962 | return err; |
| 963 | } |
| 964 | |
| 965 | int pthread_mutex_destroy(pthread_mutex_t *mutex) |
| 966 | { |
| 967 | int ret; |
| 968 | |
| 969 | /* use trylock to ensure that the mutex value is |
| 970 | * valid and is not already locked. */ |
| 971 | ret = pthread_mutex_trylock_impl(mutex); |
| 972 | if (ret != 0) |
| 973 | return ret; |
| 974 | |
| 975 | mutex->value = 0xdead10cc; |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | |
| 980 | |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 981 | int pthread_condattr_init(pthread_condattr_t *attr) |
| 982 | { |
| 983 | if (attr == NULL) |
| 984 | return EINVAL; |
| 985 | |
| 986 | *attr = PTHREAD_PROCESS_PRIVATE; |
| 987 | return 0; |
| 988 | } |
| 989 | |
| 990 | int pthread_condattr_getpshared(pthread_condattr_t *attr, int *pshared) |
| 991 | { |
| 992 | if (attr == NULL || pshared == NULL) |
| 993 | return EINVAL; |
| 994 | |
| 995 | *pshared = *attr; |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared) |
| 1000 | { |
| 1001 | if (attr == NULL) |
| 1002 | return EINVAL; |
| 1003 | |
| 1004 | if (pshared != PTHREAD_PROCESS_SHARED && |
| 1005 | pshared != PTHREAD_PROCESS_PRIVATE) |
| 1006 | return EINVAL; |
| 1007 | |
| 1008 | *attr = pshared; |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | int pthread_condattr_destroy(pthread_condattr_t *attr) |
| 1013 | { |
| 1014 | if (attr == NULL) |
| 1015 | return EINVAL; |
| 1016 | |
| 1017 | *attr = 0xdeada11d; |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | /* We use one bit in condition variable values as the 'shared' flag |
| 1022 | * The rest is a counter. |
| 1023 | */ |
David 'Digit' Turner | b5e4a41 | 2010-03-19 17:59:23 -0700 | [diff] [blame] | 1024 | #define COND_SHARED_MASK 0x0001 |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1025 | #define COND_COUNTER_INCREMENT 0x0002 |
David 'Digit' Turner | b5e4a41 | 2010-03-19 17:59:23 -0700 | [diff] [blame] | 1026 | #define COND_COUNTER_MASK (~COND_SHARED_MASK) |
| 1027 | |
| 1028 | #define COND_IS_SHARED(c) (((c)->value & COND_SHARED_MASK) != 0) |
David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 1029 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1030 | /* XXX *technically* there is a race condition that could allow |
| 1031 | * XXX a signal to be missed. If thread A is preempted in _wait() |
| 1032 | * XXX after unlocking the mutex and before waiting, and if other |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1033 | * XXX threads call signal or broadcast UINT_MAX/2 times (exactly), |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1034 | * XXX before thread A is scheduled again and calls futex_wait(), |
| 1035 | * XXX then the signal will be lost. |
| 1036 | */ |
| 1037 | |
| 1038 | int pthread_cond_init(pthread_cond_t *cond, |
| 1039 | const pthread_condattr_t *attr) |
| 1040 | { |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1041 | if (cond == NULL) |
| 1042 | return EINVAL; |
| 1043 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1044 | cond->value = 0; |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1045 | |
| 1046 | if (attr != NULL && *attr == PTHREAD_PROCESS_SHARED) |
David 'Digit' Turner | b5e4a41 | 2010-03-19 17:59:23 -0700 | [diff] [blame] | 1047 | cond->value |= COND_SHARED_MASK; |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1048 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | int pthread_cond_destroy(pthread_cond_t *cond) |
| 1053 | { |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1054 | if (cond == NULL) |
| 1055 | return EINVAL; |
| 1056 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1057 | cond->value = 0xdeadc04d; |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1061 | /* This function is used by pthread_cond_broadcast and |
David 'Digit' Turner | b5e4a41 | 2010-03-19 17:59:23 -0700 | [diff] [blame] | 1062 | * pthread_cond_signal to atomically decrement the counter |
| 1063 | * then wake-up 'counter' threads. |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1064 | */ |
David 'Digit' Turner | b5e4a41 | 2010-03-19 17:59:23 -0700 | [diff] [blame] | 1065 | static int |
| 1066 | __pthread_cond_pulse(pthread_cond_t *cond, int counter) |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1067 | { |
David 'Digit' Turner | b5e4a41 | 2010-03-19 17:59:23 -0700 | [diff] [blame] | 1068 | long flags; |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1069 | |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 1070 | if (__predict_false(cond == NULL)) |
David 'Digit' Turner | b5e4a41 | 2010-03-19 17:59:23 -0700 | [diff] [blame] | 1071 | return EINVAL; |
| 1072 | |
| 1073 | flags = (cond->value & ~COND_COUNTER_MASK); |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1074 | for (;;) { |
| 1075 | long oldval = cond->value; |
| 1076 | long newval = ((oldval - COND_COUNTER_INCREMENT) & COND_COUNTER_MASK) |
| 1077 | | flags; |
David 'Digit' Turner | e31bfae | 2011-11-15 15:47:02 +0100 | [diff] [blame] | 1078 | if (__bionic_cmpxchg(oldval, newval, &cond->value) == 0) |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1079 | break; |
| 1080 | } |
David 'Digit' Turner | b5e4a41 | 2010-03-19 17:59:23 -0700 | [diff] [blame] | 1081 | |
Andy McFadden | e2ac898 | 2010-09-02 13:34:53 -0700 | [diff] [blame] | 1082 | /* |
| 1083 | * Ensure that all memory accesses previously made by this thread are |
| 1084 | * visible to the woken thread(s). On the other side, the "wait" |
| 1085 | * code will issue any necessary barriers when locking the mutex. |
| 1086 | * |
| 1087 | * This may not strictly be necessary -- if the caller follows |
| 1088 | * recommended practice and holds the mutex before signaling the cond |
| 1089 | * var, the mutex ops will provide correct semantics. If they don't |
| 1090 | * hold the mutex, they're subject to race conditions anyway. |
| 1091 | */ |
| 1092 | ANDROID_MEMBAR_FULL(); |
| 1093 | |
David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 1094 | __futex_wake_ex(&cond->value, COND_IS_SHARED(cond), counter); |
David 'Digit' Turner | b5e4a41 | 2010-03-19 17:59:23 -0700 | [diff] [blame] | 1095 | return 0; |
David 'Digit' Turner | ee7b077 | 2010-03-18 14:07:42 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1098 | int pthread_cond_broadcast(pthread_cond_t *cond) |
| 1099 | { |
David 'Digit' Turner | b5e4a41 | 2010-03-19 17:59:23 -0700 | [diff] [blame] | 1100 | return __pthread_cond_pulse(cond, INT_MAX); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | int pthread_cond_signal(pthread_cond_t *cond) |
| 1104 | { |
David 'Digit' Turner | b5e4a41 | 2010-03-19 17:59:23 -0700 | [diff] [blame] | 1105 | return __pthread_cond_pulse(cond, 1); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) |
| 1109 | { |
| 1110 | return pthread_cond_timedwait(cond, mutex, NULL); |
| 1111 | } |
| 1112 | |
| 1113 | int __pthread_cond_timedwait_relative(pthread_cond_t *cond, |
| 1114 | pthread_mutex_t * mutex, |
| 1115 | const struct timespec *reltime) |
| 1116 | { |
| 1117 | int status; |
| 1118 | int oldvalue = cond->value; |
| 1119 | |
| 1120 | pthread_mutex_unlock(mutex); |
David 'Digit' Turner | 6304d8b | 2010-06-02 18:12:12 -0700 | [diff] [blame] | 1121 | status = __futex_wait_ex(&cond->value, COND_IS_SHARED(cond), oldvalue, reltime); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1122 | pthread_mutex_lock(mutex); |
| 1123 | |
| 1124 | if (status == (-ETIMEDOUT)) return ETIMEDOUT; |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
| 1128 | int __pthread_cond_timedwait(pthread_cond_t *cond, |
| 1129 | pthread_mutex_t * mutex, |
| 1130 | const struct timespec *abstime, |
| 1131 | clockid_t clock) |
| 1132 | { |
| 1133 | struct timespec ts; |
| 1134 | struct timespec * tsp; |
| 1135 | |
| 1136 | if (abstime != NULL) { |
David 'Digit' Turner | 3f56b7f | 2009-09-22 12:40:22 -0700 | [diff] [blame] | 1137 | if (__timespec_to_absolute(&ts, abstime, clock) < 0) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1138 | return ETIMEDOUT; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1139 | tsp = &ts; |
| 1140 | } else { |
| 1141 | tsp = NULL; |
| 1142 | } |
| 1143 | |
| 1144 | return __pthread_cond_timedwait_relative(cond, mutex, tsp); |
| 1145 | } |
| 1146 | |
| 1147 | int pthread_cond_timedwait(pthread_cond_t *cond, |
| 1148 | pthread_mutex_t * mutex, |
| 1149 | const struct timespec *abstime) |
| 1150 | { |
| 1151 | return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_REALTIME); |
| 1152 | } |
| 1153 | |
| 1154 | |
Mathias Agopian | a2f5e21 | 2009-07-13 15:00:46 -0700 | [diff] [blame] | 1155 | /* this one exists only for backward binary compatibility */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1156 | int pthread_cond_timedwait_monotonic(pthread_cond_t *cond, |
| 1157 | pthread_mutex_t * mutex, |
| 1158 | const struct timespec *abstime) |
| 1159 | { |
| 1160 | return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC); |
| 1161 | } |
| 1162 | |
Mathias Agopian | a2f5e21 | 2009-07-13 15:00:46 -0700 | [diff] [blame] | 1163 | int pthread_cond_timedwait_monotonic_np(pthread_cond_t *cond, |
| 1164 | pthread_mutex_t * mutex, |
| 1165 | const struct timespec *abstime) |
| 1166 | { |
| 1167 | return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC); |
| 1168 | } |
| 1169 | |
| 1170 | int pthread_cond_timedwait_relative_np(pthread_cond_t *cond, |
| 1171 | pthread_mutex_t * mutex, |
| 1172 | const struct timespec *reltime) |
| 1173 | { |
| 1174 | return __pthread_cond_timedwait_relative(cond, mutex, reltime); |
| 1175 | } |
| 1176 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1177 | int pthread_cond_timeout_np(pthread_cond_t *cond, |
| 1178 | pthread_mutex_t * mutex, |
| 1179 | unsigned msecs) |
| 1180 | { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1181 | struct timespec ts; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1182 | |
| 1183 | ts.tv_sec = msecs / 1000; |
| 1184 | ts.tv_nsec = (msecs % 1000) * 1000000; |
| 1185 | |
Matthieu CASTET | a4e67f4 | 2008-12-27 00:04:10 +0100 | [diff] [blame] | 1186 | return __pthread_cond_timedwait_relative(cond, mutex, &ts); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1190 | /* NOTE: this implementation doesn't support a init function that throws a C++ exception |
| 1191 | * or calls fork() |
| 1192 | */ |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 1193 | int pthread_once( pthread_once_t* once_control, void (*init_routine)(void) ) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1194 | { |
Andy McFadden | b1c9cc2 | 2010-09-23 12:30:12 -0700 | [diff] [blame] | 1195 | volatile pthread_once_t* ocptr = once_control; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1196 | |
David 'Digit' Turner | 6c6de44 | 2011-12-07 12:20:44 +0100 | [diff] [blame] | 1197 | /* PTHREAD_ONCE_INIT is 0, we use the following bit flags |
| 1198 | * |
| 1199 | * bit 0 set -> initialization is under way |
| 1200 | * bit 1 set -> initialization is complete |
| 1201 | */ |
| 1202 | #define ONCE_INITIALIZING (1 << 0) |
| 1203 | #define ONCE_COMPLETED (1 << 1) |
| 1204 | |
| 1205 | /* First check if the once is already initialized. This will be the common |
| 1206 | * case and we want to make this as fast as possible. Note that this still |
| 1207 | * requires a load_acquire operation here to ensure that all the |
| 1208 | * stores performed by the initialization function are observable on |
| 1209 | * this CPU after we exit. |
| 1210 | */ |
Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 1211 | if (__predict_true((*ocptr & ONCE_COMPLETED) != 0)) { |
David 'Digit' Turner | 6c6de44 | 2011-12-07 12:20:44 +0100 | [diff] [blame] | 1212 | ANDROID_MEMBAR_FULL(); |
| 1213 | return 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1214 | } |
David 'Digit' Turner | 6c6de44 | 2011-12-07 12:20:44 +0100 | [diff] [blame] | 1215 | |
| 1216 | for (;;) { |
| 1217 | /* Try to atomically set the INITIALIZING flag. |
| 1218 | * This requires a cmpxchg loop, and we may need |
Elliott Hughes | bfeab1b | 2012-09-05 17:47:37 -0700 | [diff] [blame] | 1219 | * to exit prematurely if we detect that |
David 'Digit' Turner | 6c6de44 | 2011-12-07 12:20:44 +0100 | [diff] [blame] | 1220 | * COMPLETED is now set. |
| 1221 | */ |
| 1222 | int32_t oldval, newval; |
| 1223 | |
| 1224 | do { |
| 1225 | oldval = *ocptr; |
| 1226 | if ((oldval & ONCE_COMPLETED) != 0) |
| 1227 | break; |
| 1228 | |
| 1229 | newval = oldval | ONCE_INITIALIZING; |
| 1230 | } while (__bionic_cmpxchg(oldval, newval, ocptr) != 0); |
| 1231 | |
| 1232 | if ((oldval & ONCE_COMPLETED) != 0) { |
| 1233 | /* We detected that COMPLETED was set while in our loop */ |
| 1234 | ANDROID_MEMBAR_FULL(); |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
| 1238 | if ((oldval & ONCE_INITIALIZING) == 0) { |
| 1239 | /* We got there first, we can jump out of the loop to |
| 1240 | * handle the initialization */ |
| 1241 | break; |
| 1242 | } |
| 1243 | |
| 1244 | /* Another thread is running the initialization and hasn't completed |
| 1245 | * yet, so wait for it, then try again. */ |
| 1246 | __futex_wait_ex(ocptr, 0, oldval, NULL); |
| 1247 | } |
| 1248 | |
| 1249 | /* call the initialization function. */ |
| 1250 | (*init_routine)(); |
| 1251 | |
| 1252 | /* Do a store_release indicating that initialization is complete */ |
| 1253 | ANDROID_MEMBAR_FULL(); |
| 1254 | *ocptr = ONCE_COMPLETED; |
| 1255 | |
| 1256 | /* Wake up any waiters, if any */ |
| 1257 | __futex_wake_ex(ocptr, 0, INT_MAX); |
| 1258 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1259 | return 0; |
| 1260 | } |
André Goddard Rosa | 78c1c04 | 2010-05-19 23:17:16 -0300 | [diff] [blame] | 1261 | |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 1262 | pid_t __pthread_gettid(pthread_t thid) { |
| 1263 | pthread_internal_t* thread = (pthread_internal_t*) thid; |
| 1264 | return thread->tid; |
Glenn Kasten | d53cae0 | 2011-07-11 15:41:28 -0700 | [diff] [blame] | 1265 | } |
Jean-Baptiste Queru | faca92f | 2012-03-26 15:25:19 -0700 | [diff] [blame] | 1266 | |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 1267 | int __pthread_settid(pthread_t thid, pid_t tid) { |
| 1268 | if (thid == 0) { |
| 1269 | return EINVAL; |
| 1270 | } |
Jean-Baptiste Queru | faca92f | 2012-03-26 15:25:19 -0700 | [diff] [blame] | 1271 | |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 1272 | pthread_internal_t* thread = (pthread_internal_t*) thid; |
| 1273 | thread->tid = tid; |
Jean-Baptiste Queru | faca92f | 2012-03-26 15:25:19 -0700 | [diff] [blame] | 1274 | |
Elliott Hughes | 40eabe2 | 2013-02-14 18:59:37 -0800 | [diff] [blame] | 1275 | return 0; |
Jean-Baptiste Queru | faca92f | 2012-03-26 15:25:19 -0700 | [diff] [blame] | 1276 | } |