diff options
| author | Mikhail Romanko <me@blankhex.com> | 2025-06-24 16:38:43 +0300 |
|---|---|---|
| committer | Mikhail Romanko <me@blankhex.com> | 2025-06-24 16:48:21 +0300 |
| commit | 703fb0f2407622a8d82abd9ae18fbde81688773f (patch) | |
| tree | e1548d0286267c02ad41569c46102fc63de2366d | |
| parent | 9212dbc7c2fbc00bb6f31f7d0ad6e9e47cbf9e19 (diff) | |
| download | bhlib-703fb0f2407622a8d82abd9ae18fbde81688773f.tar.gz | |
Rename MT functions, add tests, fix bugs
Renamed multithreaded related functions, added simple unit tests and fix
bugs caught by said tests :)
| -rw-r--r-- | doc/Manual/en/BH_Thread.pod | 12 | ||||
| -rw-r--r-- | doc/Manual/ru/BH_Thread.pod | 12 | ||||
| -rw-r--r-- | include/BH/Thread.h | 6 | ||||
| -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 | ||||
| -rw-r--r-- | test/src/TestThread.c | 110 |
15 files changed, 175 insertions, 46 deletions
diff --git a/doc/Manual/en/BH_Thread.pod b/doc/Manual/en/BH_Thread.pod index 1be67b6..2e28fda 100644 --- a/doc/Manual/en/BH_Thread.pod +++ b/doc/Manual/en/BH_Thread.pod @@ -98,9 +98,9 @@ If the mutex is locked by another thread, the behavior is undefined. Returns 0 on success, otherwise an error code. -=head2 BH_MutexLockTry +=head2 BH_MutexTryLock - int BH_MutexLockTry(BH_Mutex *mutex); + int BH_MutexTryLock(BH_Mutex *mutex); Attempts to lock the mutex. @@ -144,9 +144,9 @@ the semaphore value becomes greater than 0. Returns 0 on success, otherwise an error code. -=head2 BH_SemaphoreWaitTry +=head2 BH_SemaphoreTryWait - int BH_SemaphoreWaitTry(BH_Semaphore *semaphore); + int BH_SemaphoreTryWait(BH_Semaphore *semaphore); Attempts to decrease the semaphore value by 1. @@ -244,9 +244,9 @@ Returns 0 on success, otherwise an error code. Locks the spinlock. -=head2 BH_SpinlockLockTry +=head2 BH_SpinlockTryLock - int BH_SpinlockLockTry(int *lock); + int BH_SpinlockTryLock(int *lock); Attempts to lock the spinlock. diff --git a/doc/Manual/ru/BH_Thread.pod b/doc/Manual/ru/BH_Thread.pod index 083c421..5016ea8 100644 --- a/doc/Manual/ru/BH_Thread.pod +++ b/doc/Manual/ru/BH_Thread.pod @@ -97,9 +97,9 @@ I<callback> и данными I<data>. В случае успеха возвращает 0, иначе код ошибки. -=head2 BH_MutexLockTry +=head2 BH_MutexTryLock - int BH_MutexLockTry(BH_Mutex *mutex); + int BH_MutexTryLock(BH_Mutex *mutex); Производит попытку захвата мьютекса. @@ -143,9 +143,9 @@ I<callback> и данными I<data>. В случае успеха возвращает 0, иначе код ошибки. -=head2 BH_SemaphoreWaitTry +=head2 BH_SemaphoreTryWait - int BH_SemaphoreWaitTry(BH_Semaphore *semaphore); + int BH_SemaphoreTryWait(BH_Semaphore *semaphore); Пытается уменьшить значение семафора на 1. @@ -244,9 +244,9 @@ I<timeout>. Блокирует спинлок. -=head2 BH_SpinlockLockTry +=head2 BH_SpinlockTryLock - int BH_SpinlockLockTry(int *lock); + int BH_SpinlockTryLock(int *lock); Пытается заблокировать спинлок. diff --git a/include/BH/Thread.h b/include/BH/Thread.h index b4e931e..a28f997 100644 --- a/include/BH/Thread.h +++ b/include/BH/Thread.h @@ -39,7 +39,7 @@ int BH_MutexLock(BH_Mutex *mutex); int BH_MutexUnlock(BH_Mutex *mutex); -int BH_MutexLockTry(BH_Mutex *mutex); +int BH_MutexTryLock(BH_Mutex *mutex); BH_Semaphore *BH_SemaphoreNew(int value); @@ -54,7 +54,7 @@ int BH_SemaphorePost(BH_Semaphore *semaphore); int BH_SemaphoreWait(BH_Semaphore *semaphore); -int BH_SemaphoreWaitTry(BH_Semaphore *semaphore); +int BH_SemaphoreTryWait(BH_Semaphore *semaphore); int BH_SemaphoreWaitFor(BH_Semaphore *semaphore, @@ -85,7 +85,7 @@ int BH_ConditionBroadcast(BH_Condition *condition); void BH_SpinlockLock(int *lock); -int BH_SpinlockLockTry(int *lock); +int BH_SpinlockTryLock(int *lock); void BH_SpinlockUnlock(int *lock); 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); } diff --git a/test/src/TestThread.c b/test/src/TestThread.c new file mode 100644 index 0000000..1b82ca1 --- /dev/null +++ b/test/src/TestThread.c @@ -0,0 +1,110 @@ +#include <BH/Thread.h> +#include <BH/Unit.h> +#include <time.h> + + +BH_UNIT_TEST(Mutex) +{ + BH_Mutex *mutex; + + BH_VERIFY((mutex = BH_MutexNew()) != NULL); + BH_VERIFY(BH_MutexLock(mutex) == BH_OK); + BH_VERIFY(BH_MutexTryLock(mutex) != BH_OK); + BH_VERIFY(BH_MutexUnlock(mutex) == BH_OK); + BH_VERIFY(BH_MutexTryLock(mutex) == BH_OK); + BH_VERIFY(BH_MutexUnlock(mutex) == BH_OK); + BH_MutexFree(mutex); + + return 0; +} + + +BH_UNIT_TEST(Semaphore) +{ + BH_Semaphore *semaphore; + time_t start; + + start = time(NULL); + BH_VERIFY((semaphore = BH_SemaphoreNew(1)) != NULL); + BH_VERIFY(BH_SemaphoreWait(semaphore) == BH_OK); + BH_VERIFY(BH_SemaphoreTryWait(semaphore) != BH_OK); + BH_VERIFY(BH_SemaphoreWaitFor(semaphore, 5000) != BH_OK); + BH_VERIFY(BH_SemaphorePost(semaphore) == BH_OK); + BH_VERIFY(BH_SemaphoreTryWait(semaphore) == BH_OK); + BH_VERIFY(BH_SemaphorePost(semaphore) == BH_OK); + BH_VERIFY(BH_SemaphoreWaitFor(semaphore, 5000) == BH_OK); + BH_VERIFY(BH_SemaphorePost(semaphore) == BH_OK); + BH_VERIFY(time(NULL) - start >= 5); + BH_SemaphoreFree(semaphore); + + return 0; +} + + +BH_UNIT_TEST(Condition) +{ + BH_Condition *condition; + BH_Mutex *mutex; + time_t start; + + start = time(NULL); + BH_VERIFY((condition = BH_ConditionNew()) != NULL); + BH_VERIFY((mutex = BH_MutexNew()) != NULL); + BH_VERIFY(BH_ConditionWaitFor(condition, mutex, 5000) != BH_OK); + BH_VERIFY(BH_ConditionSignal(condition) == BH_OK); + BH_VERIFY(BH_ConditionBroadcast(condition) == BH_OK); + BH_VERIFY(time(NULL) - start >= 5); + BH_ConditionFree(condition); + BH_MutexFree(mutex); + + return 0; +} + + +static int ThreadCallback(void *data) +{ + *(int *)data = 12345; + + return 0; +} + + +BH_UNIT_TEST(Thread) +{ + BH_Thread *thread; + int data = 0; + + BH_VERIFY((thread = BH_ThreadNew(0, ThreadCallback, &data)) != NULL); + BH_VERIFY(BH_ThreadJoin(thread) == BH_OK); + BH_VERIFY(data == 12345); + + return 0; +} + + +BH_UNIT_TEST(Spinlock) +{ + int lock = 0; + + BH_SpinlockLock(&lock); + BH_VERIFY(BH_SpinlockTryLock(&lock) != BH_OK); + BH_SpinlockUnlock(&lock); + BH_VERIFY(BH_SpinlockTryLock(&lock) == BH_OK); + + return 0; +} + + +int main(int argc, char **argv) +{ + (void)argc; + (void)argv; + + BH_UNIT_ADD(Mutex); + BH_UNIT_ADD(Semaphore); + BH_UNIT_ADD(Condition); + BH_UNIT_ADD(Thread); + BH_UNIT_ADD(Spinlock); + + return BH_UnitRun(); +} |
