PISystemTime toTimespec and PIConditionVariable fix

This commit is contained in:
2020-09-25 14:08:40 +03:00
parent d180c0c0dd
commit c3f0cc0b03
3 changed files with 19 additions and 15 deletions

View File

@@ -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 { PISystemTime PISystemTime::abs() const {
if (seconds < 0) if (seconds < 0)
return PISystemTime(piAbsl(seconds) - 1, 1000000000l - piAbsl(nanoseconds)); return PISystemTime(piAbsl(seconds) - 1, 1000000000l - piAbsl(nanoseconds));
@@ -255,12 +262,12 @@ PISystemTime PISystemTime::current(bool precise_but_not_system) {
timeval tv; timeval tv;
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 0; tv.tv_usec = 0;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
t_cur.tv_sec = tv.tv_sec; t_cur.tv_sec = tv.tv_sec;
t_cur.tv_nsec = tv.tv_usec * 1000; t_cur.tv_nsec = tv.tv_usec * 1000;
# else # else
timespec t_cur; timespec t_cur;
clock_gettime(0, &t_cur); clock_gettime(precise_but_not_system ? CLOCK_MONOTONIC : 0, &t_cur);
# endif # endif
# endif # endif
return PISystemTime(t_cur.tv_sec, t_cur.tv_nsec); return PISystemTime(t_cur.tv_sec, t_cur.tv_nsec);

View File

@@ -93,6 +93,8 @@ public:
//! If you call this function on system time returned with \a PISystemTime::current() thread will be sleep almost forever. //! 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) 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 //! Returns copy of this system time with absolutely values of s and ns
PISystemTime abs() const; PISystemTime abs() const;

View File

@@ -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 PIConditionVariable::waitFor(PIMutex &lk, int timeoutMs) {
bool isNotTimeout; bool isNotTimeout;
#ifdef WINDOWS #ifdef WINDOWS
isNotTimeout = SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), timeoutMs) != 0; isNotTimeout = SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), timeoutMs) != 0;
#else #else
timespec expire_ts; timespec expire_ts;
clock_gettime(CLOCK_MONOTONIC, &expire_ts); PISystemTime st = PISystemTime::current(true);
timespec_add_ms(&expire_ts, timeoutMs); st.addMilliseconds(timeoutMs);
st.toTimespec(&expire_ts);
isNotTimeout = pthread_cond_timedwait(&PRIVATE->nativeHandle, (pthread_mutex_t*)lk.handle(), &expire_ts) == 0; isNotTimeout = pthread_cond_timedwait(&PRIVATE->nativeHandle, (pthread_mutex_t*)lk.handle(), &expire_ts) == 0;
#endif #endif
if (PRIVATE->isDestroying) return false; if (PRIVATE->isDestroying) return false;
return isNotTimeout; return isNotTimeout;
} }
bool PIConditionVariable::waitFor(PIMutex& lk, int timeoutMs, const std::function<bool()> &condition) { bool PIConditionVariable::waitFor(PIMutex& lk, int timeoutMs, const std::function<bool()> &condition) {
bool isCondition; bool isCondition;
#ifdef WINDOWS #ifdef WINDOWS
PITimeMeasurer measurer; PITimeMeasurer measurer;
#else #else
timespec expire_ts; timespec expire_ts;
clock_gettime(CLOCK_MONOTONIC, &expire_ts); PISystemTime st = PISystemTime::current(true);
timespec_add_ms(&expire_ts, timeoutMs); st.addMilliseconds(timeoutMs);
st.toTimespec(&expire_ts);
#endif #endif
while (true) { while (true) {
isCondition = condition(); isCondition = condition();