blob: f29119d3ad24cfd2c3d78ac817fbc09a6616f018 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_CONCURRENCY_MONITOR_H_
21#define _THRIFT_CONCURRENCY_MONITOR_H_ 1
Marc Slemko66949872006-07-15 01:52:39 +000022
Marc Slemko3a3b53b2007-05-22 23:59:54 +000023#include "Exception.h"
David Reissb9db49c2010-03-09 05:19:30 +000024#include "Mutex.h"
25
26#include <boost/utility.hpp>
27
Marc Slemko3a3b53b2007-05-22 23:59:54 +000028
T Jake Lucianib5e62212009-01-31 22:36:20 +000029namespace apache { namespace thrift { namespace concurrency {
Marc Slemko66949872006-07-15 01:52:39 +000030
Mark Sleef5f2be42006-09-05 21:05:31 +000031/**
32 * A monitor is a combination mutex and condition-event. Waiting and
33 * notifying condition events requires that the caller own the mutex. Mutex
34 * lock and unlock operations can be performed independently of condition
35 * events. This is more or less analogous to java.lang.Object multi-thread
David Reissb9db49c2010-03-09 05:19:30 +000036 * operations.
37 *
38 * Note the Monitor can create a new, internal mutex; alternatively, a
39 * separate Mutex can be passed in and the Monitor will re-use it without
40 * taking ownership. It's the user's responsibility to make sure that the
41 * Mutex is not deallocated before the Monitor.
Mark Sleef5f2be42006-09-05 21:05:31 +000042 *
43 * Note that all methods are const. Monitors implement logical constness, not
44 * bit constness. This allows const methods to call monitor methods without
45 * needing to cast away constness or change to non-const signatures.
46 *
Mark Sleef5f2be42006-09-05 21:05:31 +000047 * @version $Id:$
48 */
David Reissb9db49c2010-03-09 05:19:30 +000049class Monitor : boost::noncopyable {
Marc Slemko66949872006-07-15 01:52:39 +000050 public:
David Reissb9db49c2010-03-09 05:19:30 +000051 /** Creates a new mutex, and takes ownership of it. */
Marc Slemko66949872006-07-15 01:52:39 +000052 Monitor();
53
David Reissb9db49c2010-03-09 05:19:30 +000054 /** Uses the provided mutex without taking ownership. */
55 explicit Monitor(Mutex* mutex);
56
57 /** Uses the mutex inside the provided Monitor without taking ownership. */
58 explicit Monitor(Monitor* monitor);
59
60 /** Deallocates the mutex only if we own it. */
Marc Slemko66949872006-07-15 01:52:39 +000061 virtual ~Monitor();
62
David Reissb9db49c2010-03-09 05:19:30 +000063 Mutex& mutex() const;
64
Marc Slemko66949872006-07-15 01:52:39 +000065 virtual void lock() const;
66
67 virtual void unlock() const;
68
Mark Slee9b82d272007-05-23 05:16:07 +000069 virtual void wait(int64_t timeout=0LL) const;
Marc Slemko66949872006-07-15 01:52:39 +000070
71 virtual void notify() const;
72
73 virtual void notifyAll() const;
74
75 private:
76
77 class Impl;
78
Mark Slee2f6404d2006-10-10 01:37:40 +000079 Impl* impl_;
Marc Slemko66949872006-07-15 01:52:39 +000080};
81
82class Synchronized {
83 public:
David Reissb9db49c2010-03-09 05:19:30 +000084 Synchronized(const Monitor* monitor) : g(monitor->mutex()) { }
85 Synchronized(const Monitor& monitor) : g(monitor.mutex()) { }
Marc Slemko66949872006-07-15 01:52:39 +000086
87 private:
David Reissb9db49c2010-03-09 05:19:30 +000088 Guard g;
Marc Slemko66949872006-07-15 01:52:39 +000089};
90
91
T Jake Lucianib5e62212009-01-31 22:36:20 +000092}}} // apache::thrift::concurrency
Marc Slemko66949872006-07-15 01:52:39 +000093
Mark Sleef5f2be42006-09-05 21:05:31 +000094#endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_