diff options
| -rw-r--r-- | src/Platform/Posix/Condition.c | 3 | ||||
| -rw-r--r-- | src/Platform/Posix/Semaphore.c | 37 | ||||
| -rw-r--r-- | src/Platform/Posix/Timespec.h | 20 |
3 files changed, 45 insertions, 15 deletions
diff --git a/src/Platform/Posix/Condition.c b/src/Platform/Posix/Condition.c index ad950f1..a03842a 100644 --- a/src/Platform/Posix/Condition.c +++ b/src/Platform/Posix/Condition.c @@ -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)) { diff --git a/src/Platform/Posix/Semaphore.c b/src/Platform/Posix/Semaphore.c index 9482201..4c64786 100644 --- a/src/Platform/Posix/Semaphore.c +++ b/src/Platform/Posix/Semaphore.c @@ -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) { + int result; + /* Increase semaphore value */ - pthread_mutex_lock(&semaphore->mutex); + if (pthread_mutex_lock(&semaphore->mutex)) + return BH_ERROR; + + result = BH_OK; semaphore->count++; - if (semaphore->waiters) - pthread_cond_signal(&semaphore->condition); - pthread_mutex_unlock(&semaphore->mutex); + if (semaphore->waiters && pthread_cond_signal(&semaphore->condition)) + result = BH_ERROR; - return BH_OK; + 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 */ diff --git a/src/Platform/Posix/Timespec.h b/src/Platform/Posix/Timespec.h index 1582f86..4bd673f 100644 --- a/src/Platform/Posix/Timespec.h +++ b/src/Platform/Posix/Timespec.h @@ -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 */ |
