diff options
Diffstat (limited to 'src/Platform')
| -rw-r--r-- | src/Platform/Dummy/Mutex.c | 2 | ||||
| -rw-r--r-- | src/Platform/Dummy/Semaphore.c | 2 | ||||
| -rw-r--r-- | src/Platform/Posix/Condition.c | 9 | ||||
| -rw-r--r-- | src/Platform/Posix/Mutex.c | 2 | ||||
| -rw-r--r-- | src/Platform/Posix/Semaphore.c | 23 | ||||
| -rw-r--r-- | src/Platform/Posix/Timespec.h | 23 | ||||
| -rw-r--r-- | src/Platform/Posix/Tss.c | 7 | ||||
| -rw-r--r-- | src/Platform/Spinlock.c | 2 | ||||
| -rw-r--r-- | src/Platform/Win32/Mutex.c | 2 | ||||
| -rw-r--r-- | src/Platform/Win32/Semaphore.c | 2 | ||||
| -rw-r--r-- | src/Platform/Win32/Tss.c | 7 |
11 files changed, 50 insertions, 31 deletions
diff --git a/src/Platform/Dummy/Mutex.c b/src/Platform/Dummy/Mutex.c index 1d26d58..b1ecfa8 100644 --- a/src/Platform/Dummy/Mutex.c +++ b/src/Platform/Dummy/Mutex.c @@ -29,7 +29,7 @@ int BH_MutexUnlock(BH_Mutex *mutex) } -int BH_MutexLockTry(BH_Mutex *mutex) +int BH_MutexTryLock(BH_Mutex *mutex) { BH_UNUSED(mutex); diff --git a/src/Platform/Dummy/Semaphore.c b/src/Platform/Dummy/Semaphore.c index 0b656dd..76d7b9b 100644 --- a/src/Platform/Dummy/Semaphore.c +++ b/src/Platform/Dummy/Semaphore.c @@ -31,7 +31,7 @@ int BH_SemaphoreWait(BH_Semaphore *semaphore) } -int BH_SemaphoreWaitTry(BH_Semaphore *semaphore) +int BH_SemaphoreTryWait(BH_Semaphore *semaphore) { BH_UNUSED(semaphore); diff --git a/src/Platform/Posix/Condition.c b/src/Platform/Posix/Condition.c index 67715b3..ad950f1 100644 --- a/src/Platform/Posix/Condition.c +++ b/src/Platform/Posix/Condition.c @@ -1,4 +1,5 @@ #include "Thread.h" +#include "Timespec.h" #include <stdlib.h> #include <errno.h> @@ -44,13 +45,7 @@ int BH_ConditionWaitFor(BH_Condition *condition, struct timespec ts; /* 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; - } + convertToTimespec(&ts, timeout); switch (pthread_cond_timedwait(&condition->handle, &mutex->handle, &ts)) { diff --git a/src/Platform/Posix/Mutex.c b/src/Platform/Posix/Mutex.c index dd3aacb..72c28b2 100644 --- a/src/Platform/Posix/Mutex.c +++ b/src/Platform/Posix/Mutex.c @@ -45,7 +45,7 @@ int BH_MutexUnlock(BH_Mutex *mutex) } -int BH_MutexLockTry(BH_Mutex *mutex) +int BH_MutexTryLock(BH_Mutex *mutex) { switch (pthread_mutex_trylock(&mutex->handle)) { diff --git a/src/Platform/Posix/Semaphore.c b/src/Platform/Posix/Semaphore.c index aeaf3bd..9482201 100644 --- a/src/Platform/Posix/Semaphore.c +++ b/src/Platform/Posix/Semaphore.c @@ -1,4 +1,5 @@ #include "Thread.h" +#include "Timespec.h" #include <stdlib.h> #include <errno.h> @@ -47,7 +48,7 @@ int BH_SemaphoreWait(BH_Semaphore *semaphore) } -int BH_SemaphoreWaitTry(BH_Semaphore *semaphore) +int BH_SemaphoreTryWait(BH_Semaphore *semaphore) { if (sem_trywait(&semaphore->handle)) return BH_ERROR; @@ -60,15 +61,7 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore, uint32_t timeout) { struct timespec ts; - - /* 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; - } + convertToTimespec(&ts, timeout); switch (sem_timedwait(&semaphore->handle, &ts)) { @@ -147,7 +140,7 @@ int BH_SemaphoreWait(BH_Semaphore *semaphore) } -int BH_SemaphoreWaitTry(BH_Semaphore *semaphore) +int BH_SemaphoreTryWait(BH_Semaphore *semaphore) { int result; @@ -172,13 +165,7 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore, struct timespec ts; /* 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; - } + convertToTimespec(&ts, timeout); 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 new file mode 100644 index 0000000..1582f86 --- /dev/null +++ b/src/Platform/Posix/Timespec.h @@ -0,0 +1,23 @@ +#ifndef BH_PLATFORM_POSIX_TIMESPEC_H +#define BH_PLATFORM_POSIX_TIMESPEC_H + + +#include <BH/Thread.h> +#include <time.h> + + +static void convertToTimespec(struct timespec *ts, + uint32_t timeout) +{ + /* 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; + } +} + + +#endif /* BH_PLATFORM_POSIX_TIMESPEC_H */ diff --git a/src/Platform/Posix/Tss.c b/src/Platform/Posix/Tss.c index dd66429..8a7a2fb 100644 --- a/src/Platform/Posix/Tss.c +++ b/src/Platform/Posix/Tss.c @@ -17,12 +17,19 @@ static void BH_TssKeyCleanup(void *data) { int i; + /* Skip or remove TSS data */ + if (!data) + return; + else if (pthread_setspecific(tssKey, NULL)) + abort(); + /* Lock cleanup table and call cleanups */ BH_SpinlockLock(&tssCleanupLock); for (i = 0; i < tssCleanupSize; i++) tssCleanupData[i](((void **)data)[i]); BH_SpinlockUnlock(&tssCleanupLock); + /* Deallocate TSS data */ free(data); } diff --git a/src/Platform/Spinlock.c b/src/Platform/Spinlock.c index 3583500..aecb6ff 100644 --- a/src/Platform/Spinlock.c +++ b/src/Platform/Spinlock.c @@ -21,7 +21,7 @@ void BH_SpinlockLock(int *lock) } -int BH_SpinlockLockTry(int *lock) +int BH_SpinlockTryLock(int *lock) { #if defined(__clang__) || defined(__GNUC__) if (__sync_lock_test_and_set(lock, 1)) diff --git a/src/Platform/Win32/Mutex.c b/src/Platform/Win32/Mutex.c index b5af90e..fc0f78c 100644 --- a/src/Platform/Win32/Mutex.c +++ b/src/Platform/Win32/Mutex.c @@ -40,7 +40,7 @@ int BH_MutexUnlock(BH_Mutex *mutex) } -int BH_MutexLockTry(BH_Mutex *mutex) +int BH_MutexTryLock(BH_Mutex *mutex) { if (!TryEnterCriticalSection(&mutex->handle)) return BH_ERROR; diff --git a/src/Platform/Win32/Semaphore.c b/src/Platform/Win32/Semaphore.c index 1844ea6..fe0a67b 100644 --- a/src/Platform/Win32/Semaphore.c +++ b/src/Platform/Win32/Semaphore.c @@ -46,7 +46,7 @@ int BH_SemaphoreWait(BH_Semaphore *semaphore) } -int BH_SemaphoreWaitTry(BH_Semaphore *semaphore) +int BH_SemaphoreTryWait(BH_Semaphore *semaphore) { if (WaitForSingleObject(semaphore->handle, 0)) return BH_ERROR; diff --git a/src/Platform/Win32/Tss.c b/src/Platform/Win32/Tss.c index 7d8a1b3..584bc46 100644 --- a/src/Platform/Win32/Tss.c +++ b/src/Platform/Win32/Tss.c @@ -18,12 +18,19 @@ static void __stdcall BH_TssKeyCleanup(void *data) { int i; + /* Skip or remove TSS data */ + if (!data) + return; + else if (FlsSetValue(tssKey, NULL)) + abort(); + /* Lock cleanup table and call cleanups */ BH_SpinlockLock(&tssCleanupLock); for (i = 0; i < tssCleanupSize; i++) tssCleanupData[i](((void **)data)[i]); BH_SpinlockUnlock(&tssCleanupLock); + /* Deallocate TSS data */ free(data); } |
