mirror of
https://github.com/fergalmoran/ladybird.git
synced 2026-01-03 07:07:23 +00:00
LibThreading: Make Thread keep itself alive while its action is running
Previously, a `Thread` could be deleted while its action was running, even if it was running detached. By changing it to be atomically reference counted, and incrementing the count when starting the action, we can keep the Thread and its running action `Function` alive until it exits. Thus, detached `Thread` objects can be deleted by the thread creating them and allowed to die naturally.
This commit is contained in:
@@ -23,8 +23,6 @@ Thread::~Thread()
|
||||
dbgln("Destroying {} while it is still running undetached!", *this);
|
||||
[[maybe_unused]] auto res = join();
|
||||
}
|
||||
if (m_state == ThreadState::Detached)
|
||||
dbgln("Bug! {} in state {} is being destroyed; AK/Function will crash shortly!", *this, m_state.load());
|
||||
}
|
||||
|
||||
ErrorOr<void> Thread::set_priority(int priority)
|
||||
@@ -80,7 +78,8 @@ void Thread::start()
|
||||
// FIXME: Use pthread_attr_t to start a thread detached if that was requested by the user before the call to start().
|
||||
nullptr,
|
||||
[](void* arg) -> void* {
|
||||
Thread* self = static_cast<Thread*>(arg);
|
||||
auto self = adopt_ref(*static_cast<Thread*>(arg));
|
||||
|
||||
auto exit_code = self->m_action();
|
||||
|
||||
auto expected = Threading::ThreadState::Running;
|
||||
@@ -100,7 +99,7 @@ void Thread::start()
|
||||
|
||||
return reinterpret_cast<void*>(exit_code);
|
||||
},
|
||||
static_cast<void*>(this));
|
||||
&NonnullRefPtr(*this).leak_ref());
|
||||
|
||||
VERIFY(rc == 0);
|
||||
#ifdef AK_OS_SERENITY
|
||||
|
||||
Reference in New Issue
Block a user