mirror of
https://github.com/nishiowo/nishbox
synced 2025-04-21 20:24:39 +00:00
add thread
This commit is contained in:
parent
ca96d76ff0
commit
c22fd58f2e
@ -11,4 +11,6 @@
|
||||
|
||||
/* Standard */
|
||||
|
||||
nb_thread_t* nb_create_thread(void (*func)(void*), void* userdata);
|
||||
|
||||
#endif
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
2
tool/format.sh
Executable file
@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
exec clang-format --verbose -i `find engine src -name "*.c" -or -name "*.h"`
|
Loading…
x
Reference in New Issue
Block a user