threads.h organization

This commit is contained in:
bumbread 2023-02-15 20:54:58 +11:00
parent 3fe99b72cb
commit 7d43887438
5 changed files with 53 additions and 43 deletions

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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;
}

View File

@ -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");
}