| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2008 The Android Open Source Project | 
 | 3 |  * All rights reserved. | 
 | 4 |  * | 
 | 5 |  * Redistribution and use in source and binary forms, with or without | 
 | 6 |  * modification, are permitted provided that the following conditions | 
 | 7 |  * are met: | 
 | 8 |  *  * Redistributions of source code must retain the above copyright | 
 | 9 |  *    notice, this list of conditions and the following disclaimer. | 
 | 10 |  *  * Redistributions in binary form must reproduce the above copyright | 
 | 11 |  *    notice, this list of conditions and the following disclaimer in | 
 | 12 |  *    the documentation and/or other materials provided with the | 
 | 13 |  *    distribution. | 
 | 14 |  * | 
 | 15 |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
 | 16 |  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
 | 17 |  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
 | 18 |  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
 | 19 |  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
 | 20 |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
 | 21 |  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
 | 22 |  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
 | 23 |  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
 | 24 |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
 | 25 |  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
 | 26 |  * SUCH DAMAGE. | 
 | 27 |  */ | 
| Pierre Peiffer | d0c884d | 2012-02-22 16:40:15 +0100 | [diff] [blame] | 28 |  | 
| Elliott Hughes | 6f94de3 | 2013-02-12 06:06:22 +0000 | [diff] [blame] | 29 | #include <pthread.h> | 
| Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 30 |  | 
 | 31 | #include <errno.h> | 
 | 32 | #include <limits.h> | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 33 | #include <stdatomic.h> | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 34 | #include <stdlib.h> | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 35 | #include <string.h> | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 36 | #include <sys/cdefs.h> | 
| Elliott Hughes | 84114c8 | 2013-07-17 13:33:19 -0700 | [diff] [blame] | 37 | #include <sys/mman.h> | 
| Pierre Peiffer | d0c884d | 2012-02-22 16:40:15 +0100 | [diff] [blame] | 38 | #include <unistd.h> | 
 | 39 |  | 
| Pierre Peiffer | d0c884d | 2012-02-22 16:40:15 +0100 | [diff] [blame] | 40 | #include "pthread_internal.h" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 41 |  | 
| Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 42 | #include "private/bionic_constants.h" | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 43 | #include "private/bionic_fortify.h" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 44 | #include "private/bionic_futex.h" | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 45 | #include "private/bionic_systrace.h" | 
| Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 46 | #include "private/bionic_time_conversions.h" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 47 | #include "private/bionic_tls.h" | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 48 |  | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 49 | /* a mutex attribute holds the following fields | 
 | 50 |  * | 
 | 51 |  * bits:     name       description | 
 | 52 |  * 0-3       type       type of mutex | 
 | 53 |  * 4         shared     process-shared flag | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 54 |  * 5         protocol   whether it is a priority inherit mutex. | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 55 |  */ | 
 | 56 | #define  MUTEXATTR_TYPE_MASK   0x000f | 
 | 57 | #define  MUTEXATTR_SHARED_MASK 0x0010 | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 58 | #define MUTEXATTR_PROTOCOL_MASK 0x0020 | 
 | 59 |  | 
 | 60 | #define MUTEXATTR_PROTOCOL_SHIFT 5 | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 61 |  | 
 | 62 | int pthread_mutexattr_init(pthread_mutexattr_t *attr) | 
 | 63 | { | 
 | 64 |     *attr = PTHREAD_MUTEX_DEFAULT; | 
 | 65 |     return 0; | 
 | 66 | } | 
 | 67 |  | 
 | 68 | int pthread_mutexattr_destroy(pthread_mutexattr_t *attr) | 
 | 69 | { | 
 | 70 |     *attr = -1; | 
 | 71 |     return 0; | 
 | 72 | } | 
 | 73 |  | 
 | 74 | int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type_p) | 
 | 75 | { | 
 | 76 |     int type = (*attr & MUTEXATTR_TYPE_MASK); | 
 | 77 |  | 
 | 78 |     if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK) { | 
 | 79 |         return EINVAL; | 
 | 80 |     } | 
 | 81 |  | 
 | 82 |     *type_p = type; | 
 | 83 |     return 0; | 
 | 84 | } | 
 | 85 |  | 
 | 86 | int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) | 
 | 87 | { | 
 | 88 |     if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK ) { | 
 | 89 |         return EINVAL; | 
 | 90 |     } | 
 | 91 |  | 
 | 92 |     *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type; | 
 | 93 |     return 0; | 
 | 94 | } | 
 | 95 |  | 
 | 96 | /* process-shared mutexes are not supported at the moment */ | 
 | 97 |  | 
 | 98 | int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int  pshared) | 
 | 99 | { | 
 | 100 |     switch (pshared) { | 
 | 101 |     case PTHREAD_PROCESS_PRIVATE: | 
 | 102 |         *attr &= ~MUTEXATTR_SHARED_MASK; | 
 | 103 |         return 0; | 
 | 104 |  | 
 | 105 |     case PTHREAD_PROCESS_SHARED: | 
 | 106 |         /* our current implementation of pthread actually supports shared | 
 | 107 |          * mutexes but won't cleanup if a process dies with the mutex held. | 
 | 108 |          * Nevertheless, it's better than nothing. Shared mutexes are used | 
 | 109 |          * by surfaceflinger and audioflinger. | 
 | 110 |          */ | 
 | 111 |         *attr |= MUTEXATTR_SHARED_MASK; | 
 | 112 |         return 0; | 
 | 113 |     } | 
 | 114 |     return EINVAL; | 
 | 115 | } | 
 | 116 |  | 
 | 117 | int pthread_mutexattr_getpshared(const pthread_mutexattr_t* attr, int* pshared) { | 
 | 118 |     *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE; | 
 | 119 |     return 0; | 
 | 120 | } | 
 | 121 |  | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 122 | int pthread_mutexattr_setprotocol(pthread_mutexattr_t* attr, int protocol) { | 
 | 123 |     if (protocol != PTHREAD_PRIO_NONE && protocol != PTHREAD_PRIO_INHERIT) { | 
 | 124 |         return EINVAL; | 
 | 125 |     } | 
 | 126 |     *attr = (*attr & ~MUTEXATTR_PROTOCOL_MASK) | (protocol << MUTEXATTR_PROTOCOL_SHIFT); | 
 | 127 |     return 0; | 
 | 128 | } | 
 | 129 |  | 
 | 130 | int pthread_mutexattr_getprotocol(const pthread_mutexattr_t* attr, int* protocol) { | 
 | 131 |     *protocol = (*attr & MUTEXATTR_PROTOCOL_MASK) >> MUTEXATTR_PROTOCOL_SHIFT; | 
 | 132 |     return 0; | 
 | 133 | } | 
 | 134 |  | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 135 | // Priority Inheritance mutex implementation | 
 | 136 | struct PIMutex { | 
 | 137 |   // mutex type, can be 0 (normal), 1 (recursive), 2 (errorcheck), constant during lifetime | 
 | 138 |   uint8_t type; | 
 | 139 |   // process-shared flag, constant during lifetime | 
 | 140 |   bool shared; | 
 | 141 |   // <number of times a thread holding a recursive PI mutex> - 1 | 
 | 142 |   uint16_t counter; | 
 | 143 |   // owner_tid is read/written by both userspace code and kernel code. It includes three fields: | 
 | 144 |   // FUTEX_WAITERS, FUTEX_OWNER_DIED and FUTEX_TID_MASK. | 
 | 145 |   atomic_int owner_tid; | 
 | 146 | }; | 
 | 147 |  | 
 | 148 | static inline __always_inline int PIMutexTryLock(PIMutex& mutex) { | 
 | 149 |     pid_t tid = __get_thread()->tid; | 
 | 150 |     // Handle common case first. | 
 | 151 |     int old_owner = 0; | 
 | 152 |     if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex.owner_tid, | 
 | 153 |                                                                &old_owner, tid, | 
 | 154 |                                                                memory_order_acquire, | 
 | 155 |                                                                memory_order_relaxed))) { | 
 | 156 |         return 0; | 
 | 157 |     } | 
 | 158 |     if (tid == (old_owner & FUTEX_TID_MASK)) { | 
 | 159 |         // We already own this mutex. | 
 | 160 |         if (mutex.type == PTHREAD_MUTEX_NORMAL) { | 
 | 161 |             return EBUSY; | 
 | 162 |         } | 
 | 163 |         if (mutex.type == PTHREAD_MUTEX_ERRORCHECK) { | 
 | 164 |             return EDEADLK; | 
 | 165 |         } | 
 | 166 |         if (mutex.counter == 0xffff) { | 
 | 167 |             return EAGAIN; | 
 | 168 |         } | 
 | 169 |         mutex.counter++; | 
 | 170 |         return 0; | 
 | 171 |     } | 
 | 172 |     return EBUSY; | 
 | 173 | } | 
 | 174 |  | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 175 | // Inlining this function in pthread_mutex_lock() adds the cost of stack frame instructions on | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 176 | // ARM/ARM64, which increases at most 20 percent overhead. So make it noinline. | 
 | 177 | static int  __attribute__((noinline)) PIMutexTimedLock(PIMutex& mutex, | 
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 178 |                                                        bool use_realtime_clock, | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 179 |                                                        const timespec* abs_timeout) { | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 180 |     int ret = PIMutexTryLock(mutex); | 
 | 181 |     if (__predict_true(ret == 0)) { | 
 | 182 |         return 0; | 
 | 183 |     } | 
 | 184 |     if (ret == EBUSY) { | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 185 |         ScopedTrace trace("Contending for pthread mutex"); | 
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 186 |         ret = -__futex_pi_lock_ex(&mutex.owner_tid, mutex.shared, use_realtime_clock, abs_timeout); | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 187 |     } | 
 | 188 |     return ret; | 
 | 189 | } | 
 | 190 |  | 
 | 191 | static int PIMutexUnlock(PIMutex& mutex) { | 
 | 192 |     pid_t tid = __get_thread()->tid; | 
 | 193 |     int old_owner = tid; | 
 | 194 |     // Handle common case first. | 
 | 195 |     if (__predict_true(mutex.type == PTHREAD_MUTEX_NORMAL)) { | 
 | 196 |         if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex.owner_tid, | 
 | 197 |                                                                    &old_owner, 0, | 
 | 198 |                                                                    memory_order_release, | 
 | 199 |                                                                    memory_order_relaxed))) { | 
 | 200 |             return 0; | 
 | 201 |         } | 
 | 202 |     } | 
 | 203 |  | 
 | 204 |     if (tid != (old_owner & FUTEX_TID_MASK)) { | 
 | 205 |         // The mutex can only be unlocked by the thread who owns it. | 
 | 206 |         return EPERM; | 
 | 207 |     } | 
 | 208 |     if (mutex.type == PTHREAD_MUTEX_RECURSIVE) { | 
 | 209 |         if (mutex.counter != 0u) { | 
 | 210 |             --mutex.counter; | 
 | 211 |             return 0; | 
 | 212 |         } | 
 | 213 |     } | 
 | 214 |     if (old_owner == tid) { | 
 | 215 |         // No thread is waiting. | 
 | 216 |         if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex.owner_tid, | 
 | 217 |                                                                    &old_owner, 0, | 
 | 218 |                                                                    memory_order_release, | 
 | 219 |                                                                    memory_order_relaxed))) { | 
 | 220 |             return 0; | 
 | 221 |         } | 
 | 222 |     } | 
 | 223 |     return -__futex_pi_unlock(&mutex.owner_tid, mutex.shared); | 
 | 224 | } | 
 | 225 |  | 
 | 226 | static int PIMutexDestroy(PIMutex& mutex) { | 
 | 227 |     // The mutex should be in unlocked state (owner_tid == 0) when destroyed. | 
 | 228 |     // Store 0xffffffff to make the mutex unusable. | 
 | 229 |     int old_owner = 0; | 
 | 230 |     if (atomic_compare_exchange_strong_explicit(&mutex.owner_tid, &old_owner, 0xffffffff, | 
 | 231 |                                                 memory_order_relaxed, memory_order_relaxed)) { | 
 | 232 |         return 0; | 
 | 233 |     } | 
 | 234 |     return EBUSY; | 
 | 235 | } | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 236 |  | 
 | 237 | #if !defined(__LP64__) | 
 | 238 |  | 
 | 239 | namespace PIMutexAllocator { | 
 | 240 | // pthread_mutex_t has only 4 bytes in 32-bit programs, which are not enough to hold PIMutex. | 
 | 241 | // So we use malloc to allocate PIMutexes and use 16-bit of pthread_mutex_t as indexes to find | 
 | 242 | // the allocated PIMutexes. This allows at most 65536 PI mutexes. | 
 | 243 | // When calling operations like pthread_mutex_lock/unlock, the 16-bit index is mapped to the | 
 | 244 | // corresponding PIMutex. To make the map operation fast, we use a lockless mapping method: | 
 | 245 | //   Once a PIMutex is allocated, all the data used to map index to the PIMutex isn't changed until | 
 | 246 | //   it is destroyed. | 
 | 247 | // Below are the data structures: | 
 | 248 | //   // struct Node contains a PIMutex. | 
 | 249 | //   typedef Node NodeArray[256]; | 
 | 250 | //   typedef NodeArray* NodeArrayP; | 
 | 251 | //   NodeArrayP nodes[256]; | 
 | 252 | // | 
 | 253 | // A 16-bit index is mapped to Node as below: | 
 | 254 | //   (*nodes[index >> 8])[index & 0xff] | 
 | 255 | // | 
 | 256 | // Also use a free list to allow O(1) finding recycled PIMutexes. | 
 | 257 |  | 
 | 258 | union Node { | 
 | 259 |     PIMutex mutex; | 
 | 260 |     int next_free_id;  // If not -1, refer to the next node in the free PIMutex list. | 
 | 261 | }; | 
 | 262 | typedef Node NodeArray[256]; | 
 | 263 | typedef NodeArray* NodeArrayP; | 
 | 264 |  | 
 | 265 | // lock_ protects below items. | 
 | 266 | static Lock lock; | 
 | 267 | static NodeArrayP* nodes; | 
 | 268 | static int next_to_alloc_id; | 
 | 269 | static int first_free_id = -1;  // If not -1, refer to the first node in the free PIMutex list. | 
 | 270 |  | 
 | 271 | static inline __always_inline Node& IdToNode(int id) { | 
 | 272 |     return (*nodes[id >> 8])[id & 0xff]; | 
 | 273 | } | 
 | 274 |  | 
 | 275 | static inline __always_inline PIMutex& IdToPIMutex(int id) { | 
 | 276 |     return IdToNode(id).mutex; | 
 | 277 | } | 
 | 278 |  | 
 | 279 | static int AllocIdLocked() { | 
 | 280 |     if (first_free_id != -1) { | 
 | 281 |         int result = first_free_id; | 
 | 282 |         first_free_id = IdToNode(result).next_free_id; | 
 | 283 |         return result; | 
 | 284 |     } | 
 | 285 |     if (next_to_alloc_id >= 0x10000) { | 
 | 286 |         return -1; | 
 | 287 |     } | 
 | 288 |     int array_pos = next_to_alloc_id >> 8; | 
 | 289 |     int node_pos = next_to_alloc_id & 0xff; | 
 | 290 |     if (node_pos == 0) { | 
 | 291 |         if (array_pos == 0) { | 
 | 292 |             nodes = static_cast<NodeArray**>(calloc(256, sizeof(NodeArray*))); | 
 | 293 |             if (nodes == nullptr) { | 
 | 294 |                 return -1; | 
 | 295 |             } | 
 | 296 |         } | 
 | 297 |         nodes[array_pos] = static_cast<NodeArray*>(malloc(sizeof(NodeArray))); | 
 | 298 |         if (nodes[array_pos] == nullptr) { | 
 | 299 |             return -1; | 
 | 300 |         } | 
 | 301 |     } | 
 | 302 |     return next_to_alloc_id++; | 
 | 303 | } | 
 | 304 |  | 
 | 305 | // If succeed, return an id referring to a PIMutex, otherwise return -1. | 
 | 306 | // A valid id is in range [0, 0xffff]. | 
 | 307 | static int AllocId() { | 
 | 308 |     lock.lock(); | 
 | 309 |     int result = AllocIdLocked(); | 
 | 310 |     lock.unlock(); | 
 | 311 |     if (result != -1) { | 
 | 312 |         memset(&IdToPIMutex(result), 0, sizeof(PIMutex)); | 
 | 313 |     } | 
 | 314 |     return result; | 
 | 315 | } | 
 | 316 |  | 
 | 317 | static void FreeId(int id) { | 
 | 318 |     lock.lock(); | 
 | 319 |     IdToNode(id).next_free_id = first_free_id; | 
 | 320 |     first_free_id = id; | 
 | 321 |     lock.unlock(); | 
 | 322 | } | 
 | 323 |  | 
 | 324 | }  // namespace PIMutexAllocator | 
 | 325 |  | 
 | 326 | #endif  // !defined(__LP64__) | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 327 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 328 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 329 | /* Convenience macro, creates a mask of 'bits' bits that starts from | 
 | 330 |  * the 'shift'-th least significant bit in a 32-bit word. | 
 | 331 |  * | 
 | 332 |  * Examples: FIELD_MASK(0,4)  -> 0xf | 
 | 333 |  *           FIELD_MASK(16,9) -> 0x1ff0000 | 
 | 334 |  */ | 
 | 335 | #define  FIELD_MASK(shift,bits)           (((1 << (bits))-1) << (shift)) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 336 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 337 | /* This one is used to create a bit pattern from a given field value */ | 
 | 338 | #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] | 339 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 340 | /* And this one does the opposite, i.e. extract a field's value from a bit pattern */ | 
 | 341 | #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] | 342 |  | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 343 | /* Convenience macros. | 
 | 344 |  * | 
 | 345 |  * These are used to form or modify the bit pattern of a given mutex value | 
 | 346 |  */ | 
 | 347 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 348 | /* Mutex state: | 
 | 349 |  * | 
 | 350 |  * 0 for unlocked | 
 | 351 |  * 1 for locked, no waiters | 
 | 352 |  * 2 for locked, maybe waiters | 
 | 353 |  */ | 
 | 354 | #define  MUTEX_STATE_SHIFT      0 | 
 | 355 | #define  MUTEX_STATE_LEN        2 | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 356 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 357 | #define  MUTEX_STATE_MASK           FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) | 
 | 358 | #define  MUTEX_STATE_FROM_BITS(v)   FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) | 
 | 359 | #define  MUTEX_STATE_TO_BITS(v)     FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN) | 
 | 360 |  | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 361 | #define  MUTEX_STATE_UNLOCKED            0   /* must be 0 to match PTHREAD_MUTEX_INITIALIZER */ | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 362 | #define  MUTEX_STATE_LOCKED_UNCONTENDED  1   /* must be 1 due to atomic dec in unlock operation */ | 
 | 363 | #define  MUTEX_STATE_LOCKED_CONTENDED    2   /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */ | 
 | 364 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 365 | #define  MUTEX_STATE_BITS_UNLOCKED            MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED) | 
 | 366 | #define  MUTEX_STATE_BITS_LOCKED_UNCONTENDED  MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED) | 
 | 367 | #define  MUTEX_STATE_BITS_LOCKED_CONTENDED    MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED) | 
 | 368 |  | 
| Yabin Cui | 0307eee | 2015-11-16 20:19:31 -0800 | [diff] [blame] | 369 | // Return true iff the mutex is unlocked. | 
 | 370 | #define MUTEX_STATE_BITS_IS_UNLOCKED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_UNLOCKED) | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 371 |  | 
| Yabin Cui | 0307eee | 2015-11-16 20:19:31 -0800 | [diff] [blame] | 372 | // Return true iff the mutex is locked with no waiters. | 
 | 373 | #define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v)  (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED) | 
 | 374 |  | 
 | 375 | // return true iff the mutex is locked with maybe waiters. | 
 | 376 | #define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v)   (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED) | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 377 |  | 
 | 378 | /* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */ | 
 | 379 | #define  MUTEX_STATE_BITS_FLIP_CONTENTION(v)      ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) | 
 | 380 |  | 
 | 381 | /* Mutex counter: | 
 | 382 |  * | 
 | 383 |  * We need to check for overflow before incrementing, and we also need to | 
 | 384 |  * detect when the counter is 0 | 
 | 385 |  */ | 
 | 386 | #define  MUTEX_COUNTER_SHIFT         2 | 
 | 387 | #define  MUTEX_COUNTER_LEN           11 | 
 | 388 | #define  MUTEX_COUNTER_MASK          FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN) | 
 | 389 |  | 
 | 390 | #define  MUTEX_COUNTER_BITS_WILL_OVERFLOW(v)    (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK) | 
 | 391 | #define  MUTEX_COUNTER_BITS_IS_ZERO(v)          (((v) & MUTEX_COUNTER_MASK) == 0) | 
 | 392 |  | 
 | 393 | /* Used to increment the counter directly after overflow has been checked */ | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 394 | #define  MUTEX_COUNTER_BITS_ONE      FIELD_TO_BITS(1, MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN) | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 395 |  | 
 | 396 | /* Mutex shared bit flag | 
 | 397 |  * | 
 | 398 |  * This flag is set to indicate that the mutex is shared among processes. | 
 | 399 |  * This changes the futex opcode we use for futex wait/wake operations | 
 | 400 |  * (non-shared operations are much faster). | 
 | 401 |  */ | 
 | 402 | #define  MUTEX_SHARED_SHIFT    13 | 
 | 403 | #define  MUTEX_SHARED_MASK     FIELD_MASK(MUTEX_SHARED_SHIFT,1) | 
 | 404 |  | 
 | 405 | /* Mutex type: | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 406 |  * We support normal, recursive and errorcheck mutexes. | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 407 |  */ | 
 | 408 | #define  MUTEX_TYPE_SHIFT      14 | 
 | 409 | #define  MUTEX_TYPE_LEN        2 | 
 | 410 | #define  MUTEX_TYPE_MASK       FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN) | 
 | 411 |  | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 412 | #define  MUTEX_TYPE_TO_BITS(t)       FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN) | 
 | 413 |  | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 414 | #define  MUTEX_TYPE_BITS_NORMAL      MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_NORMAL) | 
 | 415 | #define  MUTEX_TYPE_BITS_RECURSIVE   MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_RECURSIVE) | 
 | 416 | #define  MUTEX_TYPE_BITS_ERRORCHECK  MUTEX_TYPE_TO_BITS(PTHREAD_MUTEX_ERRORCHECK) | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 417 | // Use a special mutex type to mark priority inheritance mutexes. | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 418 | #define  PI_MUTEX_STATE     MUTEX_TYPE_TO_BITS(3) | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 419 |  | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 420 | // For a PI mutex, it includes below fields: | 
 | 421 | //   Atomic(uint16_t) state; | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 422 | //   PIMutex pi_mutex;  // uint16_t pi_mutex_id in 32-bit programs | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 423 | // | 
 | 424 | //   state holds the following fields: | 
 | 425 | // | 
 | 426 | //   bits:   name    description | 
 | 427 | //   15-14   type    mutex type, should be 3 | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 428 | //   13-0    padding should be 0 | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 429 | // | 
 | 430 | //   pi_mutex holds the state of a PI mutex. | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 431 | //   pi_mutex_id holds an integer to find the state of a PI mutex. | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 432 | // | 
 | 433 | // For a Non-PI mutex, it includes below fields: | 
 | 434 | //   Atomic(uint16_t) state; | 
 | 435 | //   atomic_int owner_tid;  // Atomic(uint16_t) in 32-bit programs | 
 | 436 | // | 
 | 437 | //   state holds the following fields: | 
 | 438 | // | 
 | 439 | //   bits:     name     description | 
 | 440 | //   15-14     type     mutex type, can be 0 (normal), 1 (recursive), 2 (errorcheck) | 
 | 441 | //   13        shared   process-shared flag | 
 | 442 | //   12-2      counter  <number of times a thread holding a recursive Non-PI mutex> - 1 | 
 | 443 | //   1-0       state    lock state (0, 1 or 2) | 
 | 444 | // | 
 | 445 | //   bits 15-13 are constant during the lifetime of the mutex. | 
 | 446 | // | 
 | 447 | //   owner_tid is used only in recursive and errorcheck Non-PI mutexes to hold the mutex owner | 
 | 448 | //   thread id. | 
 | 449 | // | 
 | 450 | // PI mutexes and Non-PI mutexes are distinguished by checking type field in state. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 451 | #if defined(__LP64__) | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 452 | struct pthread_mutex_internal_t { | 
 | 453 |     _Atomic(uint16_t) state; | 
 | 454 |     uint16_t __pad; | 
 | 455 |     union { | 
 | 456 |         atomic_int owner_tid; | 
 | 457 |         PIMutex pi_mutex; | 
 | 458 |     }; | 
 | 459 |     char __reserved[28]; | 
 | 460 |  | 
 | 461 |     PIMutex& ToPIMutex() { | 
 | 462 |         return pi_mutex; | 
 | 463 |     } | 
 | 464 |  | 
 | 465 |     void FreePIMutex() { | 
 | 466 |     } | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 467 | } __attribute__((aligned(4))); | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 468 |  | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 469 | #else | 
 | 470 | struct pthread_mutex_internal_t { | 
 | 471 |     _Atomic(uint16_t) state; | 
 | 472 |     union { | 
 | 473 |         _Atomic(uint16_t) owner_tid; | 
 | 474 |         uint16_t pi_mutex_id; | 
 | 475 |     }; | 
 | 476 |  | 
 | 477 |     PIMutex& ToPIMutex() { | 
 | 478 |         return PIMutexAllocator::IdToPIMutex(pi_mutex_id); | 
 | 479 |     } | 
 | 480 |  | 
 | 481 |     void FreePIMutex() { | 
 | 482 |         PIMutexAllocator::FreeId(pi_mutex_id); | 
 | 483 |     } | 
 | 484 | } __attribute__((aligned(4))); | 
 | 485 | #endif | 
 | 486 |  | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 487 | static_assert(sizeof(pthread_mutex_t) == sizeof(pthread_mutex_internal_t), | 
 | 488 |               "pthread_mutex_t should actually be pthread_mutex_internal_t in implementation."); | 
 | 489 |  | 
 | 490 | // For binary compatibility with old version of pthread_mutex_t, we can't use more strict alignment | 
 | 491 | // than 4-byte alignment. | 
 | 492 | static_assert(alignof(pthread_mutex_t) == 4, | 
 | 493 |               "pthread_mutex_t should fulfill the alignment of pthread_mutex_internal_t."); | 
 | 494 |  | 
 | 495 | static inline pthread_mutex_internal_t* __get_internal_mutex(pthread_mutex_t* mutex_interface) { | 
 | 496 |   return reinterpret_cast<pthread_mutex_internal_t*>(mutex_interface); | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 497 | } | 
 | 498 |  | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 499 | int pthread_mutex_init(pthread_mutex_t* mutex_interface, const pthread_mutexattr_t* attr) { | 
 | 500 |     pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); | 
 | 501 |  | 
 | 502 |     memset(mutex, 0, sizeof(pthread_mutex_internal_t)); | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 503 |  | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 504 |     if (__predict_true(attr == nullptr)) { | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 505 |         atomic_init(&mutex->state, MUTEX_TYPE_BITS_NORMAL); | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 506 |         return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 507 |     } | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 508 |  | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 509 |     uint16_t state = 0; | 
| Elliott Hughes | dff7203 | 2013-12-11 14:54:00 -0800 | [diff] [blame] | 510 |     if ((*attr & MUTEXATTR_SHARED_MASK) != 0) { | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 511 |         state |= MUTEX_SHARED_MASK; | 
| Elliott Hughes | dff7203 | 2013-12-11 14:54:00 -0800 | [diff] [blame] | 512 |     } | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 513 |  | 
 | 514 |     switch (*attr & MUTEXATTR_TYPE_MASK) { | 
 | 515 |     case PTHREAD_MUTEX_NORMAL: | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 516 |       state |= MUTEX_TYPE_BITS_NORMAL; | 
 | 517 |       break; | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 518 |     case PTHREAD_MUTEX_RECURSIVE: | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 519 |       state |= MUTEX_TYPE_BITS_RECURSIVE; | 
 | 520 |       break; | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 521 |     case PTHREAD_MUTEX_ERRORCHECK: | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 522 |       state |= MUTEX_TYPE_BITS_ERRORCHECK; | 
 | 523 |       break; | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 524 |     default: | 
 | 525 |         return EINVAL; | 
 | 526 |     } | 
 | 527 |  | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 528 |     if (((*attr & MUTEXATTR_PROTOCOL_MASK) >> MUTEXATTR_PROTOCOL_SHIFT) == PTHREAD_PRIO_INHERIT) { | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 529 | #if !defined(__LP64__) | 
 | 530 |         if (state & MUTEX_SHARED_MASK) { | 
 | 531 |             return EINVAL; | 
 | 532 |         } | 
 | 533 |         int id = PIMutexAllocator::AllocId(); | 
 | 534 |         if (id == -1) { | 
 | 535 |             return ENOMEM; | 
 | 536 |         } | 
 | 537 |         mutex->pi_mutex_id = id; | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 538 | #endif | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 539 |         atomic_init(&mutex->state, PI_MUTEX_STATE); | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 540 |         PIMutex& pi_mutex = mutex->ToPIMutex(); | 
 | 541 |         pi_mutex.type = *attr & MUTEXATTR_TYPE_MASK; | 
 | 542 |         pi_mutex.shared = (*attr & MUTEXATTR_SHARED_MASK) != 0; | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 543 |     } else { | 
 | 544 |         atomic_init(&mutex->state, state); | 
 | 545 |         atomic_init(&mutex->owner_tid, 0); | 
 | 546 |     } | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 547 |     return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 548 | } | 
 | 549 |  | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 550 | // namespace for Non-PI mutex routines. | 
 | 551 | namespace NonPI { | 
 | 552 |  | 
 | 553 | static inline __always_inline int NormalMutexTryLock(pthread_mutex_internal_t* mutex, | 
 | 554 |                                                      uint16_t shared) { | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 555 |     const uint16_t unlocked           = shared | MUTEX_STATE_BITS_UNLOCKED; | 
 | 556 |     const uint16_t locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 557 |  | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 558 |     uint16_t old_state = unlocked; | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 559 |     if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state, | 
 | 560 |                          locked_uncontended, memory_order_acquire, memory_order_relaxed))) { | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 561 |         return 0; | 
 | 562 |     } | 
 | 563 |     return EBUSY; | 
 | 564 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 565 |  | 
 | 566 | /* | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 567 |  * Lock a normal Non-PI mutex. | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 568 |  * | 
 | 569 |  * As noted above, there are three states: | 
 | 570 |  *   0 (unlocked, no contention) | 
 | 571 |  *   1 (locked, no contention) | 
 | 572 |  *   2 (locked, contention) | 
 | 573 |  * | 
 | 574 |  * Non-recursive mutexes don't use the thread-id or counter fields, and the | 
 | 575 |  * "type" value is zero, so the only bits that will be set are the ones in | 
 | 576 |  * the lock state field. | 
 | 577 |  */ | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 578 | static inline __always_inline int NormalMutexLock(pthread_mutex_internal_t* mutex, | 
 | 579 |                                                   uint16_t shared, | 
 | 580 |                                                   bool use_realtime_clock, | 
 | 581 |                                                   const timespec* abs_timeout_or_null) { | 
 | 582 |     if (__predict_true(NormalMutexTryLock(mutex, shared) == 0)) { | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 583 |         return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 584 |     } | 
| Elliott Hughes | dd586f2 | 2015-12-16 15:15:58 -0800 | [diff] [blame] | 585 |     int result = check_timespec(abs_timeout_or_null, true); | 
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 586 |     if (result != 0) { | 
 | 587 |         return result; | 
 | 588 |     } | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 589 |  | 
 | 590 |     ScopedTrace trace("Contending for pthread mutex"); | 
 | 591 |  | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 592 |     const uint16_t unlocked           = shared | MUTEX_STATE_BITS_UNLOCKED; | 
 | 593 |     const uint16_t locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 594 |  | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 595 |     // We want to go to sleep until the mutex is available, which requires | 
 | 596 |     // promoting it to locked_contended. We need to swap in the new state | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 597 |     // and then wait until somebody wakes us up. | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 598 |     // An atomic_exchange is used to compete with other threads for the lock. | 
 | 599 |     // If it returns unlocked, we have acquired the lock, otherwise another | 
 | 600 |     // thread still holds the lock and we should wait again. | 
 | 601 |     // If lock is acquired, an acquire fence is needed to make all memory accesses | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 602 |     // made by other threads visible to the current CPU. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 603 |     while (atomic_exchange_explicit(&mutex->state, locked_contended, | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 604 |                                     memory_order_acquire) != unlocked) { | 
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 605 |         if (__futex_wait_ex(&mutex->state, shared, locked_contended, use_realtime_clock, | 
 | 606 |                             abs_timeout_or_null) == -ETIMEDOUT) { | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 607 |             return ETIMEDOUT; | 
 | 608 |         } | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 609 |     } | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 610 |     return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 611 | } | 
 | 612 |  | 
 | 613 | /* | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 614 |  * Release a normal Non-PI mutex.  The caller is responsible for determining | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 615 |  * that we are in fact the owner of this lock. | 
 | 616 |  */ | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 617 | static inline __always_inline void NormalMutexUnlock(pthread_mutex_internal_t* mutex, | 
 | 618 |                                                      uint16_t shared) { | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 619 |     const uint16_t unlocked         = shared | MUTEX_STATE_BITS_UNLOCKED; | 
 | 620 |     const uint16_t locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; | 
| Andy McFadden | fcd00eb | 2010-05-28 13:31:45 -0700 | [diff] [blame] | 621 |  | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 622 |     // We use an atomic_exchange to release the lock. If locked_contended state | 
 | 623 |     // is returned, some threads is waiting for the lock and we need to wake up | 
 | 624 |     // one of them. | 
 | 625 |     // A release fence is required to make previous stores visible to next | 
 | 626 |     // lock owner threads. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 627 |     if (atomic_exchange_explicit(&mutex->state, unlocked, | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 628 |                                  memory_order_release) == locked_contended) { | 
 | 629 |         // Wake up one waiting thread. We don't know which thread will be | 
 | 630 |         // woken or when it'll start executing -- futexes make no guarantees | 
 | 631 |         // here. There may not even be a thread waiting. | 
 | 632 |         // | 
 | 633 |         // The newly-woken thread will replace the unlocked state we just set above | 
 | 634 |         // with locked_contended state, which means that when it eventually releases | 
 | 635 |         // the mutex it will also call FUTEX_WAKE. This results in one extra wake | 
 | 636 |         // call whenever a lock is contended, but let us avoid forgetting anyone | 
 | 637 |         // without requiring us to track the number of sleepers. | 
 | 638 |         // | 
 | 639 |         // It's possible for another thread to sneak in and grab the lock between | 
 | 640 |         // the exchange above and the wake call below. If the new thread is "slow" | 
 | 641 |         // and holds the lock for a while, we'll wake up a sleeper, which will swap | 
 | 642 |         // in locked_uncontended state and then go back to sleep since the lock is | 
 | 643 |         // still held. If the new thread is "fast", running to completion before | 
 | 644 |         // we call wake, the thread we eventually wake will find an unlocked mutex | 
 | 645 |         // and will execute. Either way we have correct behavior and nobody is | 
 | 646 |         // orphaned on the wait queue. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 647 |         __futex_wake_ex(&mutex->state, shared, 1); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 648 |     } | 
 | 649 | } | 
 | 650 |  | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 651 | /* This common inlined function is used to increment the counter of a recursive Non-PI mutex. | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 652 |  * | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 653 |  * If the counter overflows, it will return EAGAIN. | 
 | 654 |  * Otherwise, it atomically increments the counter and returns 0. | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 655 |  * | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 656 |  */ | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 657 | static inline __always_inline int RecursiveIncrement(pthread_mutex_internal_t* mutex, | 
 | 658 |                                                      uint16_t old_state) { | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 659 |     // Detect recursive lock overflow and return EAGAIN. | 
 | 660 |     // This is safe because only the owner thread can modify the | 
 | 661 |     // counter bits in the mutex value. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 662 |     if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(old_state)) { | 
| David 'Digit' Turner | 022d303 | 2011-12-07 14:02:17 +0100 | [diff] [blame] | 663 |         return EAGAIN; | 
 | 664 |     } | 
 | 665 |  | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 666 |     // Other threads are able to change the lower bits (e.g. promoting it to "contended"), | 
 | 667 |     // but the mutex counter will not overflow. So we use atomic_fetch_add operation here. | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 668 |     // The mutex is already locked by current thread, so we don't need an acquire fence. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 669 |     atomic_fetch_add_explicit(&mutex->state, MUTEX_COUNTER_BITS_ONE, memory_order_relaxed); | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 670 |     return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 671 | } | 
 | 672 |  | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 673 | // Wait on a recursive or errorcheck Non-PI mutex. | 
 | 674 | static inline __always_inline int RecursiveOrErrorcheckMutexWait(pthread_mutex_internal_t* mutex, | 
 | 675 |                                                                  uint16_t shared, | 
 | 676 |                                                                  uint16_t old_state, | 
 | 677 |                                                                  bool use_realtime_clock, | 
 | 678 |                                                                  const timespec* abs_timeout) { | 
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 679 | // __futex_wait always waits on a 32-bit value. But state is 16-bit. For a normal mutex, the owner_tid | 
 | 680 | // field in mutex is not used. On 64-bit devices, the __pad field in mutex is not used. | 
 | 681 | // But when a recursive or errorcheck mutex is used on 32-bit devices, we need to add the | 
 | 682 | // owner_tid value in the value argument for __futex_wait, otherwise we may always get EAGAIN error. | 
 | 683 |  | 
 | 684 | #if defined(__LP64__) | 
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 685 |   return __futex_wait_ex(&mutex->state, shared, old_state, use_realtime_clock, abs_timeout); | 
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 686 |  | 
 | 687 | #else | 
 | 688 |   // This implementation works only when the layout of pthread_mutex_internal_t matches below expectation. | 
 | 689 |   // And it is based on the assumption that Android is always in little-endian devices. | 
 | 690 |   static_assert(offsetof(pthread_mutex_internal_t, state) == 0, ""); | 
 | 691 |   static_assert(offsetof(pthread_mutex_internal_t, owner_tid) == 2, ""); | 
 | 692 |  | 
 | 693 |   uint32_t owner_tid = atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed); | 
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 694 |   return __futex_wait_ex(&mutex->state, shared, (owner_tid << 16) | old_state, | 
 | 695 |                          use_realtime_clock, abs_timeout); | 
| Yabin Cui | f796985 | 2015-04-02 17:47:48 -0700 | [diff] [blame] | 696 | #endif | 
 | 697 | } | 
 | 698 |  | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 699 | // Lock a Non-PI mutex. | 
 | 700 | static int MutexLockWithTimeout(pthread_mutex_internal_t* mutex, bool use_realtime_clock, | 
 | 701 |                                 const timespec* abs_timeout_or_null) { | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 702 |     uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); | 
 | 703 |     uint16_t mtype = (old_state & MUTEX_TYPE_MASK); | 
 | 704 |     uint16_t shared = (old_state & MUTEX_SHARED_MASK); | 
| Fabrice Di Meglio | 8641833 | 2010-03-11 14:47:47 -0800 | [diff] [blame] | 705 |  | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 706 |     // Handle common case first. | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 707 |     if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) { | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 708 |         return NormalMutexLock(mutex, shared, use_realtime_clock, abs_timeout_or_null); | 
| David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 709 |     } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 710 |  | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 711 |     // Do we already own this recursive or error-check mutex? | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 712 |     pid_t tid = __get_thread()->tid; | 
 | 713 |     if (tid == atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed)) { | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 714 |         if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) { | 
 | 715 |             return EDEADLK; | 
 | 716 |         } | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 717 |         return RecursiveIncrement(mutex, old_state); | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 718 |     } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 719 |  | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 720 |     const uint16_t unlocked           = mtype | shared | MUTEX_STATE_BITS_UNLOCKED; | 
 | 721 |     const uint16_t locked_uncontended = mtype | shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; | 
 | 722 |     const uint16_t locked_contended   = mtype | shared | MUTEX_STATE_BITS_LOCKED_CONTENDED; | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 723 |  | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 724 |     // First, if the mutex is unlocked, try to quickly acquire it. | 
 | 725 |     // In the optimistic case where this works, set the state to locked_uncontended. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 726 |     if (old_state == unlocked) { | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 727 |         // If exchanged successfully, an acquire fence is required to make | 
 | 728 |         // all memory accesses made by other threads visible to the current CPU. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 729 |         if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state, | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 730 |                              locked_uncontended, memory_order_acquire, memory_order_relaxed))) { | 
 | 731 |             atomic_store_explicit(&mutex->owner_tid, tid, memory_order_relaxed); | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 732 |             return 0; | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 733 |         } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 734 |     } | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 735 |  | 
| Brigid Smith | a406ee6 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 736 |     ScopedTrace trace("Contending for pthread mutex"); | 
 | 737 |  | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 738 |     while (true) { | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 739 |         if (old_state == unlocked) { | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 740 |             // NOTE: We put the state to locked_contended since we _know_ there | 
 | 741 |             // is contention when we are in this loop. This ensures all waiters | 
 | 742 |             // will be unlocked. | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 743 |  | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 744 |             // If exchanged successfully, an acquire fence is required to make | 
 | 745 |             // all memory accesses made by other threads visible to the current CPU. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 746 |             if (__predict_true(atomic_compare_exchange_weak_explicit(&mutex->state, | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 747 |                                                                      &old_state, locked_contended, | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 748 |                                                                      memory_order_acquire, | 
 | 749 |                                                                      memory_order_relaxed))) { | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 750 |                 atomic_store_explicit(&mutex->owner_tid, tid, memory_order_relaxed); | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 751 |                 return 0; | 
 | 752 |             } | 
 | 753 |             continue; | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 754 |         } else if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(old_state)) { | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 755 |             // We should set it to locked_contended beforing going to sleep. This can make | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 756 |             // sure waiters will be woken up eventually. | 
 | 757 |  | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 758 |             int new_state = MUTEX_STATE_BITS_FLIP_CONTENTION(old_state); | 
 | 759 |             if (__predict_false(!atomic_compare_exchange_weak_explicit(&mutex->state, | 
 | 760 |                                                                        &old_state, new_state, | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 761 |                                                                        memory_order_relaxed, | 
 | 762 |                                                                        memory_order_relaxed))) { | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 763 |                 continue; | 
 | 764 |             } | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 765 |             old_state = new_state; | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 766 |         } | 
 | 767 |  | 
| Elliott Hughes | dd586f2 | 2015-12-16 15:15:58 -0800 | [diff] [blame] | 768 |         int result = check_timespec(abs_timeout_or_null, true); | 
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 769 |         if (result != 0) { | 
 | 770 |             return result; | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 771 |         } | 
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 772 |         // We are in locked_contended state, sleep until someone wakes us up. | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 773 |         if (RecursiveOrErrorcheckMutexWait(mutex, shared, old_state, use_realtime_clock, | 
 | 774 |                                            abs_timeout_or_null) == -ETIMEDOUT) { | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 775 |             return ETIMEDOUT; | 
 | 776 |         } | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 777 |         old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 778 |     } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 779 | } | 
 | 780 |  | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 781 | }  // namespace NonPI | 
 | 782 |  | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 783 | static inline __always_inline bool IsMutexDestroyed(uint16_t mutex_state) { | 
 | 784 |     return mutex_state == 0xffff; | 
 | 785 | } | 
 | 786 |  | 
 | 787 | // Inlining this function in pthread_mutex_lock() adds the cost of stack frame instructions on | 
 | 788 | // ARM64. So make it noinline. | 
 | 789 | static int __attribute__((noinline)) HandleUsingDestroyedMutex(pthread_mutex_t* mutex, | 
 | 790 |                                                                const char* function_name) { | 
| Elliott Hughes | c0f4656 | 2018-11-09 15:38:52 -0800 | [diff] [blame] | 791 |     if (android_get_application_target_sdk_version() >= __ANDROID_API_P__) { | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 792 |         __fortify_fatal("%s called on a destroyed mutex (%p)", function_name, mutex); | 
 | 793 |     } | 
 | 794 |     return EBUSY; | 
 | 795 | } | 
 | 796 |  | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 797 | int pthread_mutex_lock(pthread_mutex_t* mutex_interface) { | 
| Christopher Ferris | 511cfd9 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 798 | #if !defined(__LP64__) | 
| Dan Albert | baa2a97 | 2015-08-13 16:58:50 -0700 | [diff] [blame] | 799 |     // Some apps depend on being able to pass NULL as a mutex and get EINVAL | 
 | 800 |     // back. Don't need to worry about it for LP64 since the ABI is brand new, | 
 | 801 |     // but keep compatibility for LP32. http://b/19995172. | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 802 |     if (mutex_interface == nullptr) { | 
| Christopher Ferris | 511cfd9 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 803 |         return EINVAL; | 
 | 804 |     } | 
 | 805 | #endif | 
 | 806 |  | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 807 |     pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 808 |     uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); | 
 | 809 |     uint16_t mtype = (old_state & MUTEX_TYPE_MASK); | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 810 |     // Avoid slowing down fast path of normal mutex lock operation. | 
 | 811 |     if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) { | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 812 |         uint16_t shared = (old_state & MUTEX_SHARED_MASK); | 
 | 813 |         if (__predict_true(NonPI::NormalMutexTryLock(mutex, shared) == 0)) { | 
 | 814 |             return 0; | 
 | 815 |         } | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 816 |     } | 
 | 817 |     if (old_state == PI_MUTEX_STATE) { | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 818 |         PIMutex& m = mutex->ToPIMutex(); | 
 | 819 |         // Handle common case first. | 
 | 820 |         if (__predict_true(PIMutexTryLock(m) == 0)) { | 
 | 821 |             return 0; | 
 | 822 |         } | 
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 823 |         return PIMutexTimedLock(mutex->ToPIMutex(), false, nullptr); | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 824 |     } | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 825 |     if (__predict_false(IsMutexDestroyed(old_state))) { | 
 | 826 |         return HandleUsingDestroyedMutex(mutex_interface, __FUNCTION__); | 
 | 827 |     } | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 828 |     return NonPI::MutexLockWithTimeout(mutex, false, nullptr); | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 829 | } | 
 | 830 |  | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 831 | int pthread_mutex_unlock(pthread_mutex_t* mutex_interface) { | 
| Christopher Ferris | 511cfd9 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 832 | #if !defined(__LP64__) | 
| Dan Albert | baa2a97 | 2015-08-13 16:58:50 -0700 | [diff] [blame] | 833 |     // Some apps depend on being able to pass NULL as a mutex and get EINVAL | 
 | 834 |     // back. Don't need to worry about it for LP64 since the ABI is brand new, | 
 | 835 |     // but keep compatibility for LP32. http://b/19995172. | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 836 |     if (mutex_interface == nullptr) { | 
| Christopher Ferris | 511cfd9 | 2015-06-09 18:46:15 -0700 | [diff] [blame] | 837 |         return EINVAL; | 
 | 838 |     } | 
 | 839 | #endif | 
 | 840 |  | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 841 |     pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 842 |     uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); | 
 | 843 |     uint16_t mtype  = (old_state & MUTEX_TYPE_MASK); | 
 | 844 |     uint16_t shared = (old_state & MUTEX_SHARED_MASK); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 845 |  | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 846 |     // Handle common case first. | 
| Elliott Hughes | d4e753f | 2013-07-16 12:45:46 -0700 | [diff] [blame] | 847 |     if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) { | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 848 |         NonPI::NormalMutexUnlock(mutex, shared); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 849 |         return 0; | 
 | 850 |     } | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 851 |     if (old_state == PI_MUTEX_STATE) { | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 852 |         return PIMutexUnlock(mutex->ToPIMutex()); | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 853 |     } | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 854 |     if (__predict_false(IsMutexDestroyed(old_state))) { | 
 | 855 |         return HandleUsingDestroyedMutex(mutex_interface, __FUNCTION__); | 
 | 856 |     } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 857 |  | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 858 |     // Do we already own this recursive or error-check mutex? | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 859 |     pid_t tid = __get_thread()->tid; | 
 | 860 |     if ( tid != atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed) ) { | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 861 |         return EPERM; | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 862 |     } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 863 |  | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 864 |     // If the counter is > 0, we can simply decrement it atomically. | 
 | 865 |     // Since other threads can mutate the lower state bits (and only the | 
 | 866 |     // lower state bits), use a compare_exchange loop to do it. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 867 |     if (!MUTEX_COUNTER_BITS_IS_ZERO(old_state)) { | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 868 |         // We still own the mutex, so a release fence is not needed. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 869 |         atomic_fetch_sub_explicit(&mutex->state, MUTEX_COUNTER_BITS_ONE, memory_order_relaxed); | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 870 |         return 0; | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 871 |     } | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 872 |  | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 873 |     // The counter is 0, so we'are going to unlock the mutex by resetting its | 
 | 874 |     // state to unlocked, we need to perform a atomic_exchange inorder to read | 
 | 875 |     // the current state, which will be locked_contended if there may have waiters | 
 | 876 |     // to awake. | 
 | 877 |     // A release fence is required to make previous stores visible to next | 
 | 878 |     // lock owner threads. | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 879 |     atomic_store_explicit(&mutex->owner_tid, 0, memory_order_relaxed); | 
 | 880 |     const uint16_t unlocked = mtype | shared | MUTEX_STATE_BITS_UNLOCKED; | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 881 |     old_state = atomic_exchange_explicit(&mutex->state, unlocked, memory_order_release); | 
 | 882 |     if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(old_state)) { | 
 | 883 |         __futex_wake_ex(&mutex->state, shared, 1); | 
| David 'Digit' Turner | 88f06cd | 2010-03-18 17:13:41 -0700 | [diff] [blame] | 884 |     } | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 885 |  | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 886 |     return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 887 | } | 
 | 888 |  | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 889 | int pthread_mutex_trylock(pthread_mutex_t* mutex_interface) { | 
 | 890 |     pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 891 |  | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 892 |     uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); | 
 | 893 |     uint16_t mtype  = (old_state & MUTEX_TYPE_MASK); | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 894 |  | 
| Elliott Hughes | 5b1111a | 2014-10-24 19:33:11 -0700 | [diff] [blame] | 895 |     // Handle common case first. | 
 | 896 |     if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) { | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 897 |         uint16_t shared = (old_state & MUTEX_SHARED_MASK); | 
 | 898 |         return NonPI::NormalMutexTryLock(mutex, shared); | 
| David 'Digit' Turner | ba9c6f0 | 2010-03-10 16:44:08 -0800 | [diff] [blame] | 899 |     } | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 900 |     if (old_state == PI_MUTEX_STATE) { | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 901 |         return PIMutexTryLock(mutex->ToPIMutex()); | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 902 |     } | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 903 |     if (__predict_false(IsMutexDestroyed(old_state))) { | 
 | 904 |         return HandleUsingDestroyedMutex(mutex_interface, __FUNCTION__); | 
 | 905 |     } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 906 |  | 
| Elliott Hughes | 5b1111a | 2014-10-24 19:33:11 -0700 | [diff] [blame] | 907 |     // Do we already own this recursive or error-check mutex? | 
 | 908 |     pid_t tid = __get_thread()->tid; | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 909 |     if (tid == atomic_load_explicit(&mutex->owner_tid, memory_order_relaxed)) { | 
| Elliott Hughes | 5b1111a | 2014-10-24 19:33:11 -0700 | [diff] [blame] | 910 |         if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) { | 
 | 911 |             return EBUSY; | 
 | 912 |         } | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 913 |         return NonPI::RecursiveIncrement(mutex, old_state); | 
| Elliott Hughes | 5b1111a | 2014-10-24 19:33:11 -0700 | [diff] [blame] | 914 |     } | 
| David 'Digit' Turner | 40e6b82 | 2010-03-17 11:25:46 -0700 | [diff] [blame] | 915 |  | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 916 |     uint16_t shared = (old_state & MUTEX_SHARED_MASK); | 
 | 917 |     const uint16_t unlocked           = mtype | shared | MUTEX_STATE_BITS_UNLOCKED; | 
 | 918 |     const uint16_t locked_uncontended = mtype | shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED; | 
 | 919 |  | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 920 |     // Same as pthread_mutex_lock, except that we don't want to wait, and | 
 | 921 |     // the only operation that can succeed is a single compare_exchange to acquire the | 
 | 922 |     // lock if it is released / not owned by anyone. No need for a complex loop. | 
| Yabin Cui | 5b8e7cd | 2015-03-04 17:36:59 -0800 | [diff] [blame] | 923 |     // If exchanged successfully, an acquire fence is required to make | 
 | 924 |     // all memory accesses made by other threads visible to the current CPU. | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 925 |     old_state = unlocked; | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 926 |     if (__predict_true(atomic_compare_exchange_strong_explicit(&mutex->state, &old_state, | 
 | 927 |                                                                locked_uncontended, | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 928 |                                                                memory_order_acquire, | 
 | 929 |                                                                memory_order_relaxed))) { | 
| Yabin Cui | e69c245 | 2015-02-13 16:21:25 -0800 | [diff] [blame] | 930 |         atomic_store_explicit(&mutex->owner_tid, tid, memory_order_relaxed); | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 931 |         return 0; | 
 | 932 |     } | 
| David 'Digit' Turner | e1414aa | 2012-01-24 15:26:54 +0100 | [diff] [blame] | 933 |     return EBUSY; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 934 | } | 
 | 935 |  | 
| Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 936 | #if !defined(__LP64__) | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 937 | extern "C" int pthread_mutex_lock_timeout_np(pthread_mutex_t* mutex_interface, unsigned ms) { | 
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 938 |     timespec ts; | 
 | 939 |     timespec_from_ms(ts, ms); | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 940 |     timespec abs_timeout; | 
| Yabin Cui | c9a659c | 2015-11-05 15:36:08 -0800 | [diff] [blame] | 941 |     absolute_timespec_from_timespec(abs_timeout, ts, CLOCK_MONOTONIC); | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 942 |     int error = NonPI::MutexLockWithTimeout(__get_internal_mutex(mutex_interface), false, | 
 | 943 |                                             &abs_timeout); | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 944 |     if (error == ETIMEDOUT) { | 
 | 945 |         error = EBUSY; | 
 | 946 |     } | 
 | 947 |     return error; | 
| Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame] | 948 | } | 
 | 949 | #endif | 
 | 950 |  | 
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 951 | static int __pthread_mutex_timedlock(pthread_mutex_t* mutex_interface, bool use_realtime_clock, | 
 | 952 |                                      const timespec* abs_timeout, const char* function) { | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 953 |     pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); | 
 | 954 |     uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); | 
 | 955 |     uint16_t mtype = (old_state & MUTEX_TYPE_MASK); | 
 | 956 |     // Handle common case first. | 
 | 957 |     if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) { | 
 | 958 |         uint16_t shared = (old_state & MUTEX_SHARED_MASK); | 
 | 959 |         if (__predict_true(NonPI::NormalMutexTryLock(mutex, shared) == 0)) { | 
 | 960 |             return 0; | 
 | 961 |         } | 
 | 962 |     } | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 963 |     if (old_state == PI_MUTEX_STATE) { | 
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 964 |         return PIMutexTimedLock(mutex->ToPIMutex(), use_realtime_clock, abs_timeout); | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 965 |     } | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 966 |     if (__predict_false(IsMutexDestroyed(old_state))) { | 
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 967 |         return HandleUsingDestroyedMutex(mutex_interface, function); | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 968 |     } | 
| Tom Cherry | c6b5bcd | 2018-03-05 14:14:44 -0800 | [diff] [blame] | 969 |     return NonPI::MutexLockWithTimeout(mutex, use_realtime_clock, abs_timeout); | 
 | 970 | } | 
 | 971 |  | 
 | 972 | int pthread_mutex_timedlock(pthread_mutex_t* mutex_interface, const struct timespec* abs_timeout) { | 
 | 973 |     return __pthread_mutex_timedlock(mutex_interface, true, abs_timeout, __FUNCTION__); | 
 | 974 | } | 
 | 975 |  | 
 | 976 | int pthread_mutex_timedlock_monotonic_np(pthread_mutex_t* mutex_interface, | 
 | 977 |                                          const struct timespec* abs_timeout) { | 
 | 978 |     return __pthread_mutex_timedlock(mutex_interface, false, abs_timeout, __FUNCTION__); | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 979 | } | 
 | 980 |  | 
| Yabin Cui | 17393b0 | 2015-03-21 15:08:25 -0700 | [diff] [blame] | 981 | int pthread_mutex_destroy(pthread_mutex_t* mutex_interface) { | 
| Yabin Cui | 0307eee | 2015-11-16 20:19:31 -0800 | [diff] [blame] | 982 |     pthread_mutex_internal_t* mutex = __get_internal_mutex(mutex_interface); | 
 | 983 |     uint16_t old_state = atomic_load_explicit(&mutex->state, memory_order_relaxed); | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 984 |     if (__predict_false(IsMutexDestroyed(old_state))) { | 
 | 985 |         return HandleUsingDestroyedMutex(mutex_interface, __FUNCTION__); | 
| Yabin Cui | 2dec3d7 | 2018-02-02 15:45:24 -0800 | [diff] [blame] | 986 |     } | 
| Yabin Cui | 9651fdf | 2018-03-14 12:02:21 -0700 | [diff] [blame] | 987 |     if (old_state == PI_MUTEX_STATE) { | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 988 |         int result = PIMutexDestroy(mutex->ToPIMutex()); | 
 | 989 |         if (result == 0) { | 
 | 990 |             mutex->FreePIMutex(); | 
| Yabin Cui | 2dec3d7 | 2018-02-02 15:45:24 -0800 | [diff] [blame] | 991 |             atomic_store(&mutex->state, 0xffff); | 
| Yabin Cui | 5a00ba7 | 2018-01-26 17:32:31 -0800 | [diff] [blame] | 992 |         } | 
 | 993 |         return result; | 
| Yabin Cui | 6b9c85b | 2018-01-23 12:56:18 -0800 | [diff] [blame] | 994 |     } | 
| Yabin Cui | 0307eee | 2015-11-16 20:19:31 -0800 | [diff] [blame] | 995 |     // Store 0xffff to make the mutex unusable. Although POSIX standard says it is undefined | 
 | 996 |     // behavior to destroy a locked mutex, we prefer not to change mutex->state in that situation. | 
 | 997 |     if (MUTEX_STATE_BITS_IS_UNLOCKED(old_state) && | 
 | 998 |         atomic_compare_exchange_strong_explicit(&mutex->state, &old_state, 0xffff, | 
 | 999 |                                                 memory_order_relaxed, memory_order_relaxed)) { | 
 | 1000 |       return 0; | 
| Yabin Cui | 86fc96f | 2015-01-29 21:50:48 -0800 | [diff] [blame] | 1001 |     } | 
| Yabin Cui | 0307eee | 2015-11-16 20:19:31 -0800 | [diff] [blame] | 1002 |     return EBUSY; | 
| Mathias Agopian | 7c0c379 | 2011-09-05 23:54:55 -0700 | [diff] [blame] | 1003 | } |