David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Konrad Grochowski | 9be4e68 | 2013-06-22 22:03:31 +0200 | [diff] [blame] | 20 | #include <thrift/thrift-config.h> |
| 21 | |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 22 | #include <thrift/Thrift.h> |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 23 | #include <thrift/concurrency/Mutex.h> |
| 24 | #include <thrift/concurrency/Util.h> |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 25 | |
| 26 | #include <assert.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 27 | #ifdef HAVE_PTHREAD_H |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 28 | #include <pthread.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 29 | #endif |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 30 | #include <signal.h> |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 31 | |
yunfang | 1454296 | 2007-10-03 22:59:41 +0000 | [diff] [blame] | 32 | using boost::shared_ptr; |
| 33 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 34 | namespace apache { |
| 35 | namespace thrift { |
| 36 | namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 37 | |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 38 | #ifndef THRIFT_NO_CONTENTION_PROFILING |
| 39 | |
| 40 | static sig_atomic_t mutexProfilingSampleRate = 0; |
| 41 | static MutexWaitCallback mutexProfilingCallback = 0; |
| 42 | |
| 43 | volatile static sig_atomic_t mutexProfilingCounter = 0; |
| 44 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 45 | void enableMutexProfiling(int32_t profilingSampleRate, MutexWaitCallback callback) { |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 46 | mutexProfilingSampleRate = profilingSampleRate; |
| 47 | mutexProfilingCallback = callback; |
| 48 | } |
| 49 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 50 | #define PROFILE_MUTEX_START_LOCK() int64_t _lock_startTime = maybeGetProfilingStartTime(); |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 51 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 52 | #define PROFILE_MUTEX_NOT_LOCKED() \ |
| 53 | do { \ |
| 54 | if (_lock_startTime > 0) { \ |
| 55 | int64_t endTime = Util::currentTimeUsec(); \ |
| 56 | (*mutexProfilingCallback)(this, endTime - _lock_startTime); \ |
| 57 | } \ |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 58 | } while (0) |
| 59 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 60 | #define PROFILE_MUTEX_LOCKED() \ |
| 61 | do { \ |
| 62 | profileTime_ = _lock_startTime; \ |
| 63 | if (profileTime_ > 0) { \ |
| 64 | profileTime_ = Util::currentTimeUsec() - profileTime_; \ |
| 65 | } \ |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 66 | } while (0) |
| 67 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 68 | #define PROFILE_MUTEX_START_UNLOCK() \ |
| 69 | int64_t _temp_profileTime = profileTime_; \ |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 70 | profileTime_ = 0; |
| 71 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 72 | #define PROFILE_MUTEX_UNLOCKED() \ |
| 73 | do { \ |
| 74 | if (_temp_profileTime > 0) { \ |
| 75 | (*mutexProfilingCallback)(this, _temp_profileTime); \ |
| 76 | } \ |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 77 | } while (0) |
| 78 | |
| 79 | static inline int64_t maybeGetProfilingStartTime() { |
| 80 | if (mutexProfilingSampleRate && mutexProfilingCallback) { |
| 81 | // This block is unsynchronized, but should produce a reasonable sampling |
| 82 | // rate on most architectures. The main race conditions are the gap |
| 83 | // between the decrement and the test, the non-atomicity of decrement, and |
| 84 | // potential caching of different values at different CPUs. |
| 85 | // |
| 86 | // - if two decrements race, the likeliest result is that the counter |
| 87 | // decrements slowly (perhaps much more slowly) than intended. |
| 88 | // |
| 89 | // - many threads could potentially decrement before resetting the counter |
| 90 | // to its large value, causing each additional incoming thread to |
| 91 | // profile every call. This situation is unlikely to persist for long |
| 92 | // as the critical gap is quite short, but profiling could be bursty. |
| 93 | sig_atomic_t localValue = --mutexProfilingCounter; |
| 94 | if (localValue <= 0) { |
| 95 | mutexProfilingCounter = mutexProfilingSampleRate; |
| 96 | return Util::currentTimeUsec(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | #else |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 104 | #define PROFILE_MUTEX_START_LOCK() |
| 105 | #define PROFILE_MUTEX_NOT_LOCKED() |
| 106 | #define PROFILE_MUTEX_LOCKED() |
| 107 | #define PROFILE_MUTEX_START_UNLOCK() |
| 108 | #define PROFILE_MUTEX_UNLOCKED() |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 109 | #endif // THRIFT_NO_CONTENTION_PROFILING |
| 110 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 111 | /** |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 112 | * Implementation of Mutex class using POSIX mutex |
| 113 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 114 | * @version $Id:$ |
| 115 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 116 | class Mutex::impl { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 117 | public: |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 118 | impl(Initializer init) : initialized_(false) { |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 119 | #ifndef THRIFT_NO_CONTENTION_PROFILING |
| 120 | profileTime_ = 0; |
| 121 | #endif |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 122 | init(&pthread_mutex_); |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 123 | initialized_ = true; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | ~impl() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 127 | if (initialized_) { |
| 128 | initialized_ = false; |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 129 | int ret = pthread_mutex_destroy(&pthread_mutex_); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 130 | THRIFT_UNUSED_VARIABLE(ret); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 131 | assert(ret == 0); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 135 | void lock() const { |
| 136 | PROFILE_MUTEX_START_LOCK(); |
| 137 | pthread_mutex_lock(&pthread_mutex_); |
| 138 | PROFILE_MUTEX_LOCKED(); |
| 139 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 140 | |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 141 | bool trylock() const { return (0 == pthread_mutex_trylock(&pthread_mutex_)); } |
| 142 | |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 143 | bool timedlock(int64_t milliseconds) const { |
David Reiss | 318a328 | 2010-03-22 02:34:57 +0000 | [diff] [blame] | 144 | #if defined(_POSIX_TIMEOUTS) && _POSIX_TIMEOUTS >= 200112L |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 145 | PROFILE_MUTEX_START_LOCK(); |
| 146 | |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 147 | struct THRIFT_TIMESPEC ts; |
Roger Meier | f9f841d | 2012-06-19 20:42:33 +0000 | [diff] [blame] | 148 | Util::toTimespec(ts, milliseconds + Util::currentTime()); |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 149 | int ret = pthread_mutex_timedlock(&pthread_mutex_, &ts); |
| 150 | if (ret == 0) { |
| 151 | PROFILE_MUTEX_LOCKED(); |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | PROFILE_MUTEX_NOT_LOCKED(); |
| 156 | return false; |
David Reiss | 318a328 | 2010-03-22 02:34:57 +0000 | [diff] [blame] | 157 | #else |
Roger Meier | f9f841d | 2012-06-19 20:42:33 +0000 | [diff] [blame] | 158 | /* Otherwise follow solution used by Mono for Android */ |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 159 | struct THRIFT_TIMESPEC sleepytime, now, to; |
Roger Meier | f9f841d | 2012-06-19 20:42:33 +0000 | [diff] [blame] | 160 | |
| 161 | /* This is just to avoid a completely busy wait */ |
| 162 | sleepytime.tv_sec = 0; |
| 163 | sleepytime.tv_nsec = 10000000L; /* 10ms */ |
| 164 | |
| 165 | Util::toTimespec(to, milliseconds + Util::currentTime()); |
| 166 | |
| 167 | while ((trylock()) == false) { |
| 168 | Util::toTimespec(now, Util::currentTime()); |
| 169 | if (now.tv_sec >= to.tv_sec && now.tv_nsec >= to.tv_nsec) { |
| 170 | return false; |
| 171 | } |
| 172 | nanosleep(&sleepytime, NULL); |
| 173 | } |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 174 | |
Roger Meier | f9f841d | 2012-06-19 20:42:33 +0000 | [diff] [blame] | 175 | return true; |
David Reiss | 318a328 | 2010-03-22 02:34:57 +0000 | [diff] [blame] | 176 | #endif |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 177 | } |
| 178 | |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 179 | void unlock() const { |
| 180 | PROFILE_MUTEX_START_UNLOCK(); |
| 181 | pthread_mutex_unlock(&pthread_mutex_); |
| 182 | PROFILE_MUTEX_UNLOCKED(); |
| 183 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 184 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 185 | void* getUnderlyingImpl() const { return (void*)&pthread_mutex_; } |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 186 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 187 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 188 | mutable pthread_mutex_t pthread_mutex_; |
| 189 | mutable bool initialized_; |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 190 | #ifndef THRIFT_NO_CONTENTION_PROFILING |
| 191 | mutable int64_t profileTime_; |
| 192 | #endif |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 195 | Mutex::Mutex(Initializer init) : impl_(new Mutex::impl(init)) { |
| 196 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 197 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 198 | void* Mutex::getUnderlyingImpl() const { |
| 199 | return impl_->getUnderlyingImpl(); |
| 200 | } |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 201 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 202 | void Mutex::lock() const { |
| 203 | impl_->lock(); |
| 204 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 205 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 206 | bool Mutex::trylock() const { |
| 207 | return impl_->trylock(); |
| 208 | } |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 209 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 210 | bool Mutex::timedlock(int64_t ms) const { |
| 211 | return impl_->timedlock(ms); |
| 212 | } |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 213 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 214 | void Mutex::unlock() const { |
| 215 | impl_->unlock(); |
| 216 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 217 | |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 218 | void Mutex::DEFAULT_INITIALIZER(void* arg) { |
| 219 | pthread_mutex_t* pthread_mutex = (pthread_mutex_t*)arg; |
| 220 | int ret = pthread_mutex_init(pthread_mutex, NULL); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 221 | THRIFT_UNUSED_VARIABLE(ret); |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 222 | assert(ret == 0); |
| 223 | } |
| 224 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 225 | #if defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) \ |
| 226 | || defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP) |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 227 | static void init_with_kind(pthread_mutex_t* mutex, int kind) { |
| 228 | pthread_mutexattr_t mutexattr; |
| 229 | int ret = pthread_mutexattr_init(&mutexattr); |
| 230 | assert(ret == 0); |
| 231 | |
| 232 | // Apparently, this can fail. Should we really be aborting? |
| 233 | ret = pthread_mutexattr_settype(&mutexattr, kind); |
| 234 | assert(ret == 0); |
| 235 | |
| 236 | ret = pthread_mutex_init(mutex, &mutexattr); |
| 237 | assert(ret == 0); |
| 238 | |
| 239 | ret = pthread_mutexattr_destroy(&mutexattr); |
| 240 | assert(ret == 0); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 241 | THRIFT_UNUSED_VARIABLE(ret); |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 242 | } |
Roger Meier | 178f8f2 | 2010-10-25 12:36:04 +0000 | [diff] [blame] | 243 | #endif |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 244 | |
| 245 | #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP |
| 246 | void Mutex::ADAPTIVE_INITIALIZER(void* arg) { |
| 247 | // From mysql source: mysys/my_thr_init.c |
| 248 | // Set mutex type to "fast" a.k.a "adaptive" |
| 249 | // |
| 250 | // In this case the thread may steal the mutex from some other thread |
| 251 | // that is waiting for the same mutex. This will save us some |
| 252 | // context switches but may cause a thread to 'starve forever' while |
| 253 | // waiting for the mutex (not likely if the code within the mutex is |
| 254 | // short). |
| 255 | init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_ADAPTIVE_NP); |
| 256 | } |
| 257 | #endif |
| 258 | |
| 259 | #ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP |
| 260 | void Mutex::RECURSIVE_INITIALIZER(void* arg) { |
| 261 | init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE_NP); |
| 262 | } |
| 263 | #endif |
| 264 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 265 | /** |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 266 | * Implementation of ReadWriteMutex class using POSIX rw lock |
| 267 | * |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 268 | * @version $Id:$ |
| 269 | */ |
| 270 | class ReadWriteMutex::impl { |
| 271 | public: |
| 272 | impl() : initialized_(false) { |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 273 | #ifndef THRIFT_NO_CONTENTION_PROFILING |
| 274 | profileTime_ = 0; |
| 275 | #endif |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 276 | int ret = pthread_rwlock_init(&rw_lock_, NULL); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 277 | THRIFT_UNUSED_VARIABLE(ret); |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 278 | assert(ret == 0); |
| 279 | initialized_ = true; |
| 280 | } |
| 281 | |
| 282 | ~impl() { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 283 | if (initialized_) { |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 284 | initialized_ = false; |
| 285 | int ret = pthread_rwlock_destroy(&rw_lock_); |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 286 | THRIFT_UNUSED_VARIABLE(ret); |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 287 | assert(ret == 0); |
| 288 | } |
| 289 | } |
| 290 | |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 291 | void acquireRead() const { |
| 292 | PROFILE_MUTEX_START_LOCK(); |
| 293 | pthread_rwlock_rdlock(&rw_lock_); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 294 | PROFILE_MUTEX_NOT_LOCKED(); // not exclusive, so use not-locked path |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 295 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 296 | |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 297 | void acquireWrite() const { |
| 298 | PROFILE_MUTEX_START_LOCK(); |
| 299 | pthread_rwlock_wrlock(&rw_lock_); |
| 300 | PROFILE_MUTEX_LOCKED(); |
| 301 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 302 | |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 303 | bool attemptRead() const { return !pthread_rwlock_tryrdlock(&rw_lock_); } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 304 | |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 305 | bool attemptWrite() const { return !pthread_rwlock_trywrlock(&rw_lock_); } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 306 | |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 307 | void release() const { |
| 308 | PROFILE_MUTEX_START_UNLOCK(); |
| 309 | pthread_rwlock_unlock(&rw_lock_); |
| 310 | PROFILE_MUTEX_UNLOCKED(); |
| 311 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 312 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 313 | private: |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 314 | mutable pthread_rwlock_t rw_lock_; |
| 315 | mutable bool initialized_; |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 316 | #ifndef THRIFT_NO_CONTENTION_PROFILING |
| 317 | mutable int64_t profileTime_; |
| 318 | #endif |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 319 | }; |
| 320 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 321 | ReadWriteMutex::ReadWriteMutex() : impl_(new ReadWriteMutex::impl()) { |
| 322 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 323 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 324 | void ReadWriteMutex::acquireRead() const { |
| 325 | impl_->acquireRead(); |
| 326 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 327 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 328 | void ReadWriteMutex::acquireWrite() const { |
| 329 | impl_->acquireWrite(); |
| 330 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 331 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 332 | bool ReadWriteMutex::attemptRead() const { |
| 333 | return impl_->attemptRead(); |
| 334 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 335 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 336 | bool ReadWriteMutex::attemptWrite() const { |
| 337 | return impl_->attemptWrite(); |
| 338 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 339 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 340 | void ReadWriteMutex::release() const { |
| 341 | impl_->release(); |
| 342 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 343 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 344 | NoStarveReadWriteMutex::NoStarveReadWriteMutex() : writerWaiting_(false) { |
| 345 | } |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 346 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 347 | void NoStarveReadWriteMutex::acquireRead() const { |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 348 | if (writerWaiting_) { |
| 349 | // writer is waiting, block on the writer's mutex until he's done with it |
| 350 | mutex_.lock(); |
| 351 | mutex_.unlock(); |
| 352 | } |
| 353 | |
| 354 | ReadWriteMutex::acquireRead(); |
| 355 | } |
| 356 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 357 | void NoStarveReadWriteMutex::acquireWrite() const { |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame] | 358 | // if we can acquire the rwlock the easy way, we're done |
| 359 | if (attemptWrite()) { |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | // failed to get the rwlock, do it the hard way: |
| 364 | // locking the mutex and setting writerWaiting will cause all new readers to |
| 365 | // block on the mutex rather than on the rwlock. |
| 366 | mutex_.lock(); |
| 367 | writerWaiting_ = true; |
| 368 | ReadWriteMutex::acquireWrite(); |
| 369 | writerWaiting_ = false; |
| 370 | mutex_.unlock(); |
| 371 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | } // apache::thrift::concurrency |