Fix semaphore implementation for MacOS
This commit is contained in:
@@ -45,7 +45,8 @@ int BH_ConditionWaitFor(BH_Condition *condition,
|
||||
struct timespec ts;
|
||||
|
||||
/* 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))
|
||||
{
|
||||
|
||||
@@ -61,7 +61,8 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
|
||||
uint32_t timeout)
|
||||
{
|
||||
struct timespec ts;
|
||||
convertToTimespec(&ts, timeout);
|
||||
if (convertToTimespec(&ts, timeout))
|
||||
return BH_ERROR;
|
||||
|
||||
switch (sem_timedwait(&semaphore->handle, &ts))
|
||||
{
|
||||
@@ -111,14 +112,19 @@ void BH_SemaphoreFree(BH_Semaphore *semaphore)
|
||||
|
||||
int BH_SemaphorePost(BH_Semaphore *semaphore)
|
||||
{
|
||||
/* Increase semaphore value */
|
||||
pthread_mutex_lock(&semaphore->mutex);
|
||||
semaphore->count++;
|
||||
if (semaphore->waiters)
|
||||
pthread_cond_signal(&semaphore->condition);
|
||||
pthread_mutex_unlock(&semaphore->mutex);
|
||||
int result;
|
||||
|
||||
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;
|
||||
|
||||
/* Wait until semaphore count is not zero */
|
||||
result = BH_ERROR;
|
||||
pthread_mutex_lock(&semaphore->mutex);
|
||||
if (pthread_mutex_lock(&semaphore->mutex))
|
||||
return BH_ERROR;
|
||||
|
||||
result = BH_OK;
|
||||
semaphore->waiters++;
|
||||
while (!semaphore->count)
|
||||
pthread_cond_wait(&semaphore->condition, &semaphore->mutex);
|
||||
while (!semaphore->count) {
|
||||
result = pthread_cond_wait(&semaphore->condition, &semaphore->mutex);
|
||||
if (result && result != ETIMEDOUT) {
|
||||
result = BH_ERROR;
|
||||
break;
|
||||
}
|
||||
result = BH_OK;
|
||||
}
|
||||
semaphore->waiters--;
|
||||
semaphore->count--;
|
||||
pthread_mutex_unlock(&semaphore->mutex);
|
||||
@@ -165,7 +179,8 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
|
||||
struct timespec ts;
|
||||
|
||||
/* Calculate absoulute time for timed wait */
|
||||
convertToTimespec(&ts, timeout);
|
||||
if (convertToTimespec(&ts, timeout))
|
||||
return BH_ERROR;
|
||||
result = BH_OK;
|
||||
|
||||
/* Wait until semaphore count is not zero or timeout */
|
||||
|
||||
@@ -3,21 +3,35 @@
|
||||
|
||||
|
||||
#include <BH/Thread.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
|
||||
static void convertToTimespec(struct timespec *ts,
|
||||
static int convertToTimespec(struct timespec *ts,
|
||||
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 */
|
||||
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;
|
||||
}
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
|
||||
#endif /* BH_PLATFORM_POSIX_TIMESPEC_H */
|
||||
|
||||
Reference in New Issue
Block a user