]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | */ | |
19 | ||
20 | #ifndef _THRIFT_CONCURRENCY_MONITOR_H_ | |
21 | #define _THRIFT_CONCURRENCY_MONITOR_H_ 1 | |
22 | ||
23 | #include <chrono> | |
24 | #include <thrift/concurrency/Exception.h> | |
25 | #include <thrift/concurrency/Mutex.h> | |
26 | ||
27 | namespace apache { | |
28 | namespace thrift { | |
29 | namespace concurrency { | |
30 | ||
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 | |
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. | |
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 | * | |
47 | * @version $Id:$ | |
48 | */ | |
49 | class Monitor : boost::noncopyable { | |
50 | public: | |
51 | /** Creates a new mutex, and takes ownership of it. */ | |
52 | Monitor(); | |
53 | ||
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. */ | |
61 | virtual ~Monitor(); | |
62 | ||
63 | Mutex& mutex() const; | |
64 | ||
65 | virtual void lock() const; | |
66 | ||
67 | virtual void unlock() const; | |
68 | ||
69 | /** | |
70 | * Waits a maximum of the specified timeout in milliseconds for the condition | |
71 | * to occur, or waits forever if timeout is zero. | |
72 | * | |
73 | * Returns 0 if condition occurs, THRIFT_ETIMEDOUT on timeout, or an error code. | |
74 | */ | |
75 | int waitForTimeRelative(const std::chrono::milliseconds &timeout) const; | |
76 | ||
77 | int waitForTimeRelative(uint64_t timeout_ms) const { return waitForTimeRelative(std::chrono::milliseconds(timeout_ms)); } | |
78 | ||
79 | /** | |
80 | * Waits until the absolute time specified by abstime. | |
81 | * Returns 0 if condition occurs, THRIFT_ETIMEDOUT on timeout, or an error code. | |
82 | */ | |
83 | int waitForTime(const std::chrono::time_point<std::chrono::steady_clock>& abstime) const; | |
84 | ||
85 | /** | |
86 | * Waits forever until the condition occurs. | |
87 | * Returns 0 if condition occurs, or an error code otherwise. | |
88 | */ | |
89 | int waitForever() const; | |
90 | ||
91 | /** | |
92 | * Exception-throwing version of waitForTimeRelative(), called simply | |
93 | * wait(std::chrono::milliseconds) for historical reasons. Timeout is in milliseconds. | |
94 | * | |
95 | * If the condition occurs, this function returns cleanly; on timeout or | |
96 | * error an exception is thrown. | |
97 | */ | |
98 | void wait(const std::chrono::milliseconds &timeout) const; | |
99 | ||
100 | void wait(uint64_t timeout_ms = 0ULL) const { this->wait(std::chrono::milliseconds(timeout_ms)); } | |
101 | ||
102 | /** Wakes up one thread waiting on this monitor. */ | |
103 | virtual void notify() const; | |
104 | ||
105 | /** Wakes up all waiting threads on this monitor. */ | |
106 | virtual void notifyAll() const; | |
107 | ||
108 | private: | |
109 | class Impl; | |
110 | ||
111 | Impl* impl_; | |
112 | }; | |
113 | ||
114 | class Synchronized { | |
115 | public: | |
116 | Synchronized(const Monitor* monitor) : g(monitor->mutex()) {} | |
117 | Synchronized(const Monitor& monitor) : g(monitor.mutex()) {} | |
118 | ||
119 | private: | |
120 | Guard g; | |
121 | }; | |
122 | } | |
123 | } | |
124 | } // apache::thrift::concurrency | |
125 | ||
126 | #endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_ |