mirror of https://github.com/flysand7/ciabatta.git
threads.h organization
This commit is contained in:
parent
3fe99b72cb
commit
7d43887438
4
bake.cmd
4
bake.cmd
|
@ -8,6 +8,6 @@ windres -o utf8.obj utf8.rc
|
||||||
nasm src\os_win\chkstk.asm -ochkstk.o -fwin64
|
nasm src\os_win\chkstk.asm -ochkstk.o -fwin64
|
||||||
|
|
||||||
:: Compile the rest of the party
|
:: 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
|
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 inc -I src\_win -nostdlib
|
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
|
lib /nologo /out:ciabatta.lib chkstk.o utf8.obj ciabatta.obj
|
||||||
|
|
|
@ -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;
|
|
@ -20,28 +20,8 @@
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// TODO(bumbread): move this out to platform-specific folder
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
typedef struct cnd_t {
|
#include "os_win/threads_types.h"
|
||||||
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;
|
|
||||||
#else
|
#else
|
||||||
#error "Not implemented"
|
#error "Not implemented"
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -9,13 +9,13 @@
|
||||||
|
|
||||||
DWORD _tls_index = 0;
|
DWORD _tls_index = 0;
|
||||||
|
|
||||||
typedef struct thrd__wrapper_info {
|
typedef struct UserClosure {
|
||||||
thrd_start_t func;
|
thrd_start_t func;
|
||||||
void* arg;
|
void* arg;
|
||||||
} thrd__wrapper_info;
|
} UserClosure;
|
||||||
|
|
||||||
static DWORD thrd__wrapper(void* arg) {
|
static DWORD _thread_call_user(void* arg) {
|
||||||
thrd__wrapper_info info = *((thrd__wrapper_info*) arg);
|
UserClosure info = *((UserClosure*) arg);
|
||||||
int result = info.func(info.arg);
|
int result = info.func(info.arg);
|
||||||
|
|
||||||
// TODO(NeGate): setup TSS dtors here
|
// 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) {
|
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) {
|
if (info == NULL) {
|
||||||
return thrd_nomem;
|
return thrd_nomem;
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,10 @@ int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) {
|
||||||
info->func = func;
|
info->func = func;
|
||||||
info->arg = arg;
|
info->arg = arg;
|
||||||
|
|
||||||
// technically thrd_start_t and LPTHREAD_START_ROUTINE aren't the same but are close
|
// technically thrd_start_t and LPTHREAD_START_ROUTINE aren't the same
|
||||||
// enough to be ABI compatible, namely a difference in signedness of the return val.
|
// but are close enough to be ABI compatible, namely a difference in
|
||||||
thr->handle = CreateThread(NULL, 0, thrd__wrapper, info, 0, NULL);
|
// signedness of the return val.
|
||||||
|
thr->handle = CreateThread(NULL, 0, _thread_call_user, info, 0, NULL);
|
||||||
return thr->handle != NULL ? thrd_success : thrd_error;
|
return thr->handle != NULL ? thrd_success : thrd_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,21 +7,24 @@ int cnt;
|
||||||
|
|
||||||
int f(void* thr_data)
|
int f(void* thr_data)
|
||||||
{
|
{
|
||||||
for(int n = 0; n < 100000; ++n) {
|
for(int n = 0; n < 5; ++n)
|
||||||
atomic_fetch_add_explicit(&acnt, 1, memory_order_relaxed); // atomic
|
puts("b");
|
||||||
++cnt; // undefined behavior, in practice some updates missed
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
thrd_t thr[10];
|
thrd_t thread;
|
||||||
for(int n = 0; n < 10; ++n)
|
int status = thrd_create(&thread, f, NULL);
|
||||||
thrd_create(&thr[n], f, NULL);
|
if(status == thrd_error) {
|
||||||
for(int n = 0; n < 10; ++n)
|
puts("Failed creating threads");
|
||||||
thrd_join(thr[n], NULL);
|
}
|
||||||
|
for(int n = 0; n < 5; ++n) {
|
||||||
printf("The atomic counter is %d\n", acnt);
|
puts("a");
|
||||||
printf("The non-atomic counter is %d\n", cnt);
|
}
|
||||||
|
int res;
|
||||||
|
if(thrd_join(thread, &res) == thrd_error) {
|
||||||
|
puts("Failed waiting on thread");
|
||||||
|
}
|
||||||
|
puts("Finished");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue