diff --git a/cwMutex.cpp b/cwMutex.cpp index 8368621..d60ab75 100644 --- a/cwMutex.cpp +++ b/cwMutex.cpp @@ -104,7 +104,11 @@ cw::rc_t cw::mutex::lock( handle_t h, unsigned timeout_milliseconds ) int sysRc; 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); diff --git a/cwTime.cpp b/cwTime.cpp index 7061c29..cfefc8a 100644 --- a/cwTime.cpp +++ b/cwTime.cpp @@ -44,11 +44,16 @@ void cw::time::get( spec_t& t ) #include // gettimeofday() void cw::time::get( spec_t& t ) { - // NOTcw::mutex::lock(h,timeout) relies on using - // CLOCK_REALTIME. If the source of this clock changes - // then change cw::mutex::loc(h,timeout) as well - clock_gettime(CLOCK_REALTIME,&t); + clock_gettime(CLOCK_MONOTONIC,&t); } + +cw::time::spec_t cw::time::current_time() +{ + spec_t t; + clock_gettime(CLOCK_REALTIME,&t); + return t; +} + #endif // this assumes that the seconds have been normalized to a recent start time @@ -177,8 +182,8 @@ cw::rc_t cw::time::now( spec_t& ts ) memset(&ts,0,sizeof(ts)); - if((errRC = clock_gettime(CLOCK_REALTIME, &ts)) != 0 ) - rc = cwLogSysError(kInvalidOpRC,errRC,"Unable to obtain system time."); + if((errRC = clock_gettime(CLOCK_MONOTONIC, &ts)) != 0 ) + rc = cwLogSysError(kInvalidOpRC,errRC,"Unable to obtain system time."); return rc; } diff --git a/cwTime.h b/cwTime.h index 6288c60..faae9b6 100644 --- a/cwTime.h +++ b/cwTime.h @@ -20,6 +20,7 @@ namespace cw // Get the time void get( spec_t& tRef ); + spec_t current_time(); // same as get() // Return the elapsed time (t1 - t0) in microseconds // t1 is assumed to be at a later time than t0. @@ -61,7 +62,7 @@ namespace cw 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 );