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 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |
| 21 | #define _THRIFT_CONCURRENCY_MUTEX_H_ 1 |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 23 | #include <boost/shared_ptr.hpp> |
Bryan Duxbury | c58c9e8 | 2011-09-01 16:52:56 +0000 | [diff] [blame] | 24 | #include <boost/noncopyable.hpp> |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 25 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 26 | namespace apache { namespace thrift { namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 27 | |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 28 | #ifndef THRIFT_NO_CONTENTION_PROFILING |
| 29 | |
| 30 | /** |
| 31 | * Determines if the Thrift Mutex and ReadWriteMutex classes will attempt to |
| 32 | * profile their blocking acquire methods. If this value is set to non-zero, |
| 33 | * Thrift will attempt to invoke the callback once every profilingSampleRate |
| 34 | * times. However, as the sampling is not synchronized the rate is not |
| 35 | * guranateed, and could be subject to big bursts and swings. Please ensure |
| 36 | * your sampling callback is as performant as your application requires. |
| 37 | * |
| 38 | * The callback will get called with the wait time taken to lock the mutex in |
| 39 | * usec and a (void*) that uniquely identifies the Mutex (or ReadWriteMutex) |
| 40 | * being locked. |
| 41 | * |
| 42 | * The enableMutexProfiling() function is unsynchronized; calling this function |
| 43 | * while profiling is already enabled may result in race conditions. On |
| 44 | * architectures where a pointer assignment is atomic, this is safe but there |
| 45 | * is no guarantee threads will agree on a single callback within any |
| 46 | * particular time period. |
| 47 | */ |
| 48 | typedef void (*MutexWaitCallback)(const void* id, int64_t waitTimeMicros); |
| 49 | void enableMutexProfiling(int32_t profilingSampleRate, |
| 50 | MutexWaitCallback callback); |
| 51 | |
| 52 | #endif |
| 53 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 54 | /** |
| 55 | * A simple mutex class |
| 56 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 57 | * @version $Id:$ |
| 58 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 59 | class Mutex { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 60 | public: |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 61 | typedef void (*Initializer)(void*); |
| 62 | |
| 63 | Mutex(Initializer init = DEFAULT_INITIALIZER); |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 64 | virtual ~Mutex() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 65 | virtual void lock() const; |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 66 | virtual bool trylock() const; |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 67 | virtual bool timedlock(int64_t milliseconds) const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 68 | virtual void unlock() const; |
| 69 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 70 | void* getUnderlyingImpl() const; |
| 71 | |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 72 | static void DEFAULT_INITIALIZER(void*); |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 73 | static void ADAPTIVE_INITIALIZER(void*); |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 74 | static void RECURSIVE_INITIALIZER(void*); |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 75 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 76 | private: |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 77 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 78 | class impl; |
yunfang | 1454296 | 2007-10-03 22:59:41 +0000 | [diff] [blame] | 79 | boost::shared_ptr<impl> impl_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 82 | class ReadWriteMutex { |
| 83 | public: |
| 84 | ReadWriteMutex(); |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 85 | virtual ~ReadWriteMutex() {} |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 86 | |
| 87 | // these get the lock and block until it is done successfully |
| 88 | virtual void acquireRead() const; |
| 89 | virtual void acquireWrite() const; |
| 90 | |
| 91 | // these attempt to get the lock, returning false immediately if they fail |
| 92 | virtual bool attemptRead() const; |
| 93 | virtual bool attemptWrite() const; |
| 94 | |
| 95 | // this releases both read and write locks |
| 96 | virtual void release() const; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 97 | |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 98 | private: |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 99 | |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 100 | class impl; |
yunfang | 1454296 | 2007-10-03 22:59:41 +0000 | [diff] [blame] | 101 | boost::shared_ptr<impl> impl_; |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Roger Meier | 3fc4819 | 2011-12-11 21:05:35 +0000 | [diff] [blame^] | 104 | /** |
| 105 | * A ReadWriteMutex that guarantees writers will not be starved by readers: |
| 106 | * When a writer attempts to acquire the mutex, all new readers will be |
| 107 | * blocked from acquiring the mutex until the writer has acquired and |
| 108 | * released it. In some operating systems, this may already be guaranteed |
| 109 | * by a regular ReadWriteMutex. |
| 110 | */ |
| 111 | class NoStarveReadWriteMutex : public ReadWriteMutex { |
| 112 | public: |
| 113 | NoStarveReadWriteMutex(); |
| 114 | |
| 115 | virtual void acquireRead() const; |
| 116 | virtual void acquireWrite() const; |
| 117 | |
| 118 | private: |
| 119 | Mutex mutex_; |
| 120 | mutable volatile bool writerWaiting_; |
| 121 | }; |
| 122 | |
Bryan Duxbury | c58c9e8 | 2011-09-01 16:52:56 +0000 | [diff] [blame] | 123 | class Guard : boost::noncopyable { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 124 | public: |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 125 | Guard(const Mutex& value, int64_t timeout = 0) : mutex_(&value) { |
| 126 | if (timeout == 0) { |
| 127 | value.lock(); |
| 128 | } else if (timeout < 0) { |
| 129 | if (!value.trylock()) { |
| 130 | mutex_ = NULL; |
| 131 | } |
| 132 | } else { |
| 133 | if (!value.timedlock(timeout)) { |
| 134 | mutex_ = NULL; |
| 135 | } |
| 136 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 137 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 138 | ~Guard() { |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 139 | if (mutex_) { |
| 140 | mutex_->unlock(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | operator bool() const { |
| 145 | return (mutex_ != NULL); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | private: |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame] | 149 | const Mutex* mutex_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
David Reiss | e96fa55 | 2010-03-09 05:19:37 +0000 | [diff] [blame] | 152 | // Can be used as second argument to RWGuard to make code more readable |
| 153 | // as to whether we're doing acquireRead() or acquireWrite(). |
| 154 | enum RWGuardType { |
| 155 | RW_READ = 0, |
Roger Meier | 0069cc4 | 2010-10-13 18:10:18 +0000 | [diff] [blame] | 156 | RW_WRITE = 1 |
David Reiss | e96fa55 | 2010-03-09 05:19:37 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | |
Jake Farrell | 2e9f510 | 2011-09-09 04:32:36 +0000 | [diff] [blame] | 160 | class RWGuard : boost::noncopyable { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 161 | public: |
David Reiss | e96fa55 | 2010-03-09 05:19:37 +0000 | [diff] [blame] | 162 | RWGuard(const ReadWriteMutex& value, bool write = false) |
| 163 | : rw_mutex_(value) { |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 164 | if (write) { |
| 165 | rw_mutex_.acquireWrite(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 166 | } else { |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 167 | rw_mutex_.acquireRead(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 168 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 169 | } |
David Reiss | e96fa55 | 2010-03-09 05:19:37 +0000 | [diff] [blame] | 170 | |
| 171 | RWGuard(const ReadWriteMutex& value, RWGuardType type) |
| 172 | : rw_mutex_(value) { |
| 173 | if (type == RW_WRITE) { |
| 174 | rw_mutex_.acquireWrite(); |
| 175 | } else { |
| 176 | rw_mutex_.acquireRead(); |
| 177 | } |
| 178 | } |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 179 | ~RWGuard() { |
| 180 | rw_mutex_.release(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 181 | } |
| 182 | private: |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 183 | const ReadWriteMutex& rw_mutex_; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 184 | }; |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 185 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 186 | |
David Reiss | eaa61e4 | 2007-12-20 21:42:05 +0000 | [diff] [blame] | 187 | // A little hack to prevent someone from trying to do "Guard(m);" |
David Reiss | be378f2 | 2009-05-07 00:41:18 +0000 | [diff] [blame] | 188 | // Such a use is invalid because the temporary Guard object is |
Roger Meier | 3faaedf | 2011-10-02 10:51:45 +0000 | [diff] [blame] | 189 | // destroyed at the end of the line, releasing the lock. |
David Reiss | eaa61e4 | 2007-12-20 21:42:05 +0000 | [diff] [blame] | 190 | // Sorry for polluting the global namespace, but I think it's worth it. |
| 191 | #define Guard(m) incorrect_use_of_Guard(m) |
| 192 | #define RWGuard(m) incorrect_use_of_RWGuard(m) |
| 193 | |
| 194 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 195 | }}} // apache::thrift::concurrency |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 196 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 197 | #endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |