David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_CONCURRENCY_MONITOR_H_ |
| 21 | #define _THRIFT_CONCURRENCY_MONITOR_H_ 1 |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 22 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 23 | #include "Exception.h" |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 24 | #include "Mutex.h" |
| 25 | |
| 26 | #include <boost/utility.hpp> |
| 27 | |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 28 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 29 | namespace apache { namespace thrift { namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 30 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 31 | /** |
| 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 Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 36 | * 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 Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 42 | * |
| 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 Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 47 | * @version $Id:$ |
| 48 | */ |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 49 | class Monitor : boost::noncopyable { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 50 | public: |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 51 | /** Creates a new mutex, and takes ownership of it. */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 52 | Monitor(); |
| 53 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 54 | /** 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 Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 61 | virtual ~Monitor(); |
| 62 | |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 63 | Mutex& mutex() const; |
| 64 | |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 65 | virtual void lock() const; |
| 66 | |
| 67 | virtual void unlock() const; |
| 68 | |
Bryan Duxbury | 3746b29 | 2011-08-24 00:35:52 +0000 | [diff] [blame] | 69 | /** |
| 70 | * Waits a maximum of the specified timeout in milliseconds for the condition |
| 71 | * to occur, or waits forever if timeout_ms == 0. |
| 72 | * |
| 73 | * Returns 0 if condition occurs, ETIMEDOUT on timeout, or an error code. |
| 74 | */ |
| 75 | int waitForTimeRelative(int64_t timeout_ms) const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 76 | |
Bryan Duxbury | 3746b29 | 2011-08-24 00:35:52 +0000 | [diff] [blame] | 77 | /** |
| 78 | * Waits until the absolute time specified using struct timespec. |
| 79 | * Returns 0 if condition occurs, ETIMEDOUT on timeout, or an error code. |
| 80 | */ |
| 81 | int waitForTime(const timespec* abstime) const; |
| 82 | |
| 83 | /** |
| 84 | * Waits forever until the condition occurs. |
| 85 | * Returns 0 if condition occurs, or an error code otherwise. |
| 86 | */ |
| 87 | int waitForever() const; |
| 88 | |
| 89 | /** |
| 90 | * Exception-throwing version of waitForTimeRelative(), called simply |
| 91 | * wait(int64) for historical reasons. Timeout is in milliseconds. |
| 92 | * |
| 93 | * If the condition occurs, this function returns cleanly; on timeout or |
| 94 | * error an exception is thrown. |
| 95 | */ |
| 96 | void wait(int64_t timeout_ms = 0LL) const; |
| 97 | |
| 98 | |
| 99 | /** Wakes up one thread waiting on this monitor. */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 100 | virtual void notify() const; |
| 101 | |
Bryan Duxbury | 3746b29 | 2011-08-24 00:35:52 +0000 | [diff] [blame] | 102 | /** Wakes up all waiting threads on this monitor. */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 103 | virtual void notifyAll() const; |
| 104 | |
| 105 | private: |
| 106 | |
| 107 | class Impl; |
| 108 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 109 | Impl* impl_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | class Synchronized { |
| 113 | public: |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 114 | Synchronized(const Monitor* monitor) : g(monitor->mutex()) { } |
| 115 | Synchronized(const Monitor& monitor) : g(monitor.mutex()) { } |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 116 | |
| 117 | private: |
David Reiss | b9db49c | 2010-03-09 05:19:30 +0000 | [diff] [blame] | 118 | Guard g; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 122 | }}} // apache::thrift::concurrency |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 123 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 124 | #endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_ |