]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/condition_variable_debug.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / common / condition_variable_debug.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #pragma once
5
6 #include <condition_variable>
7 #include <ctime>
8 #include <pthread.h>
9 #include "common/ceph_time.h"
10
11 namespace ceph {
12
13 namespace mutex_debug_detail {
14 template<bool> class mutex_debug_impl;
15 }
16
17 class condition_variable_debug {
18 using mutex_debug = mutex_debug_detail::mutex_debug_impl<false>;
19
20 pthread_cond_t cond;
21 mutex_debug* waiter_mutex;
22
23 condition_variable_debug&
24 operator=(const condition_variable_debug&) = delete;
25 condition_variable_debug(const condition_variable_debug&) = delete;
26
27 public:
28 condition_variable_debug();
29 ~condition_variable_debug();
30 void wait(std::unique_lock<mutex_debug>& lock);
31 template<class Predicate>
32 void wait(std::unique_lock<mutex_debug>& lock, Predicate pred) {
33 while (!pred()) {
34 wait(lock);
35 }
36 }
37 template<class Clock, class Duration>
38 std::cv_status wait_until(
39 std::unique_lock<mutex_debug>& lock,
40 const std::chrono::time_point<Clock, Duration>& when) {
41 timespec ts = when.to_timespec(when);
42 return _wait_until(lock.mutex(), &ts);
43 }
44 template<class Rep, class Period>
45 std::cv_status wait_for(
46 std::unique_lock<mutex_debug>& lock,
47 const std::chrono::duration<Rep, Period>& awhile) {
48 ceph::real_time when{ceph::real_clock::now()};
49 when += awhile;
50 timespec ts = ceph::real_clock::to_timespec(when);
51 return _wait_until(lock.mutex(), &ts);
52 }
53 void notify_one();
54 void notify_all(bool sloppy = false);
55 private:
56 std::cv_status _wait_until(mutex_debug* mutex, timespec* ts);
57 };
58
59 } // namespace ceph