blob: 6cfb1486334c829cc3e2572fe7f20572c3c63784 [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
Marc Slemko3a3b53b2007-05-22 23:59:54 +000010#include "Exception.h"
11
T Jake Lucianib5e62212009-01-31 22:36:20 +000012namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000013
Mark Sleef5f2be42006-09-05 21:05:31 +000014/**
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 *
Mark Sleef5f2be42006-09-05 21:05:31 +000025 * @version $Id:$
26 */
Marc Slemko66949872006-07-15 01:52:39 +000027class Monitor {
28
29 public:
30
31 Monitor();
32
33 virtual ~Monitor();
34
35 virtual void lock() const;
36
37 virtual void unlock() const;
38
Mark Slee9b82d272007-05-23 05:16:07 +000039 virtual void wait(int64_t timeout=0LL) const;
Marc Slemko66949872006-07-15 01:52:39 +000040
41 virtual void notify() const;
42
43 virtual void notifyAll() const;
44
45 private:
46
47 class Impl;
48
Mark Slee2f6404d2006-10-10 01:37:40 +000049 Impl* impl_;
Marc Slemko66949872006-07-15 01:52:39 +000050};
51
52class Synchronized {
53 public:
Marc Slemko3a3b53b2007-05-22 23:59:54 +000054
Mark Slee2f6404d2006-10-10 01:37:40 +000055 Synchronized(const Monitor& value) :
56 monitor_(value) {
57 monitor_.lock();
Marc Slemko66949872006-07-15 01:52:39 +000058 }
59
60 ~Synchronized() {
Mark Slee2f6404d2006-10-10 01:37:40 +000061 monitor_.unlock();
Marc Slemko66949872006-07-15 01:52:39 +000062 }
63
64 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000065 const Monitor& monitor_;
Marc Slemko66949872006-07-15 01:52:39 +000066};
67
68
T Jake Lucianib5e62212009-01-31 22:36:20 +000069}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +000070
Mark Sleef5f2be42006-09-05 21:05:31 +000071#endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_