diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/common/condition_variable_debug.cc | |
parent | Initial commit. (diff) | |
download | ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/common/condition_variable_debug.cc')
-rw-r--r-- | src/common/condition_variable_debug.cc | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/common/condition_variable_debug.cc b/src/common/condition_variable_debug.cc new file mode 100644 index 00000000..a2ddd769 --- /dev/null +++ b/src/common/condition_variable_debug.cc @@ -0,0 +1,77 @@ +#include "condition_variable_debug.h" +#include "common/mutex_debug.h" + +namespace ceph { + +condition_variable_debug::condition_variable_debug() + : waiter_mutex{nullptr} +{ + int r = pthread_cond_init(&cond, nullptr); + if (r) { + throw std::system_error(r, std::generic_category()); + } +} + +condition_variable_debug::~condition_variable_debug() +{ + pthread_cond_destroy(&cond); +} + +void condition_variable_debug::wait(std::unique_lock<mutex_debug>& lock) +{ + // make sure this cond is used with one mutex only + ceph_assert(waiter_mutex == nullptr || + waiter_mutex == lock.mutex()); + waiter_mutex = lock.mutex(); + ceph_assert(waiter_mutex->is_locked()); + waiter_mutex->_pre_unlock(); + if (int r = pthread_cond_wait(&cond, waiter_mutex->native_handle()); + r != 0) { + throw std::system_error(r, std::generic_category()); + } + waiter_mutex->_post_lock(); +} + +void condition_variable_debug::notify_one() +{ + // make sure signaler is holding the waiter's lock. + ceph_assert(waiter_mutex == nullptr || + waiter_mutex->is_locked()); + if (int r = pthread_cond_signal(&cond); r != 0) { + throw std::system_error(r, std::generic_category()); + } +} + +void condition_variable_debug::notify_all(bool sloppy) +{ + // make sure signaler is holding the waiter's lock. + ceph_assert(waiter_mutex == NULL || + waiter_mutex->is_locked()); + if (int r = pthread_cond_broadcast(&cond); r != 0 && !sloppy) { + throw std::system_error(r, std::generic_category()); + } +} + +std::cv_status condition_variable_debug::_wait_until(mutex_debug* mutex, + timespec* ts) +{ + // make sure this cond is used with one mutex only + ceph_assert(waiter_mutex == nullptr || + waiter_mutex == mutex); + waiter_mutex = mutex; + ceph_assert(waiter_mutex->is_locked()); + + waiter_mutex->_pre_unlock(); + int r = pthread_cond_timedwait(&cond, waiter_mutex->native_handle(), ts); + waiter_mutex->_post_lock(); + switch (r) { + case 0: + return std::cv_status::no_timeout; + case ETIMEDOUT: + return std::cv_status::timeout; + default: + throw std::system_error(r, std::generic_category()); + } +} + +} // namespace ceph |