diff --git a/include/threads.h b/include/threads.h index a3ed831..82eab15 100644 --- a/include/threads.h +++ b/include/threads.h @@ -18,4 +18,5 @@ enum { int thrd_create(thrd_t *thr, thrd_start_t func, void *arg); int thrd_join(thrd_t thr, int *out_exit_code); -int thrd_detach(thrd_t thr); \ No newline at end of file +int thrd_detach(thrd_t thr); +void thrd_yield(); \ No newline at end of file diff --git a/include/tinyrt.h b/include/tinyrt.h index 15a291e..cd8fc02 100644 --- a/include/tinyrt.h +++ b/include/tinyrt.h @@ -58,6 +58,7 @@ static _RT_Status _rt_thread_create(_RT_Thread *thread, int (*thread_fn)(void *c static _RT_Status _rt_thread_join(_RT_Thread *thread, int *out_exit_code); static _RT_Status _rt_thread_detach(_RT_Thread *thread); static _RT_Status _rt_thread_terminate(_RT_Thread *thread); +static _RT_Status _rt_thread_yield(); static _RT_Status _rt_thread_sleep(u64 time); static _RT_Status _rt_thread_get_timer_freq(u64 *freq); diff --git a/os/linux/tinyrt-threads.c b/os/linux/tinyrt-threads.c index 21bc468..81dcf30 100644 --- a/os/linux/tinyrt-threads.c +++ b/os/linux/tinyrt-threads.c @@ -96,6 +96,15 @@ static _RT_Status _rt_thread_detach(_RT_Thread *thread) { return _RT_STATUS_OK; } +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; } diff --git a/src/cia-sync/mutex.c b/src/cia-sync/mutex.c index 2e4a646..c5b1318 100644 --- a/src/cia-sync/mutex.c +++ b/src/cia-sync/mutex.c @@ -18,6 +18,7 @@ void cia_mutex_lock(Cia_Mutex *mutex) { , memory_order_acquire , memory_order_relaxed ); + // We got the mutex, lets bail if(p_tag == _CIA_MUTEX_FREE) { break; diff --git a/src/stdlib-thread/thread.c b/src/stdlib-thread/thread.c index 58d5627..e15876e 100644 --- a/src/stdlib-thread/thread.c +++ b/src/stdlib-thread/thread.c @@ -22,3 +22,7 @@ int thrd_join(thrd_t thr, int *out_exit_code) { } return thrd_error; } + +void thrd_yield(void) { + _rt_thread_yield(); +} \ No newline at end of file