David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | */ |
| 28 | |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 29 | #include <errno.h> |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 30 | #include <sys/atomics.h> |
| 31 | |
| 32 | #include "pthread_internal.h" |
| 33 | #include "private/bionic_futex.h" |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 34 | |
| 35 | /* Technical note: |
| 36 | * |
| 37 | * Possible states of a read/write lock: |
| 38 | * |
| 39 | * - no readers and no writer (unlocked) |
| 40 | * - one or more readers sharing the lock at the same time (read-locked) |
| 41 | * - one writer holding the lock (write-lock) |
| 42 | * |
| 43 | * Additionally: |
| 44 | * - trying to get the write-lock while there are any readers blocks |
| 45 | * - trying to get the read-lock while there is a writer blocks |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 46 | * - a single thread can acquire the lock multiple times in read mode |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 47 | * |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 48 | * - Posix states that behavior is undefined (may deadlock) if a thread tries |
| 49 | * to acquire the lock |
| 50 | * - in write mode while already holding the lock (whether in read or write mode) |
| 51 | * - in read mode while already holding the lock in write mode. |
| 52 | * - This implementation will return EDEADLK in "write after write" and "read after |
| 53 | * write" cases and will deadlock in write after read case. |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 54 | * |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 55 | * TODO: VERY CAREFULLY convert this to use C++11 atomics when possible. All volatile |
| 56 | * members of pthread_rwlock_t should be converted to atomics<> and __atomic_cmpxchg |
| 57 | * should be changed to compare_exchange_strong accompanied by the proper ordering |
| 58 | * constraints (comments have been added with the intending ordering across the code). |
| 59 | * |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 60 | * TODO: As it stands now, pending_readers and pending_writers could be merged into a |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 61 | * a single waiters variable. Keeping them separate adds a bit of clarity and keeps |
| 62 | * the door open for a writer-biased implementation. |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 63 | * |
| 64 | */ |
| 65 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 66 | #define RWLOCKATTR_DEFAULT 0 |
| 67 | #define RWLOCKATTR_SHARED_MASK 0x0010 |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 68 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 69 | static inline bool rwlock_is_shared(const pthread_rwlock_t* rwlock) { |
| 70 | return rwlock->attr == PTHREAD_PROCESS_SHARED; |
| 71 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 72 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 73 | static bool timespec_from_absolute(timespec* rel_timeout, const timespec* abs_timeout) { |
| 74 | if (abs_timeout != NULL) { |
| 75 | if (__timespec_from_absolute(rel_timeout, abs_timeout, CLOCK_REALTIME) < 0) { |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | return true; |
| 80 | } |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 81 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 82 | int pthread_rwlockattr_init(pthread_rwlockattr_t* attr) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 83 | *attr = PTHREAD_PROCESS_PRIVATE; |
| 84 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 87 | int pthread_rwlockattr_destroy(pthread_rwlockattr_t* attr) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 88 | *attr = -1; |
| 89 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 92 | int pthread_rwlockattr_setpshared(pthread_rwlockattr_t* attr, int pshared) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 93 | switch (pshared) { |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 94 | case PTHREAD_PROCESS_PRIVATE: |
| 95 | case PTHREAD_PROCESS_SHARED: |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 96 | *attr = pshared; |
| 97 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 98 | default: |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 99 | return EINVAL; |
| 100 | } |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 103 | int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t* attr, int* pshared) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 104 | *pshared = *attr; |
| 105 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 108 | int pthread_rwlock_init(pthread_rwlock_t* rwlock, const pthread_rwlockattr_t* attr) { |
| 109 | if (attr != NULL) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 110 | switch (*attr) { |
| 111 | case PTHREAD_PROCESS_SHARED: |
| 112 | case PTHREAD_PROCESS_PRIVATE: |
| 113 | rwlock->attr= *attr; |
| 114 | break; |
| 115 | default: |
| 116 | return EINVAL; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 117 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 118 | } |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 119 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 120 | rwlock->state = 0; |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 121 | rwlock->pending_readers = 0; |
| 122 | rwlock->pending_writers = 0; |
| 123 | rwlock->writer_thread_id = 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 124 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 125 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 128 | int pthread_rwlock_destroy(pthread_rwlock_t* rwlock) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 129 | if (rwlock->state != 0) { |
| 130 | return EBUSY; |
| 131 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 132 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 135 | static int __pthread_rwlock_timedrdlock(pthread_rwlock_t* rwlock, const timespec* abs_timeout) { |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 136 | if (__predict_false(__get_thread()->tid == rwlock->writer_thread_id)) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 137 | return EDEADLK; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 138 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 139 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 140 | timespec ts; |
| 141 | timespec* rel_timeout = (abs_timeout == NULL) ? NULL : &ts; |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 142 | bool done = false; |
| 143 | do { |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 144 | // This is actually a race read as there's nothing that guarantees the atomicity of integer |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 145 | // reads / writes. However, in practice this "never" happens so until we switch to C++11 this |
| 146 | // should work fine. The same applies in the other places this idiom is used. |
| 147 | int32_t cur_state = rwlock->state; // C++11 relaxed atomic read |
| 148 | if (__predict_true(cur_state >= 0)) { |
| 149 | // Add as an extra reader. |
| 150 | done = __atomic_cmpxchg(cur_state, cur_state + 1, &rwlock->state) == 0; // C++11 memory_order_aquire |
| 151 | } else { |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 152 | if (!timespec_from_absolute(rel_timeout, abs_timeout)) { |
| 153 | return ETIMEDOUT; |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 154 | } |
| 155 | // Owner holds it in write mode, hang up. |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 156 | // To avoid losing wake ups the pending_readers update and the state read should be |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 157 | // sequentially consistent. (currently enforced by __atomic_inc which creates a full barrier) |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 158 | __atomic_inc(&rwlock->pending_readers); // C++11 memory_order_relaxed (if the futex_wait ensures the ordering) |
| 159 | int ret = __futex_wait_ex(&rwlock->state, rwlock_is_shared(rwlock), cur_state, rel_timeout); |
| 160 | __atomic_dec(&rwlock->pending_readers); // C++11 memory_order_relaxed |
| 161 | if (ret == -ETIMEDOUT) { |
| 162 | return ETIMEDOUT; |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 163 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 164 | } |
| 165 | } while (!done); |
| 166 | |
| 167 | return 0; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static int __pthread_rwlock_timedwrlock(pthread_rwlock_t* rwlock, const timespec* abs_timeout) { |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 171 | int tid = __get_thread()->tid; |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 172 | if (__predict_false(tid == rwlock->writer_thread_id)) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 173 | return EDEADLK; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 174 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 175 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 176 | timespec ts; |
| 177 | timespec* rel_timeout = (abs_timeout == NULL) ? NULL : &ts; |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 178 | bool done = false; |
| 179 | do { |
| 180 | int32_t cur_state = rwlock->state; |
| 181 | if (__predict_true(cur_state == 0)) { |
| 182 | // Change state from 0 to -1. |
| 183 | done = __atomic_cmpxchg(0 /* cur_state */, -1 /* new state */, &rwlock->state) == 0; // C++11 memory_order_aquire |
| 184 | } else { |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 185 | if (!timespec_from_absolute(rel_timeout, abs_timeout)) { |
| 186 | return ETIMEDOUT; |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 187 | } |
| 188 | // Failed to acquire, hang up. |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 189 | // To avoid losing wake ups the pending_writers update and the state read should be |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 190 | // sequentially consistent. (currently enforced by __atomic_inc which creates a full barrier) |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 191 | __atomic_inc(&rwlock->pending_writers); // C++11 memory_order_relaxed (if the futex_wait ensures the ordering) |
| 192 | int ret = __futex_wait_ex(&rwlock->state, rwlock_is_shared(rwlock), cur_state, rel_timeout); |
| 193 | __atomic_dec(&rwlock->pending_writers); // C++11 memory_order_relaxed |
| 194 | if (ret == -ETIMEDOUT) { |
| 195 | return ETIMEDOUT; |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 196 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 197 | } |
| 198 | } while (!done); |
| 199 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 200 | rwlock->writer_thread_id = tid; |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 201 | return 0; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | int pthread_rwlock_rdlock(pthread_rwlock_t* rwlock) { |
| 205 | return __pthread_rwlock_timedrdlock(rwlock, NULL); |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 208 | int pthread_rwlock_timedrdlock(pthread_rwlock_t* rwlock, const timespec* abs_timeout) { |
| 209 | return __pthread_rwlock_timedrdlock(rwlock, abs_timeout); |
| 210 | } |
| 211 | |
| 212 | int pthread_rwlock_tryrdlock(pthread_rwlock_t* rwlock) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 213 | int32_t cur_state = rwlock->state; |
| 214 | if (cur_state >= 0) { |
| 215 | if(__atomic_cmpxchg(cur_state, cur_state + 1, &rwlock->state) != 0) { // C++11 memory_order_acquire |
| 216 | return EBUSY; |
| 217 | } |
| 218 | } else { |
| 219 | return EBUSY; |
| 220 | } |
| 221 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 224 | int pthread_rwlock_wrlock(pthread_rwlock_t* rwlock) { |
| 225 | return __pthread_rwlock_timedwrlock(rwlock, NULL); |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 228 | int pthread_rwlock_timedwrlock(pthread_rwlock_t* rwlock, const timespec* abs_timeout) { |
| 229 | return __pthread_rwlock_timedwrlock(rwlock, abs_timeout); |
| 230 | } |
| 231 | |
| 232 | int pthread_rwlock_trywrlock(pthread_rwlock_t* rwlock) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 233 | int tid = __get_thread()->tid; |
| 234 | int32_t cur_state = rwlock->state; |
| 235 | if (cur_state == 0) { |
| 236 | if(__atomic_cmpxchg(0, -1, &rwlock->state) != 0) { // C++11 memory_order_acquire |
| 237 | return EBUSY; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 238 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 239 | } else { |
| 240 | return EBUSY; |
| 241 | } |
| 242 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 243 | rwlock->writer_thread_id = tid; |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 244 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 245 | } |
| 246 | |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 247 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 248 | int pthread_rwlock_unlock(pthread_rwlock_t* rwlock) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 249 | int tid = __get_thread()->tid; |
| 250 | bool done = false; |
| 251 | do { |
| 252 | int32_t cur_state = rwlock->state; |
| 253 | if (cur_state == 0) { |
| 254 | return EPERM; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 255 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 256 | if (cur_state == -1) { |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 257 | if (rwlock->writer_thread_id != tid) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 258 | return EPERM; |
| 259 | } |
| 260 | // We're no longer the owner. |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 261 | rwlock->writer_thread_id = 0; |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 262 | // Change state from -1 to 0. |
| 263 | // We use __atomic_cmpxchg to achieve sequential consistency of the state store and |
| 264 | // the following pendingX loads. A simple store with memory_order_release semantics |
| 265 | // is not enough to guarantee that the pendingX loads are not reordered before the |
| 266 | // store (which may lead to a lost wakeup). |
| 267 | __atomic_cmpxchg(-1 /* cur_state*/, 0 /* new state */, &rwlock->state); // C++11 maybe memory_order_seq_cst? |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 268 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 269 | // Wake any waiters. |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 270 | if (__predict_false(rwlock->pending_readers > 0 || rwlock->pending_writers > 0)) { |
| 271 | __futex_wake_ex(&rwlock->state, rwlock_is_shared(rwlock), INT_MAX); |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 272 | } |
| 273 | done = true; |
| 274 | } else { // cur_state > 0 |
| 275 | // Reduce state by 1. |
| 276 | // See the above comment on why we need __atomic_cmpxchg. |
| 277 | done = __atomic_cmpxchg(cur_state, cur_state - 1, &rwlock->state) == 0; // C++11 maybe memory_order_seq_cst? |
| 278 | if (done && (cur_state - 1) == 0) { |
| 279 | // There are no more readers, wake any waiters. |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame^] | 280 | if (__predict_false(rwlock->pending_readers > 0 || rwlock->pending_writers > 0)) { |
| 281 | __futex_wake_ex(&rwlock->state, rwlock_is_shared(rwlock), INT_MAX); |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 282 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 283 | } |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 284 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 285 | } while (!done); |
| 286 | |
| 287 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 288 | } |