From f52cecaaa21627f89cbaf8468da9d40a627b1076 Mon Sep 17 00:00:00 2001 From: obdev Date: Fri, 11 Aug 2023 06:12:35 +0000 Subject: [PATCH] fix cond.h time --- deps/oblib/src/lib/lock/cond.cpp | 19 +++++++++++++++++-- deps/oblib/src/lib/lock/cond.h | 3 ++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/deps/oblib/src/lib/lock/cond.cpp b/deps/oblib/src/lib/lock/cond.cpp index f72728b43f..d0190b4ce0 100644 --- a/deps/oblib/src/lib/lock/cond.cpp +++ b/deps/oblib/src/lib/lock/cond.cpp @@ -20,7 +20,18 @@ namespace obutil { Cond::Cond() { - int rt = pthread_cond_init(&_cond, NULL); + + int rt = pthread_condattr_init(&_attr); + if (0 != rt) { + _OB_LOG_RET(WARN, OB_ERR_SYS, "Failed to init cond attr, err=%d", rt); + } + // Set the attribute to use CLOCK_MONOTONIC clock source + rt = pthread_condattr_setclock(&_attr, CLOCK_MONOTONIC); + if (0 != rt) { + _OB_LOG_RET(WARN, OB_ERR_SYS, "Failed to set MONOTONIC Clock, err=%d", rt); + } + + rt = pthread_cond_init(&_cond, &_attr); if (0 != rt) { _OB_LOG_RET(WARN, OB_ERR_SYS, "Failed to init cond, err=%d", rt); } @@ -28,7 +39,11 @@ Cond::Cond() Cond::~Cond() { - int rt = pthread_cond_destroy(&_cond); + int rt = pthread_condattr_destroy(&_attr); + if (0 != rt) { + _OB_LOG_RET(WARN, OB_ERR_SYS, "Failed to destroy cond attr, err=%d", rt); + } + rt = pthread_cond_destroy(&_cond); if (0 != rt) { _OB_LOG_RET(WARN, OB_ERR_SYS, "Failed to destroy cond, err=%d", rt); } diff --git a/deps/oblib/src/lib/lock/cond.h b/deps/oblib/src/lib/lock/cond.h index b794bc0a2c..87f21b1be4 100644 --- a/deps/oblib/src/lib/lock/cond.h +++ b/deps/oblib/src/lib/lock/cond.h @@ -71,6 +71,7 @@ private: template bool timed_wait_impl(const M&, const ObSysTime&) const; mutable pthread_cond_t _cond; + mutable pthread_condattr_t _attr; }; template inline bool @@ -104,7 +105,7 @@ Cond::timed_wait_impl(const M& mutex, const ObSysTime& timeout) const LockState state; mutex.unlock(state); - timeval tv = ObSysTime::now(ObSysTime::Realtime) + timeout; + timeval tv = ObSysTime::now(ObSysTime::Monotonic) + timeout; timespec ts; ts.tv_sec = tv.tv_sec; ts.tv_nsec = tv.tv_usec * 1000;