mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-24 23:23:58 +00:00
65cb406327
This is needed to eliminate a race in Thread::wait_on() where we'd otherwise have to wait until after unlocking the process lock before we can disable interrupts.
58 lines
1.5 KiB
C++
58 lines
1.5 KiB
C++
#include <Kernel/Lock.h>
|
|
#include <Kernel/Thread.h>
|
|
|
|
void Lock::lock()
|
|
{
|
|
ASSERT(!Scheduler::is_active());
|
|
if (!are_interrupts_enabled()) {
|
|
kprintf("Interrupts disabled when trying to take Lock{%s}\n", m_name);
|
|
dump_backtrace();
|
|
hang();
|
|
}
|
|
for (;;) {
|
|
bool expected = false;
|
|
if (m_lock.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
|
|
if (!m_holder || m_holder == current) {
|
|
m_holder = current;
|
|
++m_level;
|
|
m_lock.store(false, AK::memory_order_release);
|
|
return;
|
|
}
|
|
current->wait_on(m_queue, &m_lock, m_holder, m_name);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Lock::unlock()
|
|
{
|
|
for (;;) {
|
|
bool expected = false;
|
|
if (m_lock.compare_exchange_strong(expected, true, AK::memory_order_acq_rel)) {
|
|
ASSERT(m_holder == current);
|
|
ASSERT(m_level);
|
|
--m_level;
|
|
if (m_level) {
|
|
m_lock.store(false, AK::memory_order_release);
|
|
return;
|
|
}
|
|
m_holder = nullptr;
|
|
m_queue.wake_one(&m_lock);
|
|
return;
|
|
}
|
|
// I don't know *who* is using "m_lock", so just yield.
|
|
Scheduler::yield();
|
|
}
|
|
}
|
|
|
|
bool Lock::force_unlock_if_locked()
|
|
{
|
|
InterruptDisabler disabler;
|
|
if (m_holder != current)
|
|
return false;
|
|
ASSERT(m_level == 1);
|
|
ASSERT(m_holder == current);
|
|
m_holder = nullptr;
|
|
--m_level;
|
|
m_queue.wake_one();
|
|
return true;
|
|
}
|