Rename MT functions, add tests, fix bugs

Renamed multithreaded related functions, added simple unit tests and fix
bugs caught by said tests :)
This commit is contained in:
2025-06-24 16:38:43 +03:00
parent 9212dbc7c2
commit 703fb0f240
15 changed files with 175 additions and 46 deletions

View File

@@ -98,9 +98,9 @@ If the mutex is locked by another thread, the behavior is undefined.
Returns 0 on success, otherwise an error code. 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. Attempts to lock the mutex.
@@ -144,9 +144,9 @@ the semaphore value becomes greater than 0.
Returns 0 on success, otherwise an error code. 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. Attempts to decrease the semaphore value by 1.
@@ -244,9 +244,9 @@ Returns 0 on success, otherwise an error code.
Locks the spinlock. Locks the spinlock.
=head2 BH_SpinlockLockTry =head2 BH_SpinlockTryLock
int BH_SpinlockLockTry(int *lock); int BH_SpinlockTryLock(int *lock);
Attempts to lock the spinlock. Attempts to lock the spinlock.

View File

@@ -97,9 +97,9 @@ I<callback> и данными I<data>.
В случае успеха возвращает 0, иначе код ошибки. В случае успеха возвращает 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, иначе код ошибки. В случае успеха возвращает 0, иначе код ошибки.
=head2 BH_SemaphoreWaitTry =head2 BH_SemaphoreTryWait
int BH_SemaphoreWaitTry(BH_Semaphore *semaphore); int BH_SemaphoreTryWait(BH_Semaphore *semaphore);
Пытается уменьшить значение семафора на 1. Пытается уменьшить значение семафора на 1.
@@ -244,9 +244,9 @@ I<timeout>.
Блокирует спинлок. Блокирует спинлок.
=head2 BH_SpinlockLockTry =head2 BH_SpinlockTryLock
int BH_SpinlockLockTry(int *lock); int BH_SpinlockTryLock(int *lock);
Пытается заблокировать спинлок. Пытается заблокировать спинлок.

View File

@@ -39,7 +39,7 @@ int BH_MutexLock(BH_Mutex *mutex);
int BH_MutexUnlock(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); BH_Semaphore *BH_SemaphoreNew(int value);
@@ -54,7 +54,7 @@ int BH_SemaphorePost(BH_Semaphore *semaphore);
int BH_SemaphoreWait(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, int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
@@ -85,7 +85,7 @@ int BH_ConditionBroadcast(BH_Condition *condition);
void BH_SpinlockLock(int *lock); void BH_SpinlockLock(int *lock);
int BH_SpinlockLockTry(int *lock); int BH_SpinlockTryLock(int *lock);
void BH_SpinlockUnlock(int *lock); void BH_SpinlockUnlock(int *lock);

View File

@@ -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); BH_UNUSED(mutex);

View File

@@ -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); BH_UNUSED(semaphore);

View File

@@ -1,4 +1,5 @@
#include "Thread.h" #include "Thread.h"
#include "Timespec.h"
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
@@ -44,13 +45,7 @@ int BH_ConditionWaitFor(BH_Condition *condition,
struct timespec ts; struct timespec ts;
/* Calculate absoulute time for timed wait */ /* Calculate absoulute time for timed wait */
clock_gettime(CLOCK_REALTIME, &ts); convertToTimespec(&ts, timeout);
ts.tv_sec += timeout / 1000;
ts.tv_nsec += (timeout % 1000) * 1000000;
while (ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
ts.tv_sec += 1;
}
switch (pthread_cond_timedwait(&condition->handle, &mutex->handle, &ts)) switch (pthread_cond_timedwait(&condition->handle, &mutex->handle, &ts))
{ {

View File

@@ -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)) switch (pthread_mutex_trylock(&mutex->handle))
{ {

View File

@@ -1,4 +1,5 @@
#include "Thread.h" #include "Thread.h"
#include "Timespec.h"
#include <stdlib.h> #include <stdlib.h>
#include <errno.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)) if (sem_trywait(&semaphore->handle))
return BH_ERROR; return BH_ERROR;
@@ -60,15 +61,7 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
uint32_t timeout) uint32_t timeout)
{ {
struct timespec ts; struct timespec ts;
convertToTimespec(&ts, 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;
}
switch (sem_timedwait(&semaphore->handle, &ts)) 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; int result;
@@ -172,13 +165,7 @@ int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
struct timespec ts; struct timespec ts;
/* Calculate absoulute time for timed wait */ /* Calculate absoulute time for timed wait */
clock_gettime(CLOCK_REALTIME, &ts); convertToTimespec(&ts, timeout);
ts.tv_sec += timeout / 1000;
ts.tv_nsec += (timeout % 1000) * 1000000;
while (ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000;
ts.tv_sec += 1;
}
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

@@ -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 */

View File

@@ -17,12 +17,19 @@ static void BH_TssKeyCleanup(void *data)
{ {
int i; int i;
/* Skip or remove TSS data */
if (!data)
return;
else if (pthread_setspecific(tssKey, NULL))
abort();
/* Lock cleanup table and call cleanups */ /* Lock cleanup table and call cleanups */
BH_SpinlockLock(&tssCleanupLock); BH_SpinlockLock(&tssCleanupLock);
for (i = 0; i < tssCleanupSize; i++) for (i = 0; i < tssCleanupSize; i++)
tssCleanupData[i](((void **)data)[i]); tssCleanupData[i](((void **)data)[i]);
BH_SpinlockUnlock(&tssCleanupLock); BH_SpinlockUnlock(&tssCleanupLock);
/* Deallocate TSS data */
free(data); free(data);
} }

View File

@@ -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 defined(__clang__) || defined(__GNUC__)
if (__sync_lock_test_and_set(lock, 1)) if (__sync_lock_test_and_set(lock, 1))

View File

@@ -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)) if (!TryEnterCriticalSection(&mutex->handle))
return BH_ERROR; return BH_ERROR;

View File

@@ -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)) if (WaitForSingleObject(semaphore->handle, 0))
return BH_ERROR; return BH_ERROR;

View File

@@ -18,12 +18,19 @@ static void __stdcall BH_TssKeyCleanup(void *data)
{ {
int i; int i;
/* Skip or remove TSS data */
if (!data)
return;
else if (FlsSetValue(tssKey, NULL))
abort();
/* Lock cleanup table and call cleanups */ /* Lock cleanup table and call cleanups */
BH_SpinlockLock(&tssCleanupLock); BH_SpinlockLock(&tssCleanupLock);
for (i = 0; i < tssCleanupSize; i++) for (i = 0; i < tssCleanupSize; i++)
tssCleanupData[i](((void **)data)[i]); tssCleanupData[i](((void **)data)[i]);
BH_SpinlockUnlock(&tssCleanupLock); BH_SpinlockUnlock(&tssCleanupLock);
/* Deallocate TSS data */
free(data); free(data);
} }

110
test/src/TestThread.c Normal file
View File

@@ -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();
}