ciabatta/os/linux/tinyrt-threads.c

119 lines
4.4 KiB
C
Raw Normal View History

2023-09-06 09:37:30 +00:00
extern i64 _rt_thread_start(
u64 flags,
void *stack_base,
int *parent_tid,
int *child_tid,
void *tls,
int (*thread_fn)(void *ctx),
void *ctx
);
static _RT_Status _rt_thread_current(_RT_Thread *thread) {
thread->handle = (void *)((u64)__builtin_frame_address(0) & ~(cia_stack_size - 1));
return _RT_STATUS_OK;
}
static _RT_Status _rt_thread_create(_RT_Thread *thread, int (*thread_fn)(void *ctx), void *ctx) {
// Create the memory for stack
u64 mmap_prot = PROT_READ|PROT_WRITE;
u64 mmap_flags = MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE;
void *stack_base = sys_mmap(0, 2*cia_stack_size, mmap_prot, mmap_flags, -1, 0);
if((i64)stack_base < 0) {
return _RT_ERROR_GENERIC;
}
void *stack = (u8*)stack_base + 2*cia_stack_size;
// Find the TLS base and initialize the tls
_LD_Thread_Block *tcb = (void *)((u64)((u8 *)stack - 1) & ~(cia_stack_size - 1));
tcb->stack_canary = 0x12345678deadbeef;
u8 *tls_base = (u8 *)tcb - cia_tls_image_size;
for(int i = 0; i < cia_tls_image_size; ++i) {
tls_base[i] = ((u8 *)cia_tls_image_base)[i];
}
// Initialize the _RT_Thread handle, which would point to
// the TCB
thread->handle = tcb;
2023-09-10 06:53:22 +00:00
atomic_store_explicit(&tcb->thread_finished, 0, memory_order_relaxed);
// Create the new thread
u64 flags = 0;
// flags |= CLONE_CHILD_CLEARTID;
// flags |= CLONE_PARENT_SETTID;
flags |= CLONE_FS;
flags |= CLONE_FILES;
flags |= CLONE_SIGHAND;
flags |= CLONE_THREAD;
flags |= CLONE_VM;
flags |= CLONE_SYSVSEM;
int *child_tid = (int *)&tcb->thread_id;
int *parent_tid = (int *)&tcb->parent_id;
*child_tid = 1;
*parent_tid = 0;
2023-09-06 09:37:30 +00:00
i64 ret = _rt_thread_start(flags, stack, parent_tid, child_tid, 0, thread_fn, ctx);
if(ret < 0) {
return _RT_ERROR_GENERIC;
}
return _RT_STATUS_OK;
}
2023-09-06 09:37:30 +00:00
void _rt_thread_finish(int exit_code) {
_LD_Thread_Block *tcb = (void *)((u64)__builtin_frame_address(0) & ~(cia_stack_size - 1));
2023-09-06 09:37:30 +00:00
// Wait until the main thread decides what to do with the child thread
2023-09-10 06:53:22 +00:00
u32 thread_behaviour = atomic_load_explicit(&tcb->thread_behaviour, memory_order_relaxed);
while(thread_behaviour == _LD_THREAD_BEHAVIOUR_NOT_SET) {
syscall(SYS_futex, &tcb->thread_behaviour, FUTEX_WAIT, _LD_THREAD_BEHAVIOUR_NOT_SET, NULL, 0, 0);
2023-09-10 06:53:22 +00:00
thread_behaviour = atomic_load_explicit(&tcb->thread_behaviour, memory_order_relaxed);
2023-09-06 09:37:30 +00:00
}
2023-09-10 06:53:22 +00:00
// If main thread set this thread to be joined, we signal it that we're done here
if(thread_behaviour == _LD_THREAD_BEHAVIOUR_JOIN) {
2023-09-10 07:12:17 +00:00
atomic_store_explicit(&tcb->exit_code, exit_code, memory_order_relaxed);
2023-09-10 06:53:22 +00:00
atomic_store_explicit(&tcb->thread_finished, 1, memory_order_release);
2023-09-06 09:37:30 +00:00
syscall(SYS_futex, &tcb->thread_finished, FUTEX_WAKE, 0, NULL, 0, 0);
sys_exit(exit_code);
}
2023-09-10 06:53:22 +00:00
else if(thread_behaviour == _LD_THREAD_BEHAVIOUR_DETACH) {
2023-09-06 09:37:30 +00:00
// TODO: clean up the thread resources
sys_exit(exit_code);
}
}
static _RT_Status _rt_thread_join(_RT_Thread *thread, int *out_exit_code) {
_LD_Thread_Block *tcb = thread->handle;
2023-09-06 09:37:30 +00:00
// Signal the thread that we want it to be joined
2023-09-10 06:53:22 +00:00
atomic_store_explicit(&tcb->thread_behaviour, _LD_THREAD_BEHAVIOUR_JOIN, memory_order_relaxed);
syscall(SYS_futex, &tcb->thread_behaviour, FUTEX_WAKE, 0, NULL, 0, 0);
2023-09-06 09:37:30 +00:00
// Wait until the thread signals that it has completed the execution
2023-09-10 06:53:22 +00:00
while(atomic_load_explicit(&tcb->thread_finished, memory_order_acquire) != 1) {
2023-09-06 09:37:30 +00:00
syscall(SYS_futex, &tcb->thread_finished, FUTEX_WAIT, 0, NULL, 0, 0);
}
// Set the exit code
2023-09-10 07:12:17 +00:00
*out_exit_code = atomic_load_explicit(&tcb->exit_code, memory_order_acquire);
2023-09-06 09:37:30 +00:00
return _RT_STATUS_OK;
}
static _RT_Status _rt_thread_detach(_RT_Thread *thread) {
_LD_Thread_Block *tcb = thread->handle;
2023-09-10 06:53:22 +00:00
atomic_store_explicit(&tcb->thread_behaviour, _LD_THREAD_BEHAVIOUR_DETACH, memory_order_relaxed);
2023-09-06 09:37:30 +00:00
return _RT_STATUS_OK;
}
2023-09-11 11:44:06 +00:00
static _RT_Status _rt_thread_yield() {
i64 status = syscall(SYS_sched_yield);
if(status != 0) {
// shouldn't happen on linux
return _RT_ERROR_GENERIC;
}
return _RT_STATUS_OK;
}
static _RT_Status _rt_thread_terminate(_RT_Thread *thread) {
return _RT_ERROR_NOT_IMPLEMENTED;
}
static _RT_Status _rt_thread_sleep(u64 time) {
return _RT_ERROR_NOT_IMPLEMENTED;
}
static _RT_Status _rt_thread_get_timer_freq(u64 *freq) {
return _RT_ERROR_NOT_IMPLEMENTED;
}