Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 1 | #ifndef _THRIFT_CONCURRENCY_MONITOR_H_ |
| 2 | #define _THRIFT_CONCURRENCY_MONITOR_H_ 1 |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 3 | |
| 4 | namespace facebook { namespace thrift { namespace concurrency { |
| 5 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 6 | /** |
| 7 | * A monitor is a combination mutex and condition-event. Waiting and |
| 8 | * notifying condition events requires that the caller own the mutex. Mutex |
| 9 | * lock and unlock operations can be performed independently of condition |
| 10 | * events. This is more or less analogous to java.lang.Object multi-thread |
| 11 | * operations |
| 12 | * |
| 13 | * Note that all methods are const. Monitors implement logical constness, not |
| 14 | * bit constness. This allows const methods to call monitor methods without |
| 15 | * needing to cast away constness or change to non-const signatures. |
| 16 | * |
| 17 | * @author marc |
| 18 | * @version $Id:$ |
| 19 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 20 | class Monitor { |
| 21 | |
| 22 | public: |
| 23 | |
| 24 | Monitor(); |
| 25 | |
| 26 | virtual ~Monitor(); |
| 27 | |
| 28 | virtual void lock() const; |
| 29 | |
| 30 | virtual void unlock() const; |
| 31 | |
| 32 | virtual void wait(long long timeout=0LL) const; |
| 33 | |
| 34 | virtual void notify() const; |
| 35 | |
| 36 | virtual void notifyAll() const; |
| 37 | |
| 38 | private: |
| 39 | |
| 40 | class Impl; |
| 41 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 42 | Impl* impl_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | class Synchronized { |
| 46 | public: |
| 47 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 48 | Synchronized(const Monitor& value) : |
| 49 | monitor_(value) { |
| 50 | monitor_.lock(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | ~Synchronized() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 54 | monitor_.unlock(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 58 | const Monitor& monitor_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | |
| 62 | }}} // facebook::thrift::concurrency |
| 63 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 64 | #endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_ |