add thread

This commit is contained in:
NishiOwO 2025-04-07 18:25:52 +09:00
parent ca96d76ff0
commit c22fd58f2e
No known key found for this signature in database
GPG Key ID: 27EF69B208EB9343
5 changed files with 61 additions and 1 deletions

View File

@ -11,4 +11,6 @@
/* Standard */
nb_thread_t* nb_create_thread(void (*func)(void*), void* userdata);
#endif

View File

@ -6,15 +6,35 @@
#ifdef NB_EXPOSE_THREAD
/* External library */
#if defined(THREAD_POSIX)
#include <pthread.h>
#elif defined(THREAD_WIN32)
#include <windows.h>
#endif
/* NishBox */
/* Standard */
NB_DECLARE_TYPE(thread, {
NB_DECLARE_TYPE(thread_context, {
void (*func)(void*);
void* data;
});
#if defined(THREAD_POSIX)
NB_DECLARE_TYPE(thread, {
nb_thread_context_t context;
pthread_t thread;
});
#elif defined(THREAD_WIN32)
NB_DECLARE_TYPE(thread, {
nb_thread_context_t context;
HANDLE thread;
});
#endif
#else
typedef void nb_thread_t;
typedef void nb_thread_context_t;
#endif
#endif

View File

@ -3,6 +3,7 @@
#include <nb_pre.h>
/* External library */
#include <pthread.h>
/* Interface */
#include <nb_thread.h>
@ -10,3 +11,19 @@
/* NishBox */
/* Standard */
#include <stdlib.h>
void* nb_wrap_thread(void* arg) {
nb_thread_context_t* ctx = (nb_thread_context_t*)arg;
ctx->func(ctx->data);
return NULL;
}
nb_thread_t* nb_create_thread(void (*func)(void*), void* userdata) {
nb_thread_t* thread = malloc(sizeof(*thread));
thread->context.func = func;
thread->context.data = userdata;
if(pthread_create(&thread->thread, NULL, nb_wrap_thread, &thread->context) == 0) return thread;
free(thread);
return NULL;
}

View File

@ -3,6 +3,7 @@
#include <nb_pre.h>
/* External library */
#include <windows.h>
/* Interface */
#include <nb_thread.h>
@ -10,3 +11,21 @@
/* NishBox */
/* Standard */
#include <stdlib.h>
DWORD WINAPI nb_wrap_thread(void* arg) {
nb_thread_context_t* ctx = (nb_thread_context_t*)arg;
ctx->func(ctx->data);
return 0;
}
nb_thread_t* nb_create_thread(void (*func)(void*), void* userdata) {
nb_thread_t* thread = malloc(sizeof(*thread));
thread->context.func = func;
thread->context.data = userdata;
if((thread->thread = CreateThread(NULL, 0, nb_wrap_thread, &thread->context, 0, NULL)) != NULL) return thread;
/* XXX: Is this needed? */
ResumeThread(thread->thread);
free(thread);
return NULL;
}

2
tool/format.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/sh
exec clang-format --verbose -i `find engine src -name "*.c" -or -name "*.h"`