Fix timedwait operations

This commit is contained in:
2025-06-21 21:39:37 +03:00
parent 56a7cba942
commit 6e643403b4
2 changed files with 25 additions and 8 deletions

View File

@@ -43,8 +43,14 @@ int BH_ConditionWaitFor(BH_Condition *condition,
{ {
struct timespec ts; struct timespec ts;
ts.tv_sec = timeout / 1000; /* Calculate absoulute time for timed wait */
ts.tv_nsec = (timeout - ts.tv_sec * 1000) * 1000000; clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += timeout / 1000;
ts.tv_nsec += (timeout % 1000) * 1000000;
while (ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
ts.tv_sec += 1;
}
switch (pthread_cond_timedwait(&condition->handle, &mutex->handle, &ts)) switch (pthread_cond_timedwait(&condition->handle, &mutex->handle, &ts))
{ {

View File

@@ -61,8 +61,14 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
{ {
struct timespec ts; struct timespec ts;
ts.tv_sec = timeout / 1000; /* Calculate absoulute time for timed wait */
ts.tv_nsec = (timeout - ts.tv_sec * 1000) * 1000000; clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += timeout / 1000;
ts.tv_nsec += (timeout % 1000) * 1000000;
while (ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
ts.tv_sec += 1;
}
switch (sem_timedwait(&semaphore->handle, &ts)) switch (sem_timedwait(&semaphore->handle, &ts))
{ {
@@ -165,9 +171,14 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
int result; int result;
struct timespec ts; struct timespec ts;
/* Setup timeout */ /* Calculate absoulute time for timed wait */
ts.tv_sec = timeout / 1000; clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_nsec = (timeout - ts.tv_sec * 1000) * 1000000; ts.tv_sec += timeout / 1000;
ts.tv_nsec += (timeout % 1000) * 1000000;
while (ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
ts.tv_sec += 1;
}
result = BH_OK; result = BH_OK;
/* Wait until semaphore count is not zero or timeout */ /* Wait until semaphore count is not zero or timeout */