1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 10:37:45 +00:00

Kernel: Fix race condition in Lock::lock that may leave corrupted state

This commit is contained in:
Tom 2021-01-23 10:43:52 -07:00 committed by Andreas Kling
parent d4668507d4
commit 4cf0859612

View file

@ -50,8 +50,12 @@ void Lock::lock(Mode mode)
auto current_thread = Thread::current();
ScopedCritical critical; // in case we're not in a critical section already
for (;;) {
if (m_lock.exchange(true, AK::memory_order_acq_rel) == false) {
do {
if (m_lock.exchange(true, AK::memory_order_acq_rel) != false) {
// I don't know *who* is using "m_lock", so just yield.
Scheduler::yield_from_critical();
continue;
}
// FIXME: Do not add new readers if writers are queued.
Mode current_mode = m_mode;
switch (current_mode) {
@ -121,11 +125,7 @@ void Lock::lock(Mode mode)
ASSERT_NOT_REACHED();
}
m_lock.store(false, AK::memory_order_release);
} while (m_queue.wait_on({}, m_name) == Thread::BlockResult::NotBlocked);
} else {
// I don't know *who* is using "m_lock", so just yield.
Scheduler::yield_from_critical();
}
m_queue.wait_on({}, m_name);
}
}