| Gavin McDonald | 0b75e1a | 2010-10-28 02:12:01 +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 | */ |
| 19 | |
| 20 | #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |
| 21 | #define _THRIFT_CONCURRENCY_MUTEX_H_ 1 |
| 22 | |
| 23 | #include <boost/shared_ptr.hpp> |
| 24 | |
| 25 | namespace apache { namespace thrift { namespace concurrency { |
| 26 | |
| 27 | /** |
| 28 | * A simple mutex class |
| 29 | * |
| 30 | * @version $Id:$ |
| 31 | */ |
| 32 | class Mutex { |
| 33 | public: |
| 34 | typedef void (*Initializer)(void*); |
| 35 | |
| 36 | Mutex(Initializer init = DEFAULT_INITIALIZER); |
| 37 | virtual ~Mutex() {} |
| 38 | virtual void lock() const; |
| 39 | virtual bool trylock() const; |
| 40 | virtual void unlock() const; |
| 41 | |
| 42 | static void DEFAULT_INITIALIZER(void*); |
| 43 | static void ADAPTIVE_INITIALIZER(void*); |
| 44 | static void RECURSIVE_INITIALIZER(void*); |
| 45 | |
| 46 | private: |
| 47 | |
| 48 | class impl; |
| 49 | boost::shared_ptr<impl> impl_; |
| 50 | }; |
| 51 | |
| 52 | class ReadWriteMutex { |
| 53 | public: |
| 54 | ReadWriteMutex(); |
| 55 | virtual ~ReadWriteMutex() {} |
| 56 | |
| 57 | // these get the lock and block until it is done successfully |
| 58 | virtual void acquireRead() const; |
| 59 | virtual void acquireWrite() const; |
| 60 | |
| 61 | // these attempt to get the lock, returning false immediately if they fail |
| 62 | virtual bool attemptRead() const; |
| 63 | virtual bool attemptWrite() const; |
| 64 | |
| 65 | // this releases both read and write locks |
| 66 | virtual void release() const; |
| 67 | |
| 68 | private: |
| 69 | |
| 70 | class impl; |
| 71 | boost::shared_ptr<impl> impl_; |
| 72 | }; |
| 73 | |
| 74 | class Guard { |
| 75 | public: |
| 76 | Guard(const Mutex& value) : mutex_(value) { |
| 77 | mutex_.lock(); |
| 78 | } |
| 79 | ~Guard() { |
| 80 | mutex_.unlock(); |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | const Mutex& mutex_; |
| 85 | }; |
| 86 | |
| 87 | class RWGuard { |
| 88 | public: |
| 89 | RWGuard(const ReadWriteMutex& value, bool write = 0) : rw_mutex_(value) { |
| 90 | if (write) { |
| 91 | rw_mutex_.acquireWrite(); |
| 92 | } else { |
| 93 | rw_mutex_.acquireRead(); |
| 94 | } |
| 95 | } |
| 96 | ~RWGuard() { |
| 97 | rw_mutex_.release(); |
| 98 | } |
| 99 | private: |
| 100 | const ReadWriteMutex& rw_mutex_; |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | // A little hack to prevent someone from trying to do "Guard(m);" |
| 105 | // Sorry for polluting the global namespace, but I think it's worth it. |
| 106 | #define Guard(m) incorrect_use_of_Guard(m) |
| 107 | #define RWGuard(m) incorrect_use_of_RWGuard(m) |
| 108 | |
| 109 | |
| 110 | }}} // apache::thrift::concurrency |
| 111 | |
| 112 | #endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |