From 13f18fe5f0b5d1bc6ee628727707a64f33e8e163 Mon Sep 17 00:00:00 2001 From: kevin Date: Sun, 18 Feb 2024 08:50:49 -0500 Subject: [PATCH] cwMutex.cpp : Added use of _future_ms() to handle timing based on CLOCK_REALTIME for all time dependent calls in cwMutex.cpp. --- cwMutex.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/cwMutex.cpp b/cwMutex.cpp index d60ab75..4b8df35 100644 --- a/cwMutex.cpp +++ b/cwMutex.cpp @@ -36,6 +36,15 @@ namespace cw return rc; } + + time::spec_t _future_ms(unsigned timeout_milliseconds) + { + time::spec_t ts; + clock_gettime(CLOCK_REALTIME,&ts); + time::advanceMs(ts,timeout_milliseconds); + return ts; + } + } } @@ -102,14 +111,15 @@ cw::rc_t cw::mutex::lock( handle_t h, unsigned timeout_milliseconds ) rc_t rc = kOkRC; mutex_t* p = _handleToPtr(h); int sysRc; - time::spec_t ts; + time::spec_t ts = _future_ms(timeout_milliseconds); // Apparently timedlock depends on using CLOCK_REALTIME vs CLOCK_MONOTONIC // so we can't call to time::current_time() which uses CLOCK_MONOTONIC. // TODO: See this: https://stackoverflow.com/questions/14248033/clock-monotonic-and-pthread-mutex-timedlock-pthread-cond-timedwait // and consider changing this to use CLOCK_MONOTONIC. - clock_gettime(CLOCK_REALTIME,&ts); - time::advanceMs(ts,timeout_milliseconds); + //clock_gettime(CLOCK_REALTIME,&ts); + //time::advanceMs(ts,timeout_milliseconds); + switch(sysRc = pthread_mutex_timedlock(&p->mutex,&ts) ) @@ -174,9 +184,10 @@ cw::rc_t cw::mutex::waitOnCondVar( handle_t h, bool lockThenWaitFl, unsigned tim else // ... otherwise use the cond. var. wait with timeout API { - struct timespec ts; + struct timespec ts = _future_ms(timeOutMs); - if((rc = time::futureMs(ts,timeOutMs)) == kOkRC ) + + //if((rc = time::futureMs(ts,timeOutMs)) == kOkRC ) if((sysRC = pthread_cond_timedwait(&p->cvar,&p->mutex,&ts)) != 0 ) { if( sysRC == ETIMEDOUT )