cwTime.h/cpp, cwMutex.cpp : Time now depends on CLOCK_MONOTONIC instead of CLOCK_REALTIME.

mutex::lock() apparently depends on CLOCK_REALTIME and so that is now handled with a direct call to clock_gettime().
This commit is contained in:
kevin 2024-02-10 11:49:47 -05:00
parent 28c8dfc982
commit e1aa033958
3 changed files with 18 additions and 8 deletions

View File

@ -104,7 +104,11 @@ cw::rc_t cw::mutex::lock( handle_t h, unsigned timeout_milliseconds )
int sysRc; int sysRc;
time::spec_t ts; time::spec_t ts;
time::get(ts); // 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); time::advanceMs(ts,timeout_milliseconds);

View File

@ -44,11 +44,16 @@ void cw::time::get( spec_t& t )
#include <sys/time.h> // gettimeofday() #include <sys/time.h> // gettimeofday()
void cw::time::get( spec_t& t ) void cw::time::get( spec_t& t )
{ {
// NOTcw::mutex::lock(h,timeout) relies on using clock_gettime(CLOCK_MONOTONIC,&t);
// CLOCK_REALTIME. If the source of this clock changes
// then change cw::mutex::loc(h,timeout) as well
clock_gettime(CLOCK_REALTIME,&t);
} }
cw::time::spec_t cw::time::current_time()
{
spec_t t;
clock_gettime(CLOCK_REALTIME,&t);
return t;
}
#endif #endif
// this assumes that the seconds have been normalized to a recent start time // this assumes that the seconds have been normalized to a recent start time
@ -177,7 +182,7 @@ cw::rc_t cw::time::now( spec_t& ts )
memset(&ts,0,sizeof(ts)); memset(&ts,0,sizeof(ts));
if((errRC = clock_gettime(CLOCK_REALTIME, &ts)) != 0 ) if((errRC = clock_gettime(CLOCK_MONOTONIC, &ts)) != 0 )
rc = cwLogSysError(kInvalidOpRC,errRC,"Unable to obtain system time."); rc = cwLogSysError(kInvalidOpRC,errRC,"Unable to obtain system time.");
return rc; return rc;

View File

@ -20,6 +20,7 @@ namespace cw
// Get the time // Get the time
void get( spec_t& tRef ); void get( spec_t& tRef );
spec_t current_time(); // same as get()
// Return the elapsed time (t1 - t0) in microseconds // Return the elapsed time (t1 - t0) in microseconds
// t1 is assumed to be at a later time than t0. // t1 is assumed to be at a later time than t0.
@ -61,7 +62,7 @@ namespace cw
void setZero( spec_t& t0 ); void setZero( spec_t& t0 );
rc_t now( spec_t& ts ); rc_t now( spec_t& ts ); // same as get()
void subtractMicros( spec_t& ts, unsigned us ); void subtractMicros( spec_t& ts, unsigned us );