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:
2025-03-02 23:18:23 +03:00
parent 2ca6a3e316
commit d403d41f2c
21 changed files with 1492 additions and 9 deletions

View File

@@ -0,0 +1,90 @@
#include "Thread.h"
#include <BH/Thread.h>
#include <process.h>
struct BH_ThreadContext
{
BH_ThreadCallback callback;
void *data;
};
static unsigned __stdcall BH_ThreadRun(void *context)
{
BH_ThreadCallback callback;
void *data;
callback = ((struct BH_ThreadContext *)context)->callback;
data = ((struct BH_ThreadContext *)context)->data;
free(context);
callback(data);
BH_TssCleanup();
_endthreadex(0);
}
static int BH_ThreadInit(BH_Thread *thread,
size_t stack,
BH_ThreadCallback callback,
void *data)
{
struct BH_ThreadContext *context;
context = malloc(sizeof(*context));
if (!context)
return BH_ERROR;
context->callback = callback;
context->data = data;
thread->handle = (HANDLE)_beginthreadex(NULL, stack, BH_ThreadRun, context, 0, NULL);
if (!thread->handle)
{
free(context);
return BH_ERROR;
}
return BH_OK;
}
BH_Thread *BH_ThreadNew(size_t stack,
BH_ThreadCallback callback,
void *data)
{
BH_Thread *thread;
thread = malloc(sizeof(BH_Thread));
if (thread && BH_ThreadInit(thread, stack, callback, data))
{
free(thread);
return NULL;
}
return thread;
}
int BH_ThreadJoin(BH_Thread *thread)
{
/* Join the thread */
WaitForSingleObject(thread->handle, INFINITE);
CloseHandle(thread->handle);
free(thread);
return BH_OK;
}
int BH_ThreadDetach(BH_Thread *thread)
{
/* Detach from thread */
CloseHandle(thread->handle);
free(thread);
return BH_OK;
}