blob: 62a83446169bd6f3a4e6f3dd0ecf99eac7d126c0 [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
42 Impl* _impl;
43};
44
45class 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 Sleef5f2be42006-09-05 21:05:31 +000063#endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_