mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-25 05:55:13 +00:00
The kernel's Lock class now uses a proper wait queue internally instead of just having everyone wake up regularly to try to acquire the lock. We also keep the donation mechanism, so that whenever someone tries to take the lock and fails, that thread donates the remainder of its timeslice to the current lock holder. After unlocking a Lock, the unlocking thread calls WaitQueue::wake_one, which unblocks the next thread in queue.
34 lines
593 B
C++
34 lines
593 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()
|
|
{
|
|
InterruptDisabler disabler;
|
|
if (m_threads.is_empty())
|
|
return;
|
|
if (auto* thread = m_threads.take_first())
|
|
thread->unblock();
|
|
}
|
|
|
|
void WaitQueue::wake_all()
|
|
{
|
|
InterruptDisabler disabler;
|
|
if (m_threads.is_empty())
|
|
return;
|
|
while (!m_threads.is_empty())
|
|
m_threads.take_first()->unblock();
|
|
}
|