summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-09-04 21:49:08 +0200
committerAndreas Kling <kling@serenityos.org>2021-09-04 23:11:04 +0200
commit5d5a3708c459afb2a5de1cafc5afa3c6e5b6fee7 (patch)
tree6bb43caed331094d9f0627b61af765e02f250dc7
parentcfc1a628d59ac36186a643468065a6a826386131 (diff)
downloadserenity-5d5a3708c459afb2a5de1cafc5afa3c6e5b6fee7.zip
Kernel: Rename Thread::clone() => try_clone() and propagate errors
-rw-r--r--Kernel/Process.cpp26
-rw-r--r--Kernel/Thread.cpp10
-rw-r--r--Kernel/Thread.h2
3 files changed, 20 insertions, 18 deletions
diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp
index f10065d793..8ce170aa7e 100644
--- a/Kernel/Process.cpp
+++ b/Kernel/Process.cpp
@@ -275,17 +275,23 @@ Process::Process(const String& name, UserID uid, GroupID gid, ProcessID ppid, bo
KResult Process::attach_resources(NonnullOwnPtr<Memory::AddressSpace>&& preallocated_space, RefPtr<Thread>& first_thread, Process* fork_parent)
{
m_space = move(preallocated_space);
- if (fork_parent) {
- // NOTE: fork() doesn't clone all threads; the thread that called fork() becomes the only thread in the new process.
- first_thread = Thread::current()->clone(*this);
- if (!first_thread)
- return ENOMEM;
- } else {
+
+ auto thread_or_error = [&] {
+ if (fork_parent) {
+ // NOTE: fork() doesn't clone all threads; the thread that called fork() becomes the only thread in the new process.
+ return Thread::current()->try_clone(*this);
+ }
// NOTE: This non-forked code path is only taken when the kernel creates a process "manually" (at boot.)
- auto thread_or_error = Thread::try_create(*this);
- if (thread_or_error.is_error())
- return thread_or_error.error();
- first_thread = thread_or_error.release_value();
+ return Thread::try_create(*this);
+ }();
+
+ if (thread_or_error.is_error())
+ return thread_or_error.error();
+
+ first_thread = thread_or_error.release_value();
+
+ if (!fork_parent) {
+ // FIXME: Figure out if this is really necessary.
first_thread->detach();
}
return KSuccess;
diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp
index bf45dea28a..2140bbec9f 100644
--- a/Kernel/Thread.cpp
+++ b/Kernel/Thread.cpp
@@ -50,11 +50,7 @@ KResultOr<NonnullRefPtr<Thread>> Thread::try_create(NonnullRefPtr<Process> proce
auto name = KString::try_create(process->name());
- auto thread = adopt_ref_if_nonnull(new (nothrow) Thread(move(process), kernel_stack_region.release_nonnull(), block_timer.release_nonnull(), move(name)));
- if (!thread)
- return ENOMEM;
-
- return thread.release_nonnull();
+ return adopt_nonnull_ref_or_enomem(new (nothrow) Thread(move(process), kernel_stack_region.release_nonnull(), block_timer.release_nonnull(), move(name)));
}
Thread::Thread(NonnullRefPtr<Process> process, NonnullOwnPtr<Memory::Region> kernel_stack_region, NonnullRefPtr<Timer> block_timer, OwnPtr<KString> name)
@@ -1030,11 +1026,11 @@ RegisterState& Thread::get_register_dump_from_stack()
return *trap->regs;
}
-RefPtr<Thread> Thread::clone(Process& process)
+KResultOr<NonnullRefPtr<Thread>> Thread::try_clone(Process& process)
{
auto thread_or_error = Thread::try_create(process);
if (thread_or_error.is_error())
- return {};
+ return thread_or_error.error();
auto& clone = thread_or_error.value();
auto signal_action_data_span = m_signal_action_data.span();
signal_action_data_span.copy_to(clone->m_signal_action_data.span());
diff --git a/Kernel/Thread.h b/Kernel/Thread.h
index c7b3b56b2c..6b062ff980 100644
--- a/Kernel/Thread.h
+++ b/Kernel/Thread.h
@@ -1101,7 +1101,7 @@ public:
return !m_is_joinable;
}
- RefPtr<Thread> clone(Process&);
+ KResultOr<NonnullRefPtr<Thread>> try_clone(Process&);
template<IteratorFunction<Thread&> Callback>
static IterationDecision for_each_in_state(State, Callback);