From 7d43887438d31a582b1e91b4743f5e5d8d7e2611 Mon Sep 17 00:00:00 2001 From: bumbread Date: Wed, 15 Feb 2023 20:54:58 +1100 Subject: [PATCH] threads.h organization --- bake.cmd | 4 ++-- inc/os_win/threads_types.h | 26 ++++++++++++++++++++++++++ inc/threads.h | 22 +--------------------- src/os_win/thread.c | 17 +++++++++-------- test/test_threads.c | 27 +++++++++++++++------------ 5 files changed, 53 insertions(+), 43 deletions(-) create mode 100644 inc/os_win/threads_types.h diff --git a/bake.cmd b/bake.cmd index 81eaafc..a8362c6 100644 --- a/bake.cmd +++ b/bake.cmd @@ -8,6 +8,6 @@ windres -o utf8.obj utf8.rc nasm src\os_win\chkstk.asm -ochkstk.o -fwin64 :: Compile the rest of the party -clang -Wall src\ciabatta.c -o ciabatta.obj -c -DCIABATTA_WIN -I inc -I src\_win -nodefaultlibs -g -mfma -rem cuik src\ciabatta.c -o ciabatta.obj -c -DCIABATTA_WIN -I inc -I src\_win -nostdlib +clang -Wall src\ciabatta.c -o ciabatta.obj -c -DCIABATTA_WIN -I src -I inc -nodefaultlibs -g -mfma +rem cuik src\ciabatta.c -o ciabatta.obj -c -DCIABATTA_WIN -I src -I inc -nostdlib lib /nologo /out:ciabatta.lib chkstk.o utf8.obj ciabatta.obj diff --git a/inc/os_win/threads_types.h b/inc/os_win/threads_types.h new file mode 100644 index 0000000..8ecc018 --- /dev/null +++ b/inc/os_win/threads_types.h @@ -0,0 +1,26 @@ + +// Note(bumbread): this file is included into threads.h when compiled under +// windows. + +#pragma once + +typedef struct cnd_t { + int idk_yet; +} cnd_t; + +typedef struct thrd_t { + void *handle; +} thrd_t; + +typedef struct tss_t { + int idk_yet; +} tss_t; + +typedef struct mtx_t { + int type; + // Done to handle recursive mutexes + unsigned long recursion; + unsigned long owner; + _Atomic(int) counter; + void* semaphore; +} mtx_t; diff --git a/inc/threads.h b/inc/threads.h index 4181199..243c25e 100644 --- a/inc/threads.h +++ b/inc/threads.h @@ -20,28 +20,8 @@ }; #endif -// TODO(bumbread): move this out to platform-specific folder #if defined(_WIN32) - typedef struct cnd_t { - int idk_yet; - } cnd_t; - - typedef struct thrd_t { - void* handle; - } thrd_t; - - typedef struct tss_t { - int idk_yet; - } tss_t; - - typedef struct mtx_t { - int type; - // Done to handle recursive mutexes - unsigned long recursion; - unsigned long owner; - _Atomic(int) counter; - void* semaphore; - } mtx_t; + #include "os_win/threads_types.h" #else #error "Not implemented" #endif diff --git a/src/os_win/thread.c b/src/os_win/thread.c index bb40f19..19021c9 100644 --- a/src/os_win/thread.c +++ b/src/os_win/thread.c @@ -9,13 +9,13 @@ DWORD _tls_index = 0; -typedef struct thrd__wrapper_info { +typedef struct UserClosure { thrd_start_t func; void* arg; -} thrd__wrapper_info; +} UserClosure; -static DWORD thrd__wrapper(void* arg) { - thrd__wrapper_info info = *((thrd__wrapper_info*) arg); +static DWORD _thread_call_user(void* arg) { + UserClosure info = *((UserClosure*) arg); int result = info.func(info.arg); // TODO(NeGate): setup TSS dtors here @@ -27,7 +27,7 @@ thrd_t thrd_current(void) { } int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) { - thrd__wrapper_info* info = malloc(sizeof(thrd__wrapper_info)); + UserClosure* info = malloc(sizeof(UserClosure)); if (info == NULL) { return thrd_nomem; } @@ -35,9 +35,10 @@ int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) { info->func = func; info->arg = arg; - // technically thrd_start_t and LPTHREAD_START_ROUTINE aren't the same but are close - // enough to be ABI compatible, namely a difference in signedness of the return val. - thr->handle = CreateThread(NULL, 0, thrd__wrapper, info, 0, NULL); + // technically thrd_start_t and LPTHREAD_START_ROUTINE aren't the same + // but are close enough to be ABI compatible, namely a difference in + // signedness of the return val. + thr->handle = CreateThread(NULL, 0, _thread_call_user, info, 0, NULL); return thr->handle != NULL ? thrd_success : thrd_error; } diff --git a/test/test_threads.c b/test/test_threads.c index 401e917..41df77d 100644 --- a/test/test_threads.c +++ b/test/test_threads.c @@ -7,21 +7,24 @@ int cnt; int f(void* thr_data) { - for(int n = 0; n < 100000; ++n) { - atomic_fetch_add_explicit(&acnt, 1, memory_order_relaxed); // atomic - ++cnt; // undefined behavior, in practice some updates missed - } + for(int n = 0; n < 5; ++n) + puts("b"); return 0; } int main(void) { - thrd_t thr[10]; - for(int n = 0; n < 10; ++n) - thrd_create(&thr[n], f, NULL); - for(int n = 0; n < 10; ++n) - thrd_join(thr[n], NULL); - - printf("The atomic counter is %d\n", acnt); - printf("The non-atomic counter is %d\n", cnt); + thrd_t thread; + int status = thrd_create(&thread, f, NULL); + if(status == thrd_error) { + puts("Failed creating threads"); + } + for(int n = 0; n < 5; ++n) { + puts("a"); + } + int res; + if(thrd_join(thread, &res) == thrd_error) { + puts("Failed waiting on thread"); + } + puts("Finished"); }