From 8dd0ffdb3b75279f7ce60a40dd770004bb5e8f04 Mon Sep 17 00:00:00 2001 From: Vftdan Date: Fri, 25 Oct 2024 01:30:48 +0200 Subject: [PATCH] Add thread_exit and thread_yield --- src/common/util/thread.h | 14 +++++++++++++- src/common/util/thread.posix.c | 13 +++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/common/util/thread.h b/src/common/util/thread.h index cbbe00e..7cc1977 100644 --- a/src/common/util/thread.h +++ b/src/common/util/thread.h @@ -37,8 +37,20 @@ Thread thread_delete_handle(Thread th); * Waits for a thread to exit * @param th the thread to join * @param result_ptr pointer to store the exit result or NULL - * @return bool on success, false on failure (errno may be set) + * @return true on success, false on failure (errno may be set) */ bool thread_join(Thread th, ThreadResult *result_ptr); +/** + * Terminates current thread + * @param result exit result to return via thread_join + */ +void thread_exit(ThreadResult result) __attribute__((noreturn)); + +/** + * Hints the OS to release CPU used by this thread + * @return true on success, false on failure (errno may be set) + */ +bool thread_yield(); + #endif /* end of include guard: COMMON_UTIL_THREAD_H_ */ diff --git a/src/common/util/thread.posix.c b/src/common/util/thread.posix.c index bdaa1b1..249da04 100644 --- a/src/common/util/thread.posix.c +++ b/src/common/util/thread.posix.c @@ -1,6 +1,7 @@ #include "thread.h" #include +#include #include #include @@ -114,3 +115,15 @@ thread_join(Thread th, ThreadResult *result_ptr) } return true; } + +void +thread_exit(ThreadResult result) +{ + pthread_exit(result.pointer); +} + +bool +thread_yield() +{ + return !sched_yield(); +}