Files
CgeThread/win32/Thread.c
Mikhail Romanko 6c14666b26
All checks were successful
CI / build-and-analyze (push) Successful in 1m20s
Fix casting to void
2026-08-09 16:20:54 +03:00

78 lines
1.7 KiB
C

#include "Internal.h"
#include <process.h>
#include <stdlib.h>
struct CgeThreadContext {
CgeThreadCallback callback;
void *data;
};
static unsigned __stdcall threadRun(void *context) {
CgeThreadCallback callback;
void *data;
callback = ((struct CgeThreadContext *)context)->callback;
data = ((struct CgeThreadContext *)context)->data;
free(context);
callback(data);
_endthreadex(0);
}
static int threadInit(CgeThread *thread, size_t stack,
CgeThreadCallback callback, void *data, int *result) {
struct CgeThreadContext *context;
context = malloc(sizeof(*context));
if (!context) {
if (result)
*result = CGE_THREAD_EOOM;
return 0;
}
context->callback = callback;
context->data = data;
thread->handle = (HANDLE)_beginthreadex(NULL, stack, threadRun, context, 0, NULL);
if (!thread->handle) {
free(context);
if (result)
*result = CGE_THREAD_EUNKNOWN;
return 0;
}
return 1;
}
CgeThread *CgeThreadNew(size_t stack, CgeThreadCallback callback, void *data,
int *result) {
CgeThread *thread;
if (result)
*result = CGE_THREAD_EOOM;
thread = malloc(sizeof(*thread));
if (thread && threadInit(thread, stack, callback, data, result)) {
free(thread);
}
return thread;
}
int CgeThreadJoin(CgeThread *thread, int *result) {
(void)result;
WaitForSingleObject(thread->handle, INFINITE);
CloseHandle(thread->handle);
free(thread);
return 1;
}
int CgeThreadDetach(CgeThread *thread, int *result) {
(void)result;
CloseHandle(thread->handle);
free(thread);
return 1;
}
void CgeThreadSleep(uint32_t timeout) {
Sleep(timeout);
}