diff --git a/include/re_thread.h b/include/re_thread.h index d67745e4b..f4778de81 100644 --- a/include/re_thread.h +++ b/include/re_thread.h @@ -25,11 +25,47 @@ typedef int (*thread_h)(void *); #define THREAD_ONCE_FLAG_INIT 0 typedef int thread_once_flag; +/** + * Creates a new thread + * + * @param thr Pointer to new thread + * @param func Function to execute + * @param arg Argument to pass to the function + * + * @return 0 if success, otherwise errorcode + */ int thread_create(thread *thr, thread_h func, void *arg); + +/** + * Checks whether lhs and rhs refer to the same thread. + * + * @return Non-zero value if lhs and rhs refer to the same value, 0 otherwise. + */ int thread_equal(thread lhs, thread rhs); + +/** + * @return the identifier of the calling thread. + */ thread thread_current(void); + +/** + * Detaches the thread identified by thr from the current environment. + * + * @return 0 if success, otherwise errorcode + */ int thread_detach(thread thr); + +/** + * Blocks the current thread until the thread identified by thr finishes + * execution + * + * @param thr Thread + * @param res Result code location + * + * @return 0 if success, otherwise errorcode + */ int thread_join(thread thr, int *res); + int thread_once(thread_once_flag *flag, void (*func)(void)); void thread_exit(int res); diff --git a/src/thread/posix.c b/src/thread/posix.c index 4d1002123..64b208aa9 100644 --- a/src/thread/posix.c +++ b/src/thread/posix.c @@ -28,15 +28,6 @@ static void *thread_handler(void *p) } -/** - * Create thread - * - * @param thr Thread - * @param func Function - * @param arg ARG - * - * @return 0 if success, otherwise errorcode - */ int thread_create(thread *thr, thread_h func, void *arg) { struct th *th; @@ -50,7 +41,7 @@ int thread_create(thread *thr, thread_h func, void *arg) return ENOMEM; th->func = func; - th->arg = arg; + th->arg = arg; err = pthread_create(thr, NULL, thread_handler, th); if (err) @@ -58,3 +49,35 @@ int thread_create(thread *thr, thread_h func, void *arg) return err; } + + +int thread_equal(thread lhs, thread rhs) +{ + return pthread_equal(lhs, rhs); +} + + +thread thread_current(void) +{ + return pthread_self(); +} + + +int thread_detach(thread thr) +{ + return pthread_detach(thr); +} + + +int thread_join(thread thr, int *res) +{ + void *code; + int err; + + err = pthread_join(thr, &code); + + if (res) + *res = (int)(intptr_t)code; + + return err; +}