1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 04:58:13 +00:00

Kernel: Fix some issues related to fixes and block conditions

Fix some problems with join blocks where the joining thread block
condition was added twice, which lead to a crash when trying to
unblock that condition a second time.

Deferred block condition evaluation by File objects were also not
properly keeping the File object alive, which lead to some random
crashes and corruption problems.

Other problems were caused by the fact that the Queued state didn't
handle signals/interruptions consistently. To solve these issues we
remove this state entirely, along with Thread::wait_on and change
the WaitQueue into a BlockCondition instead.

Also, deliver signals even if there isn't going to be a context switch
to another thread.

Fixes #4336 and #4330
This commit is contained in:
Tom 2020-12-07 21:29:41 -07:00 committed by Andreas Kling
parent 0918d8b1f8
commit da5cc34ebb
22 changed files with 474 additions and 434 deletions

View file

@ -81,11 +81,14 @@ Thread::JoinBlocker::JoinBlocker(Thread& joinee, KResult& try_join_result, void*
// We need to hold our lock to avoid a race where try_join succeeds
// but the joinee is joining immediately
ScopedSpinLock lock(m_lock);
try_join_result = joinee.try_join(*this);
try_join_result = joinee.try_join([&]() {
if (!set_block_condition(joinee.m_join_condition))
m_should_block = false;
});
m_join_error = try_join_result.is_error();
if (m_join_error)
m_should_block = false;
}
if (!set_block_condition(joinee.m_join_condition))
m_should_block = false;
}
void Thread::JoinBlocker::not_blocking(bool timeout_in_past)
@ -119,13 +122,36 @@ bool Thread::JoinBlocker::unblock(void* value, bool from_add_blocker)
return true;
}
Thread::QueueBlocker::QueueBlocker(WaitQueue& wait_queue, const char* block_reason)
: m_block_reason(block_reason)
{
if (!set_block_condition(wait_queue, Thread::current()))
m_should_block = false;
}
Thread::QueueBlocker::~QueueBlocker()
{
}
bool Thread::QueueBlocker::unblock()
{
{
ScopedSpinLock lock(m_lock);
if (m_did_unblock)
return false;
m_did_unblock = true;
}
unblock_from_blocker();
return true;
}
Thread::FileDescriptionBlocker::FileDescriptionBlocker(FileDescription& description, BlockFlags flags, BlockFlags& unblocked_flags)
: m_blocked_description(description)
, m_flags(flags)
, m_unblocked_flags(unblocked_flags)
{
m_unblocked_flags = BlockFlags::None;
if (!set_block_condition(description.block_condition()))
m_should_block = false;
}
@ -281,19 +307,13 @@ Thread::SelectBlocker::SelectBlocker(FDVector& fds)
continue;
if (!fd_entry.description->block_condition().add_blocker(*this, &fd_entry))
m_should_block = false;
m_registered_count++;
}
}
Thread::SelectBlocker::~SelectBlocker()
{
if (m_registered_count > 0) {
for (auto& fd_entry : m_fds) {
fd_entry.description->block_condition().remove_blocker(*this, &fd_entry);
if (--m_registered_count == 0)
break;
}
}
for (auto& fd_entry : m_fds)
fd_entry.description->block_condition().remove_blocker(*this, &fd_entry);
}
void Thread::SelectBlocker::not_blocking(bool timeout_in_past)