blob: d72267d88b1864b954748aac5c19df9f6ba6b992 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
Marc Slemko66949872006-07-15 01:52:39 +000020#include "Mutex.h"
David Reiss4e19f192010-03-09 05:19:59 +000021#include "Util.h"
Marc Slemko66949872006-07-15 01:52:39 +000022
23#include <assert.h>
24#include <pthread.h>
David Reiss7a2065d2010-03-09 05:20:04 +000025#include <signal.h>
Marc Slemko66949872006-07-15 01:52:39 +000026
yunfang14542962007-10-03 22:59:41 +000027using boost::shared_ptr;
28
T Jake Lucianib5e62212009-01-31 22:36:20 +000029namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000030
David Reiss7a2065d2010-03-09 05:20:04 +000031#ifndef THRIFT_NO_CONTENTION_PROFILING
32
33static sig_atomic_t mutexProfilingSampleRate = 0;
34static MutexWaitCallback mutexProfilingCallback = 0;
35
36volatile static sig_atomic_t mutexProfilingCounter = 0;
37
38void enableMutexProfiling(int32_t profilingSampleRate,
39 MutexWaitCallback callback) {
40 mutexProfilingSampleRate = profilingSampleRate;
41 mutexProfilingCallback = callback;
42}
43
44#define PROFILE_MUTEX_START_LOCK() \
45 int64_t _lock_startTime = maybeGetProfilingStartTime();
46
47#define PROFILE_MUTEX_NOT_LOCKED() \
48 do { \
49 if (_lock_startTime > 0) { \
50 int64_t endTime = Util::currentTimeUsec(); \
51 (*mutexProfilingCallback)(this, endTime - _lock_startTime); \
52 } \
53 } while (0)
54
55#define PROFILE_MUTEX_LOCKED() \
56 do { \
57 profileTime_ = _lock_startTime; \
58 if (profileTime_ > 0) { \
59 profileTime_ = Util::currentTimeUsec() - profileTime_; \
60 } \
61 } while (0)
62
63#define PROFILE_MUTEX_START_UNLOCK() \
64 int64_t _temp_profileTime = profileTime_; \
65 profileTime_ = 0;
66
67#define PROFILE_MUTEX_UNLOCKED() \
68 do { \
69 if (_temp_profileTime > 0) { \
70 (*mutexProfilingCallback)(this, _temp_profileTime); \
71 } \
72 } while (0)
73
74static inline int64_t maybeGetProfilingStartTime() {
75 if (mutexProfilingSampleRate && mutexProfilingCallback) {
76 // This block is unsynchronized, but should produce a reasonable sampling
77 // rate on most architectures. The main race conditions are the gap
78 // between the decrement and the test, the non-atomicity of decrement, and
79 // potential caching of different values at different CPUs.
80 //
81 // - if two decrements race, the likeliest result is that the counter
82 // decrements slowly (perhaps much more slowly) than intended.
83 //
84 // - many threads could potentially decrement before resetting the counter
85 // to its large value, causing each additional incoming thread to
86 // profile every call. This situation is unlikely to persist for long
87 // as the critical gap is quite short, but profiling could be bursty.
88 sig_atomic_t localValue = --mutexProfilingCounter;
89 if (localValue <= 0) {
90 mutexProfilingCounter = mutexProfilingSampleRate;
91 return Util::currentTimeUsec();
92 }
93 }
94
95 return 0;
96}
97
98#else
99# define PROFILE_MUTEX_START_LOCK()
100# define PROFILE_MUTEX_NOT_LOCKED()
101# define PROFILE_MUTEX_LOCKED()
102# define PROFILE_MUTEX_START_UNLOCK()
103# define PROFILE_MUTEX_UNLOCKED()
104#endif // THRIFT_NO_CONTENTION_PROFILING
105
David Reiss0c90f6f2008-02-06 22:18:40 +0000106/**
Mark Sleef5f2be42006-09-05 21:05:31 +0000107 * Implementation of Mutex class using POSIX mutex
108 *
Mark Sleef5f2be42006-09-05 21:05:31 +0000109 * @version $Id:$
110 */
Marc Slemko66949872006-07-15 01:52:39 +0000111class Mutex::impl {
Mark Sleef5f2be42006-09-05 21:05:31 +0000112 public:
David Reissc6dab612008-06-10 22:55:13 +0000113 impl(Initializer init) : initialized_(false) {
David Reiss7a2065d2010-03-09 05:20:04 +0000114#ifndef THRIFT_NO_CONTENTION_PROFILING
115 profileTime_ = 0;
116#endif
David Reissc6dab612008-06-10 22:55:13 +0000117 init(&pthread_mutex_);
Mark Slee2f6404d2006-10-10 01:37:40 +0000118 initialized_ = true;
Marc Slemko66949872006-07-15 01:52:39 +0000119 }
120
121 ~impl() {
Mark Slee2f6404d2006-10-10 01:37:40 +0000122 if (initialized_) {
123 initialized_ = false;
Aditya Agarwal9dc57402007-03-31 17:45:12 +0000124 int ret = pthread_mutex_destroy(&pthread_mutex_);
Aditya Agarwal3f234da2007-04-01 01:19:57 +0000125 assert(ret == 0);
Marc Slemko66949872006-07-15 01:52:39 +0000126 }
127 }
128
David Reiss7a2065d2010-03-09 05:20:04 +0000129 void lock() const {
130 PROFILE_MUTEX_START_LOCK();
131 pthread_mutex_lock(&pthread_mutex_);
132 PROFILE_MUTEX_LOCKED();
133 }
Marc Slemko66949872006-07-15 01:52:39 +0000134
boz5362e702007-08-15 20:55:36 +0000135 bool trylock() const { return (0 == pthread_mutex_trylock(&pthread_mutex_)); }
136
David Reiss4e19f192010-03-09 05:19:59 +0000137 bool timedlock(int64_t milliseconds) const {
David Reiss7a2065d2010-03-09 05:20:04 +0000138 PROFILE_MUTEX_START_LOCK();
139
David Reiss4e19f192010-03-09 05:19:59 +0000140 struct timespec ts;
141 Util::toTimespec(ts, milliseconds);
David Reiss7a2065d2010-03-09 05:20:04 +0000142 int ret = pthread_mutex_timedlock(&pthread_mutex_, &ts);
143 if (ret == 0) {
144 PROFILE_MUTEX_LOCKED();
145 return true;
146 }
147
148 PROFILE_MUTEX_NOT_LOCKED();
149 return false;
David Reiss4e19f192010-03-09 05:19:59 +0000150 }
151
David Reiss7a2065d2010-03-09 05:20:04 +0000152 void unlock() const {
153 PROFILE_MUTEX_START_UNLOCK();
154 pthread_mutex_unlock(&pthread_mutex_);
155 PROFILE_MUTEX_UNLOCKED();
156 }
Marc Slemko66949872006-07-15 01:52:39 +0000157
David Reissb9db49c2010-03-09 05:19:30 +0000158 void* getUnderlyingImpl() const { return (void*) &pthread_mutex_; }
159
Mark Sleef5f2be42006-09-05 21:05:31 +0000160 private:
Mark Slee2f6404d2006-10-10 01:37:40 +0000161 mutable pthread_mutex_t pthread_mutex_;
162 mutable bool initialized_;
David Reiss7a2065d2010-03-09 05:20:04 +0000163#ifndef THRIFT_NO_CONTENTION_PROFILING
164 mutable int64_t profileTime_;
165#endif
Marc Slemko66949872006-07-15 01:52:39 +0000166};
167
David Reissc6dab612008-06-10 22:55:13 +0000168Mutex::Mutex(Initializer init) : impl_(new Mutex::impl(init)) {}
Marc Slemko66949872006-07-15 01:52:39 +0000169
David Reissb9db49c2010-03-09 05:19:30 +0000170void* Mutex::getUnderlyingImpl() const { return impl_->getUnderlyingImpl(); }
171
Mark Slee2f6404d2006-10-10 01:37:40 +0000172void Mutex::lock() const { impl_->lock(); }
Marc Slemko66949872006-07-15 01:52:39 +0000173
boz5362e702007-08-15 20:55:36 +0000174bool Mutex::trylock() const { return impl_->trylock(); }
175
David Reiss4e19f192010-03-09 05:19:59 +0000176bool Mutex::timedlock(int64_t ms) const { return impl_->timedlock(ms); }
177
Mark Slee2f6404d2006-10-10 01:37:40 +0000178void Mutex::unlock() const { impl_->unlock(); }
Marc Slemko66949872006-07-15 01:52:39 +0000179
David Reissc6dab612008-06-10 22:55:13 +0000180void Mutex::DEFAULT_INITIALIZER(void* arg) {
181 pthread_mutex_t* pthread_mutex = (pthread_mutex_t*)arg;
182 int ret = pthread_mutex_init(pthread_mutex, NULL);
183 assert(ret == 0);
184}
185
186static void init_with_kind(pthread_mutex_t* mutex, int kind) {
187 pthread_mutexattr_t mutexattr;
188 int ret = pthread_mutexattr_init(&mutexattr);
189 assert(ret == 0);
190
191 // Apparently, this can fail. Should we really be aborting?
192 ret = pthread_mutexattr_settype(&mutexattr, kind);
193 assert(ret == 0);
194
195 ret = pthread_mutex_init(mutex, &mutexattr);
196 assert(ret == 0);
197
198 ret = pthread_mutexattr_destroy(&mutexattr);
199 assert(ret == 0);
200}
201
202#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
203void Mutex::ADAPTIVE_INITIALIZER(void* arg) {
204 // From mysql source: mysys/my_thr_init.c
205 // Set mutex type to "fast" a.k.a "adaptive"
206 //
207 // In this case the thread may steal the mutex from some other thread
208 // that is waiting for the same mutex. This will save us some
209 // context switches but may cause a thread to 'starve forever' while
210 // waiting for the mutex (not likely if the code within the mutex is
211 // short).
212 init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_ADAPTIVE_NP);
213}
214#endif
215
216#ifdef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
217void Mutex::RECURSIVE_INITIALIZER(void* arg) {
218 init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE_NP);
219}
220#endif
221
222
David Reiss0c90f6f2008-02-06 22:18:40 +0000223/**
bozcce81842007-07-06 22:27:52 +0000224 * Implementation of ReadWriteMutex class using POSIX rw lock
225 *
bozcce81842007-07-06 22:27:52 +0000226 * @version $Id:$
227 */
228class ReadWriteMutex::impl {
229public:
230 impl() : initialized_(false) {
David Reiss7a2065d2010-03-09 05:20:04 +0000231#ifndef THRIFT_NO_CONTENTION_PROFILING
232 profileTime_ = 0;
233#endif
bozcce81842007-07-06 22:27:52 +0000234 int ret = pthread_rwlock_init(&rw_lock_, NULL);
235 assert(ret == 0);
236 initialized_ = true;
237 }
238
239 ~impl() {
240 if(initialized_) {
241 initialized_ = false;
242 int ret = pthread_rwlock_destroy(&rw_lock_);
243 assert(ret == 0);
244 }
245 }
246
David Reiss7a2065d2010-03-09 05:20:04 +0000247 void acquireRead() const {
248 PROFILE_MUTEX_START_LOCK();
249 pthread_rwlock_rdlock(&rw_lock_);
250 PROFILE_MUTEX_NOT_LOCKED(); // not exclusive, so use not-locked path
251 }
bozcce81842007-07-06 22:27:52 +0000252
David Reiss7a2065d2010-03-09 05:20:04 +0000253 void acquireWrite() const {
254 PROFILE_MUTEX_START_LOCK();
255 pthread_rwlock_wrlock(&rw_lock_);
256 PROFILE_MUTEX_LOCKED();
257 }
bozcce81842007-07-06 22:27:52 +0000258
259 bool attemptRead() const { return pthread_rwlock_tryrdlock(&rw_lock_); }
260
261 bool attemptWrite() const { return pthread_rwlock_trywrlock(&rw_lock_); }
262
David Reiss7a2065d2010-03-09 05:20:04 +0000263 void release() const {
264 PROFILE_MUTEX_START_UNLOCK();
265 pthread_rwlock_unlock(&rw_lock_);
266 PROFILE_MUTEX_UNLOCKED();
267 }
bozcce81842007-07-06 22:27:52 +0000268
David Reiss0c90f6f2008-02-06 22:18:40 +0000269private:
bozcce81842007-07-06 22:27:52 +0000270 mutable pthread_rwlock_t rw_lock_;
271 mutable bool initialized_;
David Reiss7a2065d2010-03-09 05:20:04 +0000272#ifndef THRIFT_NO_CONTENTION_PROFILING
273 mutable int64_t profileTime_;
274#endif
bozcce81842007-07-06 22:27:52 +0000275};
276
277ReadWriteMutex::ReadWriteMutex() : impl_(new ReadWriteMutex::impl()) {}
278
279void ReadWriteMutex::acquireRead() const { impl_->acquireRead(); }
280
281void ReadWriteMutex::acquireWrite() const { impl_->acquireWrite(); }
282
283bool ReadWriteMutex::attemptRead() const { return impl_->attemptRead(); }
284
285bool ReadWriteMutex::attemptWrite() const { return impl_->attemptWrite(); }
286
287void ReadWriteMutex::release() const { impl_->release(); }
288
T Jake Lucianib5e62212009-01-31 22:36:20 +0000289}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +0000290