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