blob: 1eb36ebeeb3d772a25f9c3229976b97f47b3e637 [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
12namespace facebook { 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 *
25 * @author marc
26 * @version $Id:$
27 */
Marc Slemko66949872006-07-15 01:52:39 +000028class 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 Slee9b82d272007-05-23 05:16:07 +000040 virtual void wait(int64_t timeout=0LL) const;
Marc Slemko66949872006-07-15 01:52:39 +000041
42 virtual void notify() const;
43
44 virtual void notifyAll() const;
45
46 private:
47
48 class Impl;
49
Mark Slee2f6404d2006-10-10 01:37:40 +000050 Impl* impl_;
Marc Slemko66949872006-07-15 01:52:39 +000051};
52
53class Synchronized {
54 public:
Marc Slemko3a3b53b2007-05-22 23:59:54 +000055
Mark Slee2f6404d2006-10-10 01:37:40 +000056 Synchronized(const Monitor& value) :
57 monitor_(value) {
58 monitor_.lock();
Marc Slemko66949872006-07-15 01:52:39 +000059 }
60
61 ~Synchronized() {
Mark Slee2f6404d2006-10-10 01:37:40 +000062 monitor_.unlock();
Marc Slemko66949872006-07-15 01:52:39 +000063 }
64
65 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000066 const Monitor& monitor_;
Marc Slemko66949872006-07-15 01:52:39 +000067};
68
69
70}}} // facebook::thrift::concurrency
71
Mark Sleef5f2be42006-09-05 21:05:31 +000072#endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_