blob: 4c62e78e5aa3fdfd9f0eb900fb68eed1c5b82722 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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 Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_CONCURRENCY_MONITOR_H_
8#define _THRIFT_CONCURRENCY_MONITOR_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +00009
10namespace facebook { namespace thrift { namespace concurrency {
11
Mark Sleef5f2be42006-09-05 21:05:31 +000012/**
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 Slemko66949872006-07-15 01:52:39 +000026class 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 Slee2f6404d2006-10-10 01:37:40 +000048 Impl* impl_;
Marc Slemko66949872006-07-15 01:52:39 +000049};
50
51class Synchronized {
52 public:
53
Mark Slee2f6404d2006-10-10 01:37:40 +000054 Synchronized(const Monitor& value) :
55 monitor_(value) {
56 monitor_.lock();
Marc Slemko66949872006-07-15 01:52:39 +000057 }
58
59 ~Synchronized() {
Mark Slee2f6404d2006-10-10 01:37:40 +000060 monitor_.unlock();
Marc Slemko66949872006-07-15 01:52:39 +000061 }
62
63 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000064 const Monitor& monitor_;
Marc Slemko66949872006-07-15 01:52:39 +000065};
66
67
68}}} // facebook::thrift::concurrency
69
Mark Sleef5f2be42006-09-05 21:05:31 +000070#endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_