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