PISystemTime toTimespec and PIConditionVariable fix
This commit is contained in:
@@ -212,6 +212,13 @@ PIDateTime PIDateTime::current() {
|
||||
}
|
||||
|
||||
|
||||
void PISystemTime::toTimespec(void * ts) {
|
||||
#ifndef WINDOWS
|
||||
t_cur->tv_sec = seconds;
|
||||
t_cur->tv_nsec = nanoseconds;
|
||||
#endif
|
||||
}
|
||||
|
||||
PISystemTime PISystemTime::abs() const {
|
||||
if (seconds < 0)
|
||||
return PISystemTime(piAbsl(seconds) - 1, 1000000000l - piAbsl(nanoseconds));
|
||||
@@ -255,12 +262,12 @@ PISystemTime PISystemTime::current(bool precise_but_not_system) {
|
||||
timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 0;
|
||||
gettimeofday(&tv, NULL);
|
||||
gettimeofday(&tv, NULL);
|
||||
t_cur.tv_sec = tv.tv_sec;
|
||||
t_cur.tv_nsec = tv.tv_usec * 1000;
|
||||
# else
|
||||
timespec t_cur;
|
||||
clock_gettime(0, &t_cur);
|
||||
clock_gettime(precise_but_not_system ? CLOCK_MONOTONIC : 0, &t_cur);
|
||||
# endif
|
||||
# endif
|
||||
return PISystemTime(t_cur.tv_sec, t_cur.tv_nsec);
|
||||
|
||||
@@ -93,6 +93,8 @@ public:
|
||||
//! If you call this function on system time returned with \a PISystemTime::current() thread will be sleep almost forever.
|
||||
void sleep() {piUSleep(piFloord(toMicroseconds()));} // wait self value, useful to wait some dT = (t1 - t0)
|
||||
|
||||
//! On Unix system assign current value to timespec struct
|
||||
void toTimespec(void *ts);
|
||||
|
||||
//! Returns copy of this system time with absolutely values of s and ns
|
||||
PISystemTime abs() const;
|
||||
|
||||
@@ -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