aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikhail Romanko <me@blankhex.com>2025-06-21 21:39:37 +0300
committerMikhail Romanko <me@blankhex.com>2025-06-21 21:39:37 +0300
commit6e643403b4ba6232497119e1f0829a7027c36a39 (patch)
treee4ef19a487afaf15b81070f3b30468b3fb6f3974
parent56a7cba9425d53090a60cf2dd1fd96d2f50c4ac7 (diff)
downloadbhlib-6e643403b4ba6232497119e1f0829a7027c36a39.tar.gz
Fix timedwait operations
-rw-r--r--src/Platform/Posix/Condition.c10
-rw-r--r--src/Platform/Posix/Semaphore.c23
2 files changed, 25 insertions, 8 deletions
diff --git a/src/Platform/Posix/Condition.c b/src/Platform/Posix/Condition.c
index 3fb5041..67715b3 100644
--- a/src/Platform/Posix/Condition.c
+++ b/src/Platform/Posix/Condition.c
@@ -43,8 +43,14 @@ int BH_ConditionWaitFor(BH_Condition *condition,
{
struct timespec ts;
- ts.tv_sec = timeout / 1000;
- ts.tv_nsec = (timeout - ts.tv_sec * 1000) * 1000000;
+ /* Calculate absoulute time for timed wait */
+ 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))
{
diff --git a/src/Platform/Posix/Semaphore.c b/src/Platform/Posix/Semaphore.c
index a37684c..c7b9d25 100644
--- a/src/Platform/Posix/Semaphore.c
+++ b/src/Platform/Posix/Semaphore.c
@@ -60,9 +60,15 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
uint32_t timeout)
{
struct timespec ts;
-
- ts.tv_sec = timeout / 1000;
- ts.tv_nsec = (timeout - ts.tv_sec * 1000) * 1000000;
+
+ /* Calculate absoulute time for timed wait */
+ 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))
{
@@ -165,9 +171,14 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
int result;
struct timespec ts;
- /* Setup timeout */
- ts.tv_sec = timeout / 1000;
- ts.tv_nsec = (timeout - ts.tv_sec * 1000) * 1000000;
+ /* Calculate absoulute time for timed wait */
+ 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;
+ }
result = BH_OK;
/* Wait until semaphore count is not zero or timeout */