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 | |
| 42 | Impl* _impl; |
| 43 | }; |
| 44 | |
| 45 | class Synchronized { |
| 46 | public: |
| 47 | |
| 48 | Synchronized(const Monitor& value) : _monitor(value) { |
| 49 | _monitor.lock(); |
| 50 | } |
| 51 | |
| 52 | ~Synchronized() { |
| 53 | _monitor.unlock(); |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | const Monitor& _monitor; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | }}} // facebook::thrift::concurrency |
| 62 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame^] | 63 | #endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_ |