CringeDB/System Abstraction/linux/cdb_thread.c

38 lines
894 B
C
Raw Permalink Normal View History

2024-02-18 17:42:05 +01:00
#include "../cdb_system.h"
#include <pthread.h>
#include <unistd.h>
2024-02-18 17:42:05 +01:00
struct sys_thread_args {
sys_threadProc proc;
void *arg;
};
2024-02-18 17:42:05 +01:00
typedef struct sys_thread_args sys_thread_args;
// Linux only uses System V AMD64 ABI calling convention (source: https://en.wikipedia.org/wiki/X86_calling_conventions)
void *uwu_runThread(void *arg) {
2024-02-18 17:42:05 +01:00
sys_thread_args *args = (sys_thread_args *) arg;
sys_threadProc proc = args->proc;
void *arg2 = args->arg;
2024-02-18 17:42:05 +01:00
sys_heapFree(args);
proc(arg2);
return NULL;
}
2024-02-18 17:42:05 +01:00
sys_Bool sys_threadNew(sys_threadProc proc, void *arg) {
sys_thread_args *args = (sys_thread_args *) sys_heapAlloc(sizeof(sys_thread_args));
args->proc = proc;
args->arg = arg;
pthread_t thread;
if(pthread_create(&thread, NULL, uwu_runThread, args)) {
2024-02-18 17:42:05 +01:00
sys_heapFree(args);
return sys_False;
}else {
2024-02-18 17:42:05 +01:00
return sys_True;
}
}
2024-02-18 17:42:05 +01:00
void sys_threadSleep(unsigned long seconds) {
sleep(seconds);
}