Add initial implementation of threads/mutexes/semaphores/cvs/spinlocks
Added initial implementation (or wrapper) of the threading library. It's rather basic, but should work for most of the tasks. Unfortunately, spinlock implementation relies on GCC/Clang compiler built-ins (or in-worst-case-scenario on Win32 - InterlockExchange). In the future, I should revisit this code and fix/reimplement some stuff (or add support for Windows XP).
This commit is contained in:
69
src/Platform/Win32/Semaphore.c
Normal file
69
src/Platform/Win32/Semaphore.c
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "Thread.h"
|
||||
|
||||
#include <BH/Thread.h>
|
||||
|
||||
|
||||
BH_Semaphore *BH_SemaphoreNew(int value)
|
||||
{
|
||||
BH_Semaphore *semaphore;
|
||||
|
||||
/* Allocate space for mutex and initialize it */
|
||||
semaphore = malloc(sizeof(BH_Semaphore));
|
||||
if (semaphore)
|
||||
{
|
||||
semaphore->handle = CreateSemaphore(NULL, value, 0x7FFF, NULL);
|
||||
if (!semaphore->handle)
|
||||
{
|
||||
free(semaphore);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return semaphore;
|
||||
}
|
||||
|
||||
|
||||
void BH_SemaphoreFree(BH_Semaphore *semaphore)
|
||||
{
|
||||
CloseHandle(semaphore->handle);
|
||||
free(semaphore);
|
||||
}
|
||||
|
||||
|
||||
int BH_SemaphorePost(BH_Semaphore *semaphore)
|
||||
{
|
||||
if (!ReleaseSemaphore(semaphore->handle, 1, NULL))
|
||||
return BH_ERROR;
|
||||
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
|
||||
int BH_SemaphoreWait(BH_Semaphore *semaphore)
|
||||
{
|
||||
if (WaitForSingleObject(semaphore->handle, INFINITE))
|
||||
return BH_ERROR;
|
||||
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
|
||||
int BH_SemaphoreWaitTry(BH_Semaphore *semaphore)
|
||||
{
|
||||
if (WaitForSingleObject(semaphore->handle, 0))
|
||||
return BH_ERROR;
|
||||
|
||||
return BH_OK;
|
||||
}
|
||||
|
||||
|
||||
int BH_SemaphoreWaitFor(BH_Semaphore *semaphore,
|
||||
uint32_t timeout)
|
||||
{
|
||||
switch (WaitForSingleObject(semaphore->handle, timeout))
|
||||
{
|
||||
case 0: return BH_OK;
|
||||
case WAIT_TIMEOUT: return BH_TIMEOUT;
|
||||
default: return BH_ERROR;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user