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> |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 30 | #include <stdatomic.h> |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 31 | |
| 32 | #include "pthread_internal.h" |
| 33 | #include "private/bionic_futex.h" |
Elliott Hughes | 04303f5 | 2014-09-18 16:11:59 -0700 | [diff] [blame] | 34 | #include "private/bionic_time_conversions.h" |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 35 | |
| 36 | /* Technical note: |
| 37 | * |
| 38 | * Possible states of a read/write lock: |
| 39 | * |
| 40 | * - no readers and no writer (unlocked) |
| 41 | * - one or more readers sharing the lock at the same time (read-locked) |
| 42 | * - one writer holding the lock (write-lock) |
| 43 | * |
| 44 | * Additionally: |
| 45 | * - trying to get the write-lock while there are any readers blocks |
| 46 | * - trying to get the read-lock while there is a writer blocks |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 47 | * - 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] | 48 | * |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 49 | * - Posix states that behavior is undefined (may deadlock) if a thread tries |
| 50 | * to acquire the lock |
| 51 | * - in write mode while already holding the lock (whether in read or write mode) |
| 52 | * - in read mode while already holding the lock in write mode. |
| 53 | * - This implementation will return EDEADLK in "write after write" and "read after |
| 54 | * write" cases and will deadlock in write after read case. |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 55 | * |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 56 | * 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] | 57 | * a single waiters variable. Keeping them separate adds a bit of clarity and keeps |
| 58 | * the door open for a writer-biased implementation. |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 59 | * |
| 60 | */ |
| 61 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 62 | #define RWLOCKATTR_DEFAULT 0 |
| 63 | #define RWLOCKATTR_SHARED_MASK 0x0010 |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 64 | |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 65 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 66 | int pthread_rwlockattr_init(pthread_rwlockattr_t* attr) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 67 | *attr = PTHREAD_PROCESS_PRIVATE; |
| 68 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 71 | int pthread_rwlockattr_destroy(pthread_rwlockattr_t* attr) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 72 | *attr = -1; |
| 73 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 76 | int pthread_rwlockattr_setpshared(pthread_rwlockattr_t* attr, int pshared) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 77 | switch (pshared) { |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 78 | case PTHREAD_PROCESS_PRIVATE: |
| 79 | case PTHREAD_PROCESS_SHARED: |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 80 | *attr = pshared; |
| 81 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 82 | default: |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 83 | return EINVAL; |
| 84 | } |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 87 | int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t* attr, int* pshared) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 88 | *pshared = *attr; |
| 89 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 92 | struct pthread_rwlock_internal_t { |
| 93 | atomic_int state; // 0=unlock, -1=writer lock, +n=reader lock |
| 94 | atomic_int writer_thread_id; |
| 95 | atomic_uint pending_readers; |
| 96 | atomic_uint pending_writers; |
| 97 | int32_t attr; |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 98 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 99 | bool process_shared() const { |
| 100 | return attr == PTHREAD_PROCESS_SHARED; |
| 101 | } |
| 102 | |
| 103 | #if defined(__LP64__) |
| 104 | char __reserved[36]; |
| 105 | #else |
| 106 | char __reserved[20]; |
| 107 | #endif |
| 108 | }; |
| 109 | |
Yabin Cui | b584572 | 2015-03-16 22:46:42 -0700 | [diff] [blame^] | 110 | static_assert(sizeof(pthread_rwlock_t) == sizeof(pthread_rwlock_internal_t), |
| 111 | "pthread_rwlock_t should actually be pthread_rwlock_internal_t in implementation."); |
| 112 | |
| 113 | // For binary compatibility with old version of pthread_rwlock_t, we can't use more strict |
| 114 | // alignment than 4-byte alignment. |
| 115 | static_assert(alignof(pthread_rwlock_t) == 4, |
| 116 | "pthread_rwlock_t should fulfill the alignment requirement of pthread_rwlock_internal_t."); |
| 117 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 118 | static inline pthread_rwlock_internal_t* __get_internal_rwlock(pthread_rwlock_t* rwlock_interface) { |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 119 | return reinterpret_cast<pthread_rwlock_internal_t*>(rwlock_interface); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 122 | int pthread_rwlock_init(pthread_rwlock_t* rwlock_interface, const pthread_rwlockattr_t* attr) { |
| 123 | pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 124 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 125 | if (__predict_true(attr == NULL)) { |
| 126 | rwlock->attr = 0; |
| 127 | } else { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 128 | switch (*attr) { |
| 129 | case PTHREAD_PROCESS_SHARED: |
| 130 | case PTHREAD_PROCESS_PRIVATE: |
| 131 | rwlock->attr= *attr; |
| 132 | break; |
| 133 | default: |
| 134 | return EINVAL; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 135 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 136 | } |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 137 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 138 | atomic_init(&rwlock->state, 0); |
| 139 | atomic_init(&rwlock->writer_thread_id, 0); |
| 140 | atomic_init(&rwlock->pending_readers, 0); |
| 141 | atomic_init(&rwlock->pending_writers, 0); |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 142 | |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 143 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 146 | int pthread_rwlock_destroy(pthread_rwlock_t* rwlock_interface) { |
| 147 | pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface); |
| 148 | |
| 149 | if (atomic_load_explicit(&rwlock->state, memory_order_relaxed) != 0) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 150 | return EBUSY; |
| 151 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 152 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 155 | static int __pthread_rwlock_timedrdlock(pthread_rwlock_internal_t* rwlock, |
| 156 | const timespec* abs_timeout_or_null) { |
| 157 | |
| 158 | if (__predict_false(__get_thread()->tid == atomic_load_explicit(&rwlock->writer_thread_id, |
| 159 | memory_order_relaxed))) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 160 | return EDEADLK; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 161 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 162 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 163 | while (true) { |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 164 | int old_state = atomic_load_explicit(&rwlock->state, memory_order_relaxed); |
| 165 | if (__predict_true(old_state >= 0)) { |
| 166 | if (atomic_compare_exchange_weak_explicit(&rwlock->state, &old_state, old_state + 1, |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 167 | memory_order_acquire, memory_order_relaxed)) { |
| 168 | return 0; |
| 169 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 170 | } else { |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 171 | timespec ts; |
| 172 | timespec* rel_timeout = NULL; |
| 173 | |
| 174 | if (abs_timeout_or_null != NULL) { |
| 175 | rel_timeout = &ts; |
| 176 | if (!timespec_from_absolute_timespec(*rel_timeout, *abs_timeout_or_null, CLOCK_REALTIME)) { |
| 177 | return ETIMEDOUT; |
| 178 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 179 | } |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 180 | |
| 181 | // To avoid losing wake ups, the pending_readers increment should be observed before |
| 182 | // futex_wait by all threads. A seq_cst fence instead of a seq_cst operation is used |
| 183 | // here. Because only a seq_cst fence can ensure sequential consistency for non-atomic |
| 184 | // operations in futex_wait. |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 185 | atomic_fetch_add_explicit(&rwlock->pending_readers, 1, memory_order_relaxed); |
| 186 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 187 | atomic_thread_fence(memory_order_seq_cst); |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 188 | |
| 189 | int ret = __futex_wait_ex(&rwlock->state, rwlock->process_shared(), old_state, |
| 190 | rel_timeout); |
| 191 | |
| 192 | atomic_fetch_sub_explicit(&rwlock->pending_readers, 1, memory_order_relaxed); |
| 193 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 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 | } |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 198 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 201 | static int __pthread_rwlock_timedwrlock(pthread_rwlock_internal_t* rwlock, |
| 202 | const timespec* abs_timeout_or_null) { |
| 203 | |
| 204 | if (__predict_false(__get_thread()->tid == atomic_load_explicit(&rwlock->writer_thread_id, |
| 205 | memory_order_relaxed))) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 206 | return EDEADLK; |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 207 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 208 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 209 | while (true) { |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 210 | int old_state = atomic_load_explicit(&rwlock->state, memory_order_relaxed); |
| 211 | if (__predict_true(old_state == 0)) { |
| 212 | if (atomic_compare_exchange_weak_explicit(&rwlock->state, &old_state, -1, |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 213 | memory_order_acquire, memory_order_relaxed)) { |
| 214 | // writer_thread_id is protected by rwlock and can only be modified in rwlock write |
| 215 | // owner thread. Other threads may read it for EDEADLK error checking, atomic operation |
| 216 | // is safe enough for it. |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 217 | atomic_store_explicit(&rwlock->writer_thread_id, __get_thread()->tid, memory_order_relaxed); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 218 | return 0; |
| 219 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 220 | } else { |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 221 | timespec ts; |
| 222 | timespec* rel_timeout = NULL; |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 223 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 224 | if (abs_timeout_or_null != NULL) { |
| 225 | rel_timeout = &ts; |
| 226 | if (!timespec_from_absolute_timespec(*rel_timeout, *abs_timeout_or_null, CLOCK_REALTIME)) { |
| 227 | return ETIMEDOUT; |
| 228 | } |
| 229 | } |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 230 | |
| 231 | // To avoid losing wake ups, the pending_writers increment should be observed before |
| 232 | // futex_wait by all threads. A seq_cst fence instead of a seq_cst operation is used |
| 233 | // here. Because only a seq_cst fence can ensure sequential consistency for non-atomic |
| 234 | // operations in futex_wait. |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 235 | atomic_fetch_add_explicit(&rwlock->pending_writers, 1, memory_order_relaxed); |
| 236 | |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 237 | atomic_thread_fence(memory_order_seq_cst); |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 238 | |
| 239 | int ret = __futex_wait_ex(&rwlock->state, rwlock->process_shared(), old_state, |
| 240 | rel_timeout); |
| 241 | |
| 242 | atomic_fetch_sub_explicit(&rwlock->pending_writers, 1, memory_order_relaxed); |
| 243 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 244 | if (ret == -ETIMEDOUT) { |
| 245 | return ETIMEDOUT; |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 246 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 247 | } |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 248 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 251 | int pthread_rwlock_rdlock(pthread_rwlock_t* rwlock_interface) { |
| 252 | pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface); |
| 253 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 254 | return __pthread_rwlock_timedrdlock(rwlock, NULL); |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 257 | int pthread_rwlock_timedrdlock(pthread_rwlock_t* rwlock_interface, const timespec* abs_timeout) { |
| 258 | pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface); |
| 259 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 260 | return __pthread_rwlock_timedrdlock(rwlock, abs_timeout); |
| 261 | } |
| 262 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 263 | int pthread_rwlock_tryrdlock(pthread_rwlock_t* rwlock_interface) { |
| 264 | pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 265 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 266 | int old_state = atomic_load_explicit(&rwlock->state, memory_order_relaxed); |
| 267 | |
| 268 | while (old_state >= 0 && !atomic_compare_exchange_weak_explicit(&rwlock->state, &old_state, |
| 269 | old_state + 1, memory_order_acquire, memory_order_relaxed)) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 270 | } |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 271 | return (old_state >= 0) ? 0 : EBUSY; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 274 | int pthread_rwlock_wrlock(pthread_rwlock_t* rwlock_interface) { |
| 275 | pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface); |
| 276 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 277 | return __pthread_rwlock_timedwrlock(rwlock, NULL); |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 280 | int pthread_rwlock_timedwrlock(pthread_rwlock_t* rwlock_interface, const timespec* abs_timeout) { |
| 281 | pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface); |
| 282 | |
Calin Juravle | 92687e4 | 2014-05-22 19:21:22 +0100 | [diff] [blame] | 283 | return __pthread_rwlock_timedwrlock(rwlock, abs_timeout); |
| 284 | } |
| 285 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 286 | int pthread_rwlock_trywrlock(pthread_rwlock_t* rwlock_interface) { |
| 287 | pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 288 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 289 | int old_state = atomic_load_explicit(&rwlock->state, memory_order_relaxed); |
| 290 | |
| 291 | while (old_state == 0 && !atomic_compare_exchange_weak_explicit(&rwlock->state, &old_state, -1, |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 292 | memory_order_acquire, memory_order_relaxed)) { |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 293 | } |
| 294 | if (old_state == 0) { |
| 295 | atomic_store_explicit(&rwlock->writer_thread_id, __get_thread()->tid, memory_order_relaxed); |
| 296 | return 0; |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 297 | } |
Calin Juravle | 1b676ea | 2014-05-23 00:15:10 +0100 | [diff] [blame] | 298 | return EBUSY; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 299 | } |
| 300 | |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 301 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 302 | int pthread_rwlock_unlock(pthread_rwlock_t* rwlock_interface) { |
| 303 | pthread_rwlock_internal_t* rwlock = __get_internal_rwlock(rwlock_interface); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 304 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 305 | int old_state = atomic_load_explicit(&rwlock->state, memory_order_relaxed); |
| 306 | if (__predict_false(old_state == 0)) { |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 307 | return EPERM; |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 308 | } else if (old_state == -1) { |
| 309 | if (atomic_load_explicit(&rwlock->writer_thread_id, memory_order_relaxed) != __get_thread()->tid) { |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 310 | return EPERM; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 311 | } |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 312 | // We're no longer the owner. |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 313 | atomic_store_explicit(&rwlock->writer_thread_id, 0, memory_order_relaxed); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 314 | // Change state from -1 to 0. |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 315 | atomic_store_explicit(&rwlock->state, 0, memory_order_release); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 316 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 317 | } else { // old_state > 0 |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 318 | // Reduce state by 1. |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 319 | while (old_state > 0 && !atomic_compare_exchange_weak_explicit(&rwlock->state, &old_state, |
| 320 | old_state - 1, memory_order_release, memory_order_relaxed)) { |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 321 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 322 | |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 323 | if (old_state <= 0) { |
| 324 | return EPERM; |
| 325 | } else if (old_state > 1) { |
| 326 | return 0; |
| 327 | } |
| 328 | // old_state = 1, which means the last reader calling unlock. It has to wake up waiters. |
| 329 | } |
| 330 | |
| 331 | // If having waiters, wake up them. |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 332 | // To avoid losing wake ups, the update of state should be observed before reading |
| 333 | // pending_readers/pending_writers by all threads. Use read locking as an example: |
| 334 | // read locking thread unlocking thread |
| 335 | // pending_readers++; state = 0; |
| 336 | // seq_cst fence seq_cst fence |
| 337 | // read state for futex_wait read pending_readers for futex_wake |
| 338 | // |
| 339 | // So when locking and unlocking threads are running in parallel, we will not get |
| 340 | // in a situation that the locking thread reads state as negative and needs to wait, |
| 341 | // while the unlocking thread reads pending_readers as zero and doesn't need to wake up waiters. |
| 342 | atomic_thread_fence(memory_order_seq_cst); |
Yabin Cui | 2fabea4 | 2015-03-13 14:22:05 -0700 | [diff] [blame] | 343 | if (__predict_false(atomic_load_explicit(&rwlock->pending_readers, memory_order_relaxed) > 0 || |
| 344 | atomic_load_explicit(&rwlock->pending_writers, memory_order_relaxed) > 0)) { |
| 345 | __futex_wake_ex(&rwlock->state, rwlock->process_shared(), INT_MAX); |
Yabin Cui | 08ee8d2 | 2015-02-11 17:04:36 -0800 | [diff] [blame] | 346 | } |
Calin Juravle | 76f352e | 2014-05-19 13:41:10 +0100 | [diff] [blame] | 347 | return 0; |
David 'Digit' Turner | a418c3b | 2010-05-11 16:39:22 -0700 | [diff] [blame] | 348 | } |