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