PISystemTime toTimespec and PIConditionVariable fix
This commit is contained in:
@@ -86,37 +86,32 @@ void PIConditionVariable::wait(PIMutex& lk, const std::function<bool()>& conditi
|
||||
}
|
||||
}
|
||||
|
||||
void timespec_add_ms(timespec* ts, int ms) {
|
||||
ts->tv_sec += ms / 1000;
|
||||
ts->tv_nsec += ms % 1000 * 1000000;
|
||||
if (ts->tv_nsec > 1000 * 1000 * 1000) {
|
||||
ts->tv_sec++;
|
||||
ts->tv_nsec -= 1000 * 1000 * 1000;
|
||||
}
|
||||
}
|
||||
|
||||
bool PIConditionVariable::waitFor(PIMutex &lk, int timeoutMs) {
|
||||
bool isNotTimeout;
|
||||
#ifdef WINDOWS
|
||||
isNotTimeout = SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), timeoutMs) != 0;
|
||||
#else
|
||||
timespec expire_ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &expire_ts);
|
||||
timespec_add_ms(&expire_ts, timeoutMs);
|
||||
timespec expire_ts;
|
||||
PISystemTime st = PISystemTime::current(true);
|
||||
st.addMilliseconds(timeoutMs);
|
||||
st.toTimespec(&expire_ts);
|
||||
isNotTimeout = pthread_cond_timedwait(&PRIVATE->nativeHandle, (pthread_mutex_t*)lk.handle(), &expire_ts) == 0;
|
||||
#endif
|
||||
if (PRIVATE->isDestroying) return false;
|
||||
return isNotTimeout;
|
||||
}
|
||||
|
||||
|
||||
bool PIConditionVariable::waitFor(PIMutex& lk, int timeoutMs, const std::function<bool()> &condition) {
|
||||
bool isCondition;
|
||||
#ifdef WINDOWS
|
||||
PITimeMeasurer measurer;
|
||||
#else
|
||||
timespec expire_ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &expire_ts);
|
||||
timespec_add_ms(&expire_ts, timeoutMs);
|
||||
PISystemTime st = PISystemTime::current(true);
|
||||
st.addMilliseconds(timeoutMs);
|
||||
st.toTimespec(&expire_ts);
|
||||
#endif
|
||||
while (true) {
|
||||
isCondition = condition();
|
||||
|
||||
Reference in New Issue
Block a user