From dfc2e2bd6855f3755834c90d66dc8791bb3640c7 Mon Sep 17 00:00:00 2001 From: Sebastian Sumpf Date: Tue, 8 Aug 2017 14:10:18 +0200 Subject: [PATCH] pthread: 'phtread_join' issue #2791 --- repos/libports/lib/mk/pthread.mk | 2 ++ repos/libports/src/lib/pthread/thread.cc | 20 ++++++++++++++++++++ repos/libports/src/lib/pthread/thread.h | 10 ++++++++++ 3 files changed, 32 insertions(+) diff --git a/repos/libports/lib/mk/pthread.mk b/repos/libports/lib/mk/pthread.mk index 96b2648b5d..528fea209d 100644 --- a/repos/libports/lib/mk/pthread.mk +++ b/repos/libports/lib/mk/pthread.mk @@ -3,6 +3,8 @@ SRC_CC = semaphore.cc rwlock.cc \ LIBS += libc +INC_DIR += $(REP_DIR)/src/lib + vpath % $(REP_DIR)/src/lib/pthread SHARED_LIB = yes diff --git a/repos/libports/src/lib/pthread/thread.cc b/repos/libports/src/lib/pthread/thread.cc index e26f325bc6..45464d6f5b 100644 --- a/repos/libports/src/lib/pthread/thread.cc +++ b/repos/libports/src/lib/pthread/thread.cc @@ -111,6 +111,26 @@ extern "C" { /* Thread */ + int pthread_join(pthread_t thread, void **retval) + { + struct Check : Libc::Suspend_functor + { + bool suspend() override { + return true; + } + } check; + + while (!thread->exiting()) { + Libc::suspend(check); + } + + + thread->join(); + *((int **)retval) = 0; + + return 0; + } + int pthread_attr_init(pthread_attr_t *attr) { diff --git a/repos/libports/src/lib/pthread/thread.h b/repos/libports/src/lib/pthread/thread.h index fdd982b657..c283400566 100644 --- a/repos/libports/src/lib/pthread/thread.h +++ b/repos/libports/src/lib/pthread/thread.h @@ -20,6 +20,7 @@ #include #include +#include /* * Used by 'pthread_self()' to find out if the current thread is an alien @@ -105,6 +106,7 @@ struct pthread : Genode::Noncopyable, Genode::Thread::Tls::Base { start_routine_t _start_routine; void *_arg; + bool _exiting = false; enum { WEIGHT = Genode::Cpu_session::Weight::DEFAULT_WEIGHT }; @@ -120,6 +122,8 @@ struct pthread : Genode::Noncopyable, Genode::Thread::Tls::Base void entry() override { void *exit_status = _start_routine(_arg); + _exiting = true; + Libc::resume_all(); pthread_exit(exit_status); } }; @@ -187,6 +191,12 @@ struct pthread : Genode::Noncopyable, Genode::Thread::Tls::Base } void start() { _thread.start(); } + bool exiting() const + { + return _thread_object->_exiting; + } + + void join() { _thread.join(); } void *stack_top() const { return _thread.stack_top(); } void *stack_base() const { return _thread.stack_base(); }