From 194776d226b6530a9e161bf0f7c769f5532b1a86 Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Thu, 20 Oct 2022 21:49:40 -0700 Subject: [PATCH] [kernel] Remove status code from thread exit The status code from thread exit had too many issues, (eg, how does it relate to process exit code? what happens when different threads exit with different exit codes?) and not enough value, so I'm getting rid of it. --- definitions/objects/thread.def | 4 +--- src/kernel/objects/process.cpp | 13 ++++--------- src/kernel/objects/thread.cpp | 4 ++-- src/kernel/objects/thread.h | 7 +------ src/kernel/syscalls/thread.cpp | 24 ++++++++++++------------ src/user/drv.uart/main.cpp | 2 +- 6 files changed, 21 insertions(+), 33 deletions(-) diff --git a/definitions/objects/thread.def b/definitions/objects/thread.def index 8ac3c8c..757b527 100644 --- a/definitions/objects/thread.def +++ b/definitions/objects/thread.def @@ -13,9 +13,7 @@ object thread : object { method kill [destructor cap:kill] - method exit [static] { - param status int32 - } + method exit [static] method sleep [static] { param duration uint64 diff --git a/src/kernel/objects/process.cpp b/src/kernel/objects/process.cpp index 03d4a39..724ef53 100644 --- a/src/kernel/objects/process.cpp +++ b/src/kernel/objects/process.cpp @@ -55,7 +55,7 @@ process::exit(int32_t code) m_return_code = code; for (auto *thread : m_threads) { - thread->exit(code); + thread->exit(); } if (this == current_cpu().process) @@ -68,21 +68,17 @@ process::update() kassert(m_threads.count() > 0, "process::update with zero threads!"); size_t i = 0; - uint32_t status = 0; while (i < m_threads.count()) { thread *th = m_threads[i]; if (th->has_state(thread::state::exited)) { - status = th->m_return_code; m_threads.remove_swap_at(i); continue; } i++; } - if (m_threads.count() == 0) { - // TODO: What really is the return code in this case? - exit(status); - } + if (m_threads.count() == 0) + exit(-1); } thread * @@ -106,7 +102,6 @@ bool process::thread_exited(thread *th) { kassert(&th->m_parent == this, "Process got thread_exited for non-child!"); - uint32_t status = th->m_return_code; m_threads.remove_swap(th); remove_handle(th->self_handle()); delete th; @@ -114,7 +109,7 @@ process::thread_exited(thread *th) // TODO: delete the thread's stack VMA if (m_threads.count() == 0) { - exit(status); + exit(-1); return true; } diff --git a/src/kernel/objects/thread.cpp b/src/kernel/objects/thread.cpp index 56001eb..e24934d 100644 --- a/src/kernel/objects/thread.cpp +++ b/src/kernel/objects/thread.cpp @@ -71,11 +71,11 @@ thread::wake_only() } void -thread::exit(int32_t code) +thread::exit() { - m_return_code = code; m_wake_timeout = 0; set_state(state::exited); + m_join_queue.clear(); block(); } diff --git a/src/kernel/objects/thread.h b/src/kernel/objects/thread.h index e23ccc9..9b2abbe 100644 --- a/src/kernel/objects/thread.h +++ b/src/kernel/objects/thread.h @@ -138,8 +138,7 @@ public: inline process & parent() { return m_parent; } /// Terminate this thread. - /// \arg code The return code to exit with. - void exit(int32_t code); + void exit(); /// Add a stack header that returns to the given address in kernel space. /// \arg rip The address to return to, must be kernel space @@ -188,10 +187,6 @@ private: state m_state; - // There should be 3 bytes of padding here - - int32_t m_return_code; - uint64_t m_wake_value; uint64_t m_wake_timeout; message_data m_message_data; diff --git a/src/kernel/syscalls/thread.cpp b/src/kernel/syscalls/thread.cpp index d1af3d4..bb1467a 100644 --- a/src/kernel/syscalls/thread.cpp +++ b/src/kernel/syscalls/thread.cpp @@ -28,25 +28,25 @@ thread_create(j6_handle_t *self, process *proc, uintptr_t stack_top, uintptr_t e return j6_status_ok; } -j6_status_t -thread_exit(int32_t status) -{ - thread &th = thread::current(); - log::verbose(logs::task, "Thread %llx exiting with code %d", th.koid(), status); - th.exit(status); - - log::error(logs::task, "returned to exit syscall"); - return j6_err_unexpected; -} - j6_status_t thread_kill(thread *self) { log::verbose(logs::task, "Killing thread %llx", self->koid()); - self->exit(-1); + self->exit(); return j6_status_ok; } +j6_status_t +thread_exit() +{ + thread &th = thread::current(); + log::verbose(logs::task, "Thread %llx exiting", th.koid()); + th.exit(); + + log::error(logs::task, "returned to exit syscall"); + return j6_err_unexpected; +} + j6_status_t thread_sleep(uint64_t duration) { diff --git a/src/user/drv.uart/main.cpp b/src/user/drv.uart/main.cpp index ecf1285..70dbd4c 100644 --- a/src/user/drv.uart/main.cpp +++ b/src/user/drv.uart/main.cpp @@ -83,7 +83,7 @@ channel_pump_loop() void pump_proc() { - j6_thread_exit(channel_pump_loop()); + j6_process_exit(channel_pump_loop()); } int