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