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 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 27 | /** |
| 28 | * A simple mutex class |
| 29 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 30 | * @version $Id:$ |
| 31 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 32 | class Mutex { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 33 | public: |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 34 | typedef void (*Initializer)(void*); |
| 35 | |
| 36 | Mutex(Initializer init = DEFAULT_INITIALIZER); |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 37 | virtual ~Mutex() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 38 | virtual void lock() const; |
boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 39 | virtual bool trylock() const; |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame^] | 40 | virtual bool timedlock(int64_t milliseconds) const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 41 | virtual void unlock() const; |
| 42 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 43 | void* getUnderlyingImpl() const; |
| 44 | |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 45 | static void DEFAULT_INITIALIZER(void*); |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 46 | static void ADAPTIVE_INITIALIZER(void*); |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 47 | static void RECURSIVE_INITIALIZER(void*); |
David Reiss | c6dab61 | 2008-06-10 22:55:13 +0000 | [diff] [blame] | 48 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 49 | private: |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 50 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 51 | class impl; |
yunfang | 1454296 | 2007-10-03 22:59:41 +0000 | [diff] [blame] | 52 | boost::shared_ptr<impl> impl_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 55 | class ReadWriteMutex { |
| 56 | public: |
| 57 | ReadWriteMutex(); |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 58 | virtual ~ReadWriteMutex() {} |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 59 | |
| 60 | // these get the lock and block until it is done successfully |
| 61 | virtual void acquireRead() const; |
| 62 | virtual void acquireWrite() const; |
| 63 | |
| 64 | // these attempt to get the lock, returning false immediately if they fail |
| 65 | virtual bool attemptRead() const; |
| 66 | virtual bool attemptWrite() const; |
| 67 | |
| 68 | // this releases both read and write locks |
| 69 | virtual void release() const; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 70 | |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 71 | private: |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 72 | |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 73 | class impl; |
yunfang | 1454296 | 2007-10-03 22:59:41 +0000 | [diff] [blame] | 74 | boost::shared_ptr<impl> impl_; |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 77 | class Guard { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 78 | public: |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame^] | 79 | Guard(const Mutex& value, int64_t timeout = 0) : mutex_(&value) { |
| 80 | if (timeout == 0) { |
| 81 | value.lock(); |
| 82 | } else if (timeout < 0) { |
| 83 | if (!value.trylock()) { |
| 84 | mutex_ = NULL; |
| 85 | } |
| 86 | } else { |
| 87 | if (!value.timedlock(timeout)) { |
| 88 | mutex_ = NULL; |
| 89 | } |
| 90 | } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 91 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 92 | ~Guard() { |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame^] | 93 | if (mutex_) { |
| 94 | mutex_->unlock(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | operator bool() const { |
| 99 | return (mutex_ != NULL); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private: |
David Reiss | 4e19f19 | 2010-03-09 05:19:59 +0000 | [diff] [blame^] | 103 | const Mutex* mutex_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
David Reiss | e96fa55 | 2010-03-09 05:19:37 +0000 | [diff] [blame] | 106 | // Can be used as second argument to RWGuard to make code more readable |
| 107 | // as to whether we're doing acquireRead() or acquireWrite(). |
| 108 | enum RWGuardType { |
| 109 | RW_READ = 0, |
| 110 | RW_WRITE = 1, |
| 111 | }; |
| 112 | |
| 113 | |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 114 | class RWGuard { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 115 | public: |
David Reiss | e96fa55 | 2010-03-09 05:19:37 +0000 | [diff] [blame] | 116 | RWGuard(const ReadWriteMutex& value, bool write = false) |
| 117 | : rw_mutex_(value) { |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 118 | if (write) { |
| 119 | rw_mutex_.acquireWrite(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 120 | } else { |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 121 | rw_mutex_.acquireRead(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 122 | } |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 123 | } |
David Reiss | e96fa55 | 2010-03-09 05:19:37 +0000 | [diff] [blame] | 124 | |
| 125 | RWGuard(const ReadWriteMutex& value, RWGuardType type) |
| 126 | : rw_mutex_(value) { |
| 127 | if (type == RW_WRITE) { |
| 128 | rw_mutex_.acquireWrite(); |
| 129 | } else { |
| 130 | rw_mutex_.acquireRead(); |
| 131 | } |
| 132 | } |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 133 | ~RWGuard() { |
| 134 | rw_mutex_.release(); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 135 | } |
| 136 | private: |
boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 137 | const ReadWriteMutex& rw_mutex_; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 138 | }; |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 139 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 140 | |
David Reiss | eaa61e4 | 2007-12-20 21:42:05 +0000 | [diff] [blame] | 141 | // A little hack to prevent someone from trying to do "Guard(m);" |
David Reiss | be378f2 | 2009-05-07 00:41:18 +0000 | [diff] [blame] | 142 | // Such a use is invalid because the temporary Guard object is |
| 143 | // destoryed at the end of the line, releasing the lock. |
David Reiss | eaa61e4 | 2007-12-20 21:42:05 +0000 | [diff] [blame] | 144 | // Sorry for polluting the global namespace, but I think it's worth it. |
| 145 | #define Guard(m) incorrect_use_of_Guard(m) |
| 146 | #define RWGuard(m) incorrect_use_of_RWGuard(m) |
| 147 | |
| 148 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 149 | }}} // apache::thrift::concurrency |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 150 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 151 | #endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |