Fix semaphore implementation for MacOS

This commit is contained in:
2025-06-27 12:39:55 +03:00
parent 703fb0f240
commit f82f87cc41
3 changed files with 47 additions and 17 deletions

View File

@@ -45,7 +45,8 @@ int BH_ConditionWaitFor(BH_Condition *condition,
struct timespec ts; struct timespec ts;
/* Calculate absoulute time for timed wait */ /* Calculate absoulute time for timed wait */
convertToTimespec(&ts, timeout); if (convertToTimespec(&ts, timeout))
return BH_ERROR;
switch (pthread_cond_timedwait(&condition->handle, &mutex->handle, &ts)) switch (pthread_cond_timedwait(&condition->handle, &mutex->handle, &ts))
{ {

View File

@@ -61,7 +61,8 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
uint32_t timeout) uint32_t timeout)
{ {
struct timespec ts; struct timespec ts;
convertToTimespec(&ts, timeout); if (convertToTimespec(&ts, timeout))
return BH_ERROR;
switch (sem_timedwait(&semaphore->handle, &ts)) switch (sem_timedwait(&semaphore->handle, &ts))
{ {
@@ -111,14 +112,19 @@ void BH_SemaphoreFree(BH_Semaphore *semaphore)
int BH_SemaphorePost(BH_Semaphore *semaphore) int BH_SemaphorePost(BH_Semaphore *semaphore)
{ {
/* Increase semaphore value */ int result;
pthread_mutex_lock(&semaphore->mutex);
semaphore->count++;
if (semaphore->waiters)
pthread_cond_signal(&semaphore->condition);
pthread_mutex_unlock(&semaphore->mutex);
return BH_OK; /* Increase semaphore value */
if (pthread_mutex_lock(&semaphore->mutex))
return BH_ERROR;
result = BH_OK;
semaphore->count++;
if (semaphore->waiters && pthread_cond_signal(&semaphore->condition))
result = BH_ERROR;
pthread_mutex_unlock(&semaphore->mutex);
return result;
} }
@@ -127,11 +133,19 @@ int BH_SemaphoreWait(BH_Semaphore *semaphore)
int result; int result;
/* Wait until semaphore count is not zero */ /* Wait until semaphore count is not zero */
result = BH_ERROR; if (pthread_mutex_lock(&semaphore->mutex))
pthread_mutex_lock(&semaphore->mutex); return BH_ERROR;
result = BH_OK;
semaphore->waiters++; semaphore->waiters++;
while (!semaphore->count) while (!semaphore->count) {
pthread_cond_wait(&semaphore->condition, &semaphore->mutex); result = pthread_cond_wait(&semaphore->condition, &semaphore->mutex);
if (result && result != ETIMEDOUT) {
result = BH_ERROR;
break;
}
result = BH_OK;
}
semaphore->waiters--; semaphore->waiters--;
semaphore->count--; semaphore->count--;
pthread_mutex_unlock(&semaphore->mutex); pthread_mutex_unlock(&semaphore->mutex);
@@ -165,7 +179,8 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
struct timespec ts; struct timespec ts;
/* Calculate absoulute time for timed wait */ /* Calculate absoulute time for timed wait */
convertToTimespec(&ts, timeout); if (convertToTimespec(&ts, timeout))
return BH_ERROR;
result = BH_OK; result = BH_OK;
/* Wait until semaphore count is not zero or timeout */ /* Wait until semaphore count is not zero or timeout */

View File

@@ -3,21 +3,35 @@
#include <BH/Thread.h> #include <BH/Thread.h>
#include <sys/time.h>
#include <time.h> #include <time.h>
static void convertToTimespec(struct timespec *ts, static int convertToTimespec(struct timespec *ts,
uint32_t timeout) uint32_t timeout)
{ {
#ifdef USE_CLOCK_GETTIME
if (clock_gettime(CLOCK_REALTIME, ts))
return BH_ERROR;
#else
struct timeval tv;
if (gettimeofday(&tv, NULL))
return BH_ERROR;
ts->tv_sec = tv.tv_sec;
ts->tv_nsec = tv.tv_usec * 1000;
#endif
/* Calculate absoulute time for timed wait */ /* Calculate absoulute time for timed wait */
clock_gettime(CLOCK_REALTIME, ts);
ts->tv_sec += timeout / 1000; ts->tv_sec += timeout / 1000;
ts->tv_nsec += (timeout % 1000) * 1000000; ts->tv_nsec += (timeout % 1000) * 1000000;
while (ts->tv_nsec >= 1000000000) { while (ts->tv_nsec >= 1000000000) {
ts->tv_nsec -= 1000000000; ts->tv_nsec -= 1000000000;
ts->tv_sec += 1; ts->tv_sec += 1;
} }
return BH_OK;
} }
#endif /* BH_PLATFORM_POSIX_TIMESPEC_H */ #endif /* BH_PLATFORM_POSIX_TIMESPEC_H */