| 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 | |
| boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 10 | #include <boost/shared_ptr.hpp> |
| 11 | using boost::shared_ptr; | ||||
| 12 | |||||
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 13 | namespace facebook { namespace thrift { namespace concurrency { |
| 14 | |||||
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 15 | /** |
| 16 | * A simple mutex class | ||||
| 17 | * | ||||
| 18 | * @author marc | ||||
| 19 | * @version $Id:$ | ||||
| 20 | */ | ||||
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 21 | class Mutex { |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | public: |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 23 | Mutex(); |
| boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 24 | virtual ~Mutex() {} |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 25 | virtual void lock() const; |
| boz | 5362e70 | 2007-08-15 20:55:36 +0000 | [diff] [blame] | 26 | virtual bool trylock() const; |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 27 | virtual void unlock() const; |
| 28 | |||||
| 29 | private: | ||||
| boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 30 | |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 31 | class impl; |
| boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 32 | shared_ptr<impl> impl_; |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 33 | }; |
| 34 | |||||
| boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 35 | class ReadWriteMutex { |
| 36 | public: | ||||
| 37 | ReadWriteMutex(); | ||||
| boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 38 | virtual ~ReadWriteMutex() {} |
| boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 39 | |
| 40 | // these get the lock and block until it is done successfully | ||||
| 41 | virtual void acquireRead() const; | ||||
| 42 | virtual void acquireWrite() const; | ||||
| 43 | |||||
| 44 | // these attempt to get the lock, returning false immediately if they fail | ||||
| 45 | virtual bool attemptRead() const; | ||||
| 46 | virtual bool attemptWrite() const; | ||||
| 47 | |||||
| 48 | // this releases both read and write locks | ||||
| 49 | virtual void release() const; | ||||
| 50 | |||||
| 51 | private: | ||||
| boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 52 | |
| boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 53 | class impl; |
| boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 54 | shared_ptr<impl> impl_; |
| boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 55 | }; |
| 56 | |||||
| Mark Slee | 85287d3 | 2007-07-09 19:50:30 +0000 | [diff] [blame] | 57 | class Guard { |
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 58 | public: |
| boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 59 | Guard(const Mutex& value) : mutex_(value) { |
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 60 | mutex_.lock(); |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 61 | } |
| boz | cce8184 | 2007-07-06 22:27:52 +0000 | [diff] [blame] | 62 | ~Guard() { |
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 63 | mutex_.unlock(); |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 64 | } |
| 65 | |||||
| 66 | private: | ||||
| Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 67 | const Mutex& mutex_; |
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 68 | }; |
| 69 | |||||
| yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 70 | class RWGuard { |
| 71 | public: | ||||
| 72 | RWGuard(const ReadWriteMutex& value, bool write = 0) : rw_mutex_(value) { | ||||
| 73 | if (write) { | ||||
| 74 | rw_mutex_.acquireWrite(); | ||||
| David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 75 | } else { |
| yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 76 | rw_mutex_.acquireRead(); |
| David Reiss | 96d2388 | 2007-07-26 21:10:32 +0000 | [diff] [blame] | 77 | } |
| yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 78 | } |
| 79 | ~RWGuard() { | ||||
| 80 | rw_mutex_.release(); | ||||
| 81 | } | ||||
| 82 | private: | ||||
| boz | 19cee90 | 2007-09-22 23:08:11 +0000 | [diff] [blame] | 83 | const ReadWriteMutex& rw_mutex_; |
| yunfang | a36f5db | 2007-07-14 01:23:05 +0000 | [diff] [blame] | 84 | }; |
| 85 | |||||
| Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 86 | |
| 87 | }}} // facebook::thrift::concurrency | ||||
| 88 | |||||
| Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 89 | #endif // #ifndef _THRIFT_CONCURRENCY_MUTEX_H_ |