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" |
| 24 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 25 | namespace apache { namespace thrift { namespace concurrency { |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 26 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 27 | /** |
| 28 | * A monitor is a combination mutex and condition-event. Waiting and |
| 29 | * notifying condition events requires that the caller own the mutex. Mutex |
| 30 | * lock and unlock operations can be performed independently of condition |
| 31 | * events. This is more or less analogous to java.lang.Object multi-thread |
| 32 | * operations |
| 33 | * |
| 34 | * Note that all methods are const. Monitors implement logical constness, not |
| 35 | * bit constness. This allows const methods to call monitor methods without |
| 36 | * needing to cast away constness or change to non-const signatures. |
| 37 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 38 | * @version $Id:$ |
| 39 | */ |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 40 | class Monitor { |
| 41 | |
| 42 | public: |
| 43 | |
| 44 | Monitor(); |
| 45 | |
| 46 | virtual ~Monitor(); |
| 47 | |
| 48 | virtual void lock() const; |
| 49 | |
| 50 | virtual void unlock() const; |
| 51 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 52 | virtual void wait(int64_t timeout=0LL) const; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 53 | |
| 54 | virtual void notify() const; |
| 55 | |
| 56 | virtual void notifyAll() const; |
| 57 | |
| 58 | private: |
| 59 | |
| 60 | class Impl; |
| 61 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 62 | Impl* impl_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | class Synchronized { |
| 66 | public: |
Marc Slemko | 3a3b53b | 2007-05-22 23:59:54 +0000 | [diff] [blame] | 67 | |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 68 | Synchronized(const Monitor& value) : |
| 69 | monitor_(value) { |
| 70 | monitor_.lock(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | ~Synchronized() { |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 74 | monitor_.unlock(); |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame] | 78 | const Monitor& monitor_; |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 82 | }}} // apache::thrift::concurrency |
Marc Slemko | 6694987 | 2006-07-15 01:52:39 +0000 | [diff] [blame] | 83 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 84 | #endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_ |