]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/condition_variable_debug.cc
1 #include "condition_variable_debug.h"
2 #include "common/mutex_debug.h"
6 condition_variable_debug::condition_variable_debug()
7 : waiter_mutex
{nullptr}
9 int r
= pthread_cond_init(&cond
, nullptr);
11 throw std::system_error(r
, std::generic_category());
15 condition_variable_debug::~condition_variable_debug()
17 pthread_cond_destroy(&cond
);
20 void condition_variable_debug::wait(std::unique_lock
<mutex_debug
>& lock
)
22 // make sure this cond is used with one mutex only
23 ceph_assert(waiter_mutex
== nullptr ||
24 waiter_mutex
== lock
.mutex());
25 waiter_mutex
= lock
.mutex();
26 ceph_assert(waiter_mutex
->is_locked());
27 waiter_mutex
->_pre_unlock();
28 if (int r
= pthread_cond_wait(&cond
, waiter_mutex
->native_handle());
30 throw std::system_error(r
, std::generic_category());
32 waiter_mutex
->_post_lock();
35 void condition_variable_debug::notify_one()
37 // make sure signaler is holding the waiter's lock.
38 ceph_assert(waiter_mutex
== nullptr ||
39 waiter_mutex
->is_locked());
40 if (int r
= pthread_cond_signal(&cond
); r
!= 0) {
41 throw std::system_error(r
, std::generic_category());
45 void condition_variable_debug::notify_all(bool sloppy
)
48 // make sure signaler is holding the waiter's lock.
49 ceph_assert(waiter_mutex
== NULL
||
50 waiter_mutex
->is_locked());
52 if (int r
= pthread_cond_broadcast(&cond
); r
!= 0 && !sloppy
) {
53 throw std::system_error(r
, std::generic_category());
57 std::cv_status
condition_variable_debug::_wait_until(mutex_debug
* mutex
,
60 // make sure this cond is used with one mutex only
61 ceph_assert(waiter_mutex
== nullptr ||
62 waiter_mutex
== mutex
);
64 ceph_assert(waiter_mutex
->is_locked());
66 waiter_mutex
->_pre_unlock();
67 int r
= pthread_cond_timedwait(&cond
, waiter_mutex
->native_handle(), ts
);
68 waiter_mutex
->_post_lock();
71 return std::cv_status::no_timeout
;
73 return std::cv_status::timeout
;
75 throw std::system_error(r
, std::generic_category());