]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/cpp/src/thrift/concurrency/Monitor.h
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 #ifndef _THRIFT_CONCURRENCY_MONITOR_H_
21 #define _THRIFT_CONCURRENCY_MONITOR_H_ 1
24 #include <thrift/concurrency/Exception.h>
25 #include <thrift/concurrency/Mutex.h>
29 namespace concurrency
{
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
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.
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.
49 class Monitor
: boost::noncopyable
{
51 /** Creates a new mutex, and takes ownership of it. */
54 /** Uses the provided mutex without taking ownership. */
55 explicit Monitor(Mutex
* mutex
);
57 /** Uses the mutex inside the provided Monitor without taking ownership. */
58 explicit Monitor(Monitor
* monitor
);
60 /** Deallocates the mutex only if we own it. */
65 virtual void lock() const;
67 virtual void unlock() const;
70 * Waits a maximum of the specified timeout in milliseconds for the condition
71 * to occur, or waits forever if timeout is zero.
73 * Returns 0 if condition occurs, THRIFT_ETIMEDOUT on timeout, or an error code.
75 int waitForTimeRelative(const std::chrono::milliseconds
&timeout
) const;
77 int waitForTimeRelative(uint64_t timeout_ms
) const { return waitForTimeRelative(std::chrono::milliseconds(timeout_ms
)); }
80 * Waits until the absolute time specified by abstime.
81 * Returns 0 if condition occurs, THRIFT_ETIMEDOUT on timeout, or an error code.
83 int waitForTime(const std::chrono::time_point
<std::chrono::steady_clock
>& abstime
) const;
86 * Waits forever until the condition occurs.
87 * Returns 0 if condition occurs, or an error code otherwise.
89 int waitForever() const;
92 * Exception-throwing version of waitForTimeRelative(), called simply
93 * wait(std::chrono::milliseconds) for historical reasons. Timeout is in milliseconds.
95 * If the condition occurs, this function returns cleanly; on timeout or
96 * error an exception is thrown.
98 void wait(const std::chrono::milliseconds
&timeout
) const;
100 void wait(uint64_t timeout_ms
= 0ULL) const { this->wait(std::chrono::milliseconds(timeout_ms
)); }
102 /** Wakes up one thread waiting on this monitor. */
103 virtual void notify() const;
105 /** Wakes up all waiting threads on this monitor. */
106 virtual void notifyAll() const;
116 Synchronized(const Monitor
* monitor
) : g(monitor
->mutex()) {}
117 Synchronized(const Monitor
& monitor
) : g(monitor
.mutex()) {}
124 } // apache::thrift::concurrency
126 #endif // #ifndef _THRIFT_CONCURRENCY_MONITOR_H_