fix cond.h time

This commit is contained in:
obdev
2023-08-11 06:12:35 +00:00
committed by ob-robot
parent 727e11ecf3
commit f52cecaaa2
2 changed files with 19 additions and 3 deletions

View File

@ -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);
}

View File

@ -71,6 +71,7 @@ private:
template <typename M> bool timed_wait_impl(const M&, const ObSysTime&) const;
mutable pthread_cond_t _cond;
mutable pthread_condattr_t _attr;
};
template <typename M> 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;