diff options
author | sin-ack <sin-ack@users.noreply.github.com> | 2021-08-15 08:25:29 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-15 12:17:29 +0200 |
commit | 98408b8920e703979f8f0feb5fdc9e67a6de729e (patch) | |
tree | 417f6f434f75f1a803f25dd8a2e925e208fa2439 | |
parent | 4520863c0e21728f06a3c2535f5f859e95150136 (diff) | |
download | serenity-98408b8920e703979f8f0feb5fdc9e67a6de729e.zip |
Kernel: Don't hold the process list lock while destructing the process
Once we remove the process from the process list, we're free to do
whatever we want without any locks.
-rw-r--r-- | Kernel/Process.cpp | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 6c225ab345..9b1a05b078 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -302,16 +302,19 @@ bool Process::unref() const // NOTE: We need to obtain the process list lock before doing anything, // because otherwise someone might get in between us lowering the // refcount and acquiring the lock. - return processes().with_exclusive([&](auto& list) { + auto did_hit_zero = processes().with_exclusive([&](auto& list) { auto new_ref_count = deref_base(); if (new_ref_count > 0) return false; if (m_list_node.is_in_list()) list.remove(*const_cast<Process*>(this)); - delete this; return true; }); + + if (did_hit_zero) + delete this; + return did_hit_zero; } // Make sure the compiler doesn't "optimize away" this function: |