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

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

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);

View File

@@ -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))
{

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))
{

View File

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

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;
/* 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);
}

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 (__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))
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))
return BH_ERROR;

View File

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