From 95b086f47f4f63eadcb70c9b817739db77a0e197 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 7 Dec 2019 14:47:00 +0100 Subject: [PATCH] Kernel+LibPthread: Implement pthread_detach() --- Kernel/Process.cpp | 21 +++++++++++++++++++++ Kernel/Process.h | 1 + Kernel/Syscall.h | 3 ++- Libraries/LibPthread/pthread.cpp | 5 +++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index b7041c190a8..be12cef9377 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -2915,6 +2915,27 @@ void Process::sys$exit_thread(void* exit_value) ASSERT_NOT_REACHED(); } +int Process::sys$detach_thread(int tid) +{ + Thread* thread = nullptr; + for_each_thread([&](auto& child_thread) { + if (child_thread.tid() == tid) { + thread = &child_thread; + return IterationDecision::Break; + } + return IterationDecision::Continue; + }); + + if (!thread) + return -ESRCH; + + if (!thread->is_joinable()) + return -EINVAL; + + thread->set_joinable(false); + return 0; +} + int Process::sys$join_thread(int tid, void** exit_value) { if (exit_value && !validate_write_typed(exit_value)) diff --git a/Kernel/Process.h b/Kernel/Process.h index 192605afee3..bfa31ee58de 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -204,6 +204,7 @@ public: int sys$create_thread(void* (*)(void*), void* argument, const Syscall::SC_create_thread_params*); void sys$exit_thread(void*); int sys$join_thread(int tid, void** exit_value); + int sys$detach_thread(int tid); int sys$rename(const char* oldpath, const char* newpath); int sys$systrace(pid_t); int sys$mknod(const char* pathname, mode_t, dev_t); diff --git a/Kernel/Syscall.h b/Kernel/Syscall.h index d72199f55ba..dcafa120e2d 100644 --- a/Kernel/Syscall.h +++ b/Kernel/Syscall.h @@ -141,7 +141,8 @@ typedef u32 socklen_t; __ENUMERATE_SYSCALL(openat) \ __ENUMERATE_SYSCALL(join_thread) \ __ENUMERATE_SYSCALL(module_load) \ - __ENUMERATE_SYSCALL(module_unload) + __ENUMERATE_SYSCALL(module_unload) \ + __ENUMERATE_SYSCALL(detach_thread) namespace Syscall { diff --git a/Libraries/LibPthread/pthread.cpp b/Libraries/LibPthread/pthread.cpp index d3a54b8e69f..8e5e368005d 100644 --- a/Libraries/LibPthread/pthread.cpp +++ b/Libraries/LibPthread/pthread.cpp @@ -85,6 +85,11 @@ int pthread_join(pthread_t thread, void** exit_value_ptr) return syscall(SC_join_thread, thread, exit_value_ptr); } +int pthread_detach(pthread_t thread) +{ + return syscall(SC_detach_thread, thread); +} + int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attributes) { // FIXME: Implement mutex attributes