fix(PIConditionVariable): clamp negative timeout in waitFor
When elapsed time exceeded the timeout, the remaining milliseconds expression went negative and was implicitly converted to DWORD, wrapping to ~0xFFFFFFFF (5 days) — effectively INFINITE. Now clamps to 0 and returns false when time has elapsed.
This commit is contained in:
@@ -135,17 +135,19 @@ bool PIConditionVariable::waitFor(PIMutex & lk, PISystemTime timeout, std::funct
|
|||||||
if (condition()) break;
|
if (condition()) break;
|
||||||
bool isTimeout;
|
bool isTimeout;
|
||||||
#if defined(WINDOWS)
|
#if defined(WINDOWS)
|
||||||
isTimeout = SleepConditionVariableCS(&PRIVATE->nativeHandle,
|
{
|
||||||
(PCRITICAL_SECTION)lk.handle(),
|
int remain = (int)(timeout.toMilliseconds() - (int)measurer.elapsed_m());
|
||||||
timeout.toMilliseconds() - (int)measurer.elapsed_m()) == 0;
|
if (remain <= 0) return false;
|
||||||
|
isTimeout = SleepConditionVariableCS(&PRIVATE->nativeHandle, (PCRITICAL_SECTION)lk.handle(), remain) == 0;
|
||||||
|
}
|
||||||
#elif defined(FREERTOS)
|
#elif defined(FREERTOS)
|
||||||
|
{
|
||||||
|
int remain = (int)(timeout.toMilliseconds() - (int)measurer.elapsed_m());
|
||||||
|
if (remain <= 0) return false;
|
||||||
EventBits_t uxBits;
|
EventBits_t uxBits;
|
||||||
uxBits = xEventGroupWaitBits(PRIVATE->nativeHandle,
|
uxBits = xEventGroupWaitBits(PRIVATE->nativeHandle, 1, pdTRUE, pdTRUE, remain / portTICK_PERIOD_MS);
|
||||||
1,
|
|
||||||
pdTRUE,
|
|
||||||
pdTRUE,
|
|
||||||
(timeout.toMilliseconds() - (int)measurer.elapsed_m()) / portTICK_PERIOD_MS);
|
|
||||||
isTimeout = (uxBits & 1) == 0;
|
isTimeout = (uxBits & 1) == 0;
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
isTimeout = pthread_cond_timedwait(&PRIVATE->nativeHandle, (pthread_mutex_t *)lk.handle(), &expire_ts) != 0;
|
isTimeout = pthread_cond_timedwait(&PRIVATE->nativeHandle, (pthread_mutex_t *)lk.handle(), &expire_ts) != 0;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user