mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:58:11 +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:
parent
71df0ee994
commit
aff64b6a03
2 changed files with 5 additions and 5 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue