ladybird/Kernel/WaitQueue.cpp
Andreas Kling 41376d4662 Kernel: Fix Lock racing to the WaitQueue
There was a time window between releasing Lock::m_lock and calling into
the lock's WaitQueue where someone else could take m_lock and bring two
threads into a deadlock situation.

Fix this issue by holding Lock::m_lock until interrupts are disabled by
either Thread::wait_on() or WaitQueue::wake_one().
2020-01-12 19:04:16 +01:00

39 lines
723 B
C++

#include <Kernel/Thread.h>
#include <Kernel/WaitQueue.h>
WaitQueue::WaitQueue()
{
}
WaitQueue::~WaitQueue()
{
}
void WaitQueue::enqueue(Thread& thread)
{
InterruptDisabler disabler;
m_threads.append(thread);
}
void WaitQueue::wake_one(Atomic<bool>* lock)
{
InterruptDisabler disabler;
if (lock)
*lock = false;
if (m_threads.is_empty())
return;
if (auto* thread = m_threads.take_first())
thread->wake_from_queue();
Scheduler::stop_idling();
}
void WaitQueue::wake_all()
{
InterruptDisabler disabler;
if (m_threads.is_empty())
return;
while (!m_threads.is_empty())
m_threads.take_first()->wake_from_queue();
Scheduler::stop_idling();
}