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:
@@ -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.
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Пытается заблокировать спинлок.
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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))
|
||||
{
|
||||
|
||||
@@ -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))
|
||||
{
|
||||
|
||||
@@ -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 */
|
||||
|
||||
23
src/Platform/Posix/Timespec.h
Normal file
23
src/Platform/Posix/Timespec.h
Normal 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 */
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
110
test/src/TestThread.c
Normal file
110
test/src/TestThread.c
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user