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