blob: e67b71bd1fee0d0742dc3329fe644fb72d835ea7 [file] [log] [blame]
Mark Sleef5f2be42006-09-05 21:05:31 +00001#ifndef _THRIFT_CONCURRENCY_MONITOR_H_
2#define _THRIFT_CONCURRENCY_MONITOR_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +00003
4namespace facebook { namespace thrift { namespace concurrency {
5
Mark Sleef5f2be42006-09-05 21:05:31 +00006/**
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 Slemko66949872006-07-15 01:52:39 +000020class 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 Slee2f6404d2006-10-10 01:37:40 +000042 Impl* impl_;
Marc Slemko66949872006-07-15 01:52:39 +000043};
44
45class Synchronized {
46 public:
47
Mark Slee2f6404d2006-10-10 01:37:40 +000048 Synchronized(const Monitor& value) :
49 monitor_(value) {
50 monitor_.lock();
Marc Slemko66949872006-07-15 01:52:39 +000051 }
52
53 ~Synchronized() {
Mark Slee2f6404d2006-10-10 01:37:40 +000054 monitor_.unlock();
Marc Slemko66949872006-07-15 01:52:39 +000055 }
56
57 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000058 const Monitor& monitor_;
Marc Slemko66949872006-07-15 01:52:39 +000059};
60
61
62}}} // facebook::thrift::concurrency
63
Mark Sleef5f2be42006-09-05 21:05:31 +000064#endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_