1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:07:36 +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(); auto current_thread = Thread::current();
ScopedCritical critical; // in case we're not in a critical section already ScopedCritical critical; // in case we're not in a critical section already
for (;;) { for (;;) {
if (m_lock.exchange(true, AK::memory_order_acq_rel) == false) { if (m_lock.exchange(true, AK::memory_order_acq_rel) != false) {
do { // 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. // FIXME: Do not add new readers if writers are queued.
Mode current_mode = m_mode; Mode current_mode = m_mode;
switch (current_mode) { switch (current_mode) {
@ -121,11 +125,7 @@ void Lock::lock(Mode mode)
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
m_lock.store(false, AK::memory_order_release); m_lock.store(false, AK::memory_order_release);
} while (m_queue.wait_on({}, m_name) == Thread::BlockResult::NotBlocked); m_queue.wait_on({}, m_name);
} else {
// I don't know *who* is using "m_lock", so just yield.
Scheduler::yield_from_critical();
}
} }
} }