diff options
author | Andreas Kling <kling@serenityos.org> | 2021-09-06 12:44:27 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-06 13:06:05 +0200 |
commit | 7981422500696278269473d6c6f10269d0a94459 (patch) | |
tree | 978e22ce322efa72b680bbd21b953d2cf7f23a3a /Kernel | |
parent | cda2b9e71cca1793d47427fb5b43b10dd0f3fbb6 (diff) | |
download | serenity-7981422500696278269473d6c6f10269d0a94459.zip |
Kernel: Make Threads always have a name
We previously allowed Thread to exist in a state where its m_name was
null, and had to work around that in various places.
This patch removes that possibility and forces those who would create a
thread (or change the name of one) to provide a NonnullOwnPtr<KString>
with the name.
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Process.cpp | 2 | ||||
-rw-r--r-- | Kernel/Process.h | 2 | ||||
-rw-r--r-- | Kernel/Scheduler.cpp | 4 | ||||
-rw-r--r-- | Kernel/Syscalls/execve.cpp | 9 | ||||
-rw-r--r-- | Kernel/Syscalls/thread.cpp | 7 | ||||
-rw-r--r-- | Kernel/Thread.cpp | 6 | ||||
-rw-r--r-- | Kernel/Thread.h | 8 |
7 files changed, 25 insertions, 13 deletions
diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 48915789ff..01677c62d0 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -739,7 +739,7 @@ KResult Process::send_signal(u8 signal, Process* sender) return ESRCH; } -RefPtr<Thread> Process::create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, OwnPtr<KString> name, u32 affinity, bool joinable) +RefPtr<Thread> Process::create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, NonnullOwnPtr<KString> name, u32 affinity, bool joinable) { VERIFY((priority >= THREAD_PRIORITY_MIN) && (priority <= THREAD_PRIORITY_MAX)); diff --git a/Kernel/Process.h b/Kernel/Process.h index 1a4dc0db14..b5fc545539 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -187,7 +187,7 @@ public: static NonnullRefPtrVector<Process> all_processes(); - RefPtr<Thread> create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, OwnPtr<KString> name, u32 affinity = THREAD_AFFINITY_DEFAULT, bool joinable = true); + RefPtr<Thread> create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, NonnullOwnPtr<KString> name, u32 affinity = THREAD_AFFINITY_DEFAULT, bool joinable = true); bool is_profiling() const { return m_profiling; } void set_profiling(bool profiling) { m_profiling = profiling; } diff --git a/Kernel/Scheduler.cpp b/Kernel/Scheduler.cpp index 6dfd27a246..40110d1c7c 100644 --- a/Kernel/Scheduler.cpp +++ b/Kernel/Scheduler.cpp @@ -417,7 +417,7 @@ UNMAP_AFTER_INIT void Scheduler::initialize() VERIFY(s_colonel_process); VERIFY(idle_thread); idle_thread->set_priority(THREAD_PRIORITY_MIN); - idle_thread->set_name(KString::try_create("idle thread #0")); + idle_thread->set_name(KString::must_create("idle thread #0")); set_idle_thread(idle_thread); } @@ -436,7 +436,7 @@ UNMAP_AFTER_INIT Thread* Scheduler::create_ap_idle_thread(u32 cpu) VERIFY(Processor::is_bootstrap_processor()); VERIFY(s_colonel_process); - Thread* idle_thread = s_colonel_process->create_kernel_thread(idle_loop, nullptr, THREAD_PRIORITY_MIN, KString::try_create(String::formatted("idle thread #{}", cpu)), 1 << cpu, false); + Thread* idle_thread = s_colonel_process->create_kernel_thread(idle_loop, nullptr, THREAD_PRIORITY_MIN, KString::must_create(String::formatted("idle thread #{}", cpu)), 1 << cpu, false); VERIFY(idle_thread); return idle_thread; } diff --git a/Kernel/Syscalls/execve.cpp b/Kernel/Syscalls/execve.cpp index 716a77a5e6..be34bcced4 100644 --- a/Kernel/Syscalls/execve.cpp +++ b/Kernel/Syscalls/execve.cpp @@ -498,6 +498,11 @@ KResult Process::do_exec(NonnullRefPtr<FileDescription> main_program_description if (parts.is_empty()) return ENOENT; + auto new_process_name = parts.take_last(); + auto new_main_thread_name = KString::try_create(new_process_name); + if (!new_main_thread_name) + return ENOMEM; + auto main_program_metadata = main_program_description->metadata(); auto load_result = TRY(load(main_program_description, interpreter_description, main_program_header)); @@ -620,8 +625,8 @@ KResult Process::do_exec(NonnullRefPtr<FileDescription> main_program_description // NOTE: Be careful to not trigger any page faults below! - m_name = parts.take_last(); - new_main_thread->set_name(KString::try_create(m_name)); + m_name = move(new_process_name); + new_main_thread->set_name(new_main_thread_name.release_nonnull()); { ProtectedDataMutationScope scope { *this }; diff --git a/Kernel/Syscalls/thread.cpp b/Kernel/Syscalls/thread.cpp index 8fd41b408b..41652be6e8 100644 --- a/Kernel/Syscalls/thread.cpp +++ b/Kernel/Syscalls/thread.cpp @@ -43,11 +43,16 @@ KResultOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<c auto thread = TRY(Thread::try_create(*this)); + // FIXME: Don't make a temporary String here + auto new_thread_name = KString::try_create(String::formatted("{} [{}]", m_name, thread->tid().value())); + if (!new_thread_name) + return ENOMEM; + // We know this thread is not the main_thread, // So give it a unique name until the user calls $set_thread_name on it // length + 4 to give space for our extra junk at the end StringBuilder builder(m_name.length() + 4); - thread->set_name(KString::try_create(String::formatted("{} [{}]", m_name, thread->tid().value()))); + thread->set_name(new_thread_name.release_nonnull()); if (!is_thread_joinable) thread->detach(); diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index 9731891bde..69d223427b 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -47,11 +47,13 @@ KResultOr<NonnullRefPtr<Thread>> Thread::try_create(NonnullRefPtr<Process> proce return ENOMEM; auto name = KString::try_create(process->name()); + if (!name) + return ENOMEM; - return adopt_nonnull_ref_or_enomem(new (nothrow) Thread(move(process), move(kernel_stack_region), block_timer.release_nonnull(), move(name))); + return adopt_nonnull_ref_or_enomem(new (nothrow) Thread(move(process), move(kernel_stack_region), block_timer.release_nonnull(), name.release_nonnull())); } -Thread::Thread(NonnullRefPtr<Process> process, NonnullOwnPtr<Memory::Region> kernel_stack_region, NonnullRefPtr<Timer> block_timer, OwnPtr<KString> name) +Thread::Thread(NonnullRefPtr<Process> process, NonnullOwnPtr<Memory::Region> kernel_stack_region, NonnullRefPtr<Timer> block_timer, NonnullOwnPtr<KString> name) : m_process(move(process)) , m_kernel_stack_region(move(kernel_stack_region)) , m_name(move(name)) diff --git a/Kernel/Thread.h b/Kernel/Thread.h index e7470ac023..f2788e8758 100644 --- a/Kernel/Thread.h +++ b/Kernel/Thread.h @@ -194,10 +194,10 @@ public: { // NOTE: Whoever is calling this needs to be holding our lock while reading the name. VERIFY(m_lock.is_locked_by_current_processor()); - return m_name ? m_name->view() : StringView {}; + return m_name->view(); } - void set_name(OwnPtr<KString> name) + void set_name(NonnullOwnPtr<KString> name) { SpinlockLocker lock(m_lock); m_name = move(name); @@ -1202,7 +1202,7 @@ public: String backtrace(); private: - Thread(NonnullRefPtr<Process>, NonnullOwnPtr<Memory::Region>, NonnullRefPtr<Timer>, OwnPtr<KString>); + Thread(NonnullRefPtr<Process>, NonnullOwnPtr<Memory::Region>, NonnullRefPtr<Timer>, NonnullOwnPtr<KString>); IntrusiveListNode<Thread> m_process_thread_list_node; int m_runnable_priority { -1 }; @@ -1333,7 +1333,7 @@ private: FPUState m_fpu_state {}; State m_state { Invalid }; - OwnPtr<KString> m_name; + NonnullOwnPtr<KString> m_name; u32 m_priority { THREAD_PRIORITY_NORMAL }; State m_stop_state { Invalid }; |