Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |
| 8 | #define _THRIFT_CONCURRENCY_MUTEX_H_ 1 |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 9 | |
| 10 | namespace facebook { namespace thrift { namespace concurrency { |
| 11 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 12 | /** |
| 13 | * A simple mutex class |
| 14 | * |
| 15 | * @author marc |
| 16 | * @version $Id:$ |
| 17 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 18 | class Mutex { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 19 | public: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 20 | Mutex(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 21 | virtual ~Mutex() {} |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | virtual void lock() const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 23 | virtual void unlock() const; |
| 24 | |
| 25 | private: |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 26 | class impl; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 27 | impl* impl_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 30 | class ReadWriteMutex { |
| 31 | public: |
| 32 | ReadWriteMutex(); |
| 33 | virtual ~ReadWriteMutex() {} |
| 34 | |
| 35 | // these get the lock and block until it is done successfully |
| 36 | virtual void acquireRead() const; |
| 37 | virtual void acquireWrite() const; |
| 38 | |
| 39 | // these attempt to get the lock, returning false immediately if they fail |
| 40 | virtual bool attemptRead() const; |
| 41 | virtual bool attemptWrite() const; |
| 42 | |
| 43 | // this releases both read and write locks |
| 44 | virtual void release() const; |
| 45 | |
| 46 | private: |
| 47 | class impl; |
| 48 | impl* impl_; |
| 49 | }; |
| 50 | |
Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 51 | class Guard { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 52 | public: |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 53 | Guard(const Mutex& value) : mutex_(value) { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 54 | mutex_.lock(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 55 | } |
boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 56 | ~Guard() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 57 | mutex_.unlock(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 61 | const Mutex& mutex_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 64 | class RWGuard { |
| 65 | public: |
| 66 | RWGuard(const ReadWriteMutex& value, bool write = 0) : rw_mutex_(value) { |
| 67 | if (write) { |
| 68 | rw_mutex_.acquireWrite(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame^] | 69 | } else { |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 70 | rw_mutex_.acquireRead(); |
David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame^] | 71 | } |
yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 72 | } |
| 73 | ~RWGuard() { |
| 74 | rw_mutex_.release(); |
| 75 | } |
| 76 | private: |
| 77 | const ReadWriteMutex rw_mutex_; |
| 78 | }; |
| 79 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 80 | |
| 81 | }}} // facebook::thrift::concurrency |
| 82 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 83 | #endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |