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