diff options
Diffstat (limited to 'src/Platform/Win32/Thread.c')
| -rw-r--r-- | src/Platform/Win32/Thread.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/Platform/Win32/Thread.c b/src/Platform/Win32/Thread.c new file mode 100644 index 0000000..63de498 --- /dev/null +++ b/src/Platform/Win32/Thread.c @@ -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; +} |
