mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:47:45 +00:00
Kernel: Allow Lock to block from BlockCondition
This enables the Lock class to block a thread even while the thread is working on a BlockCondition. A thread can still only be either blocked by a Lock or a BlockCondition. This also establishes a linked list of threads that are blocked by a Lock and unblocking directly unlocks threads and wakes them directly.
This commit is contained in:
parent
d9fb93c5ce
commit
026ffa343d
8 changed files with 442 additions and 267 deletions
533
Kernel/Lock.cpp
533
Kernel/Lock.cpp
|
@ -8,6 +8,7 @@
|
|||
#include <Kernel/Debug.h>
|
||||
#include <Kernel/KSyms.h>
|
||||
#include <Kernel/Lock.h>
|
||||
#include <Kernel/SpinLock.h>
|
||||
#include <Kernel/Thread.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
@ -23,71 +24,83 @@ void Lock::lock(Mode mode)
|
|||
VERIFY(!Processor::current().in_irq());
|
||||
VERIFY(mode != Mode::Unlocked);
|
||||
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) {
|
||||
// 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) {
|
||||
case Mode::Unlocked: {
|
||||
dbgln_if(LOCK_TRACE_DEBUG, "Lock::lock @ ({}) {}: acquire {}, currently unlocked", this, m_name, mode_to_string(mode));
|
||||
m_mode = mode;
|
||||
VERIFY(!m_holder);
|
||||
VERIFY(m_shared_holders.is_empty());
|
||||
if (mode == Mode::Exclusive) {
|
||||
m_holder = current_thread;
|
||||
} else {
|
||||
VERIFY(mode == Mode::Shared);
|
||||
m_shared_holders.set(current_thread, 1);
|
||||
}
|
||||
VERIFY(m_times_locked == 0);
|
||||
m_times_locked++;
|
||||
|
||||
#if LOCK_DEBUG
|
||||
if (current_thread) {
|
||||
current_thread->holding_lock(*this, 1, location);
|
||||
}
|
||||
#endif
|
||||
m_queue.should_block(true);
|
||||
m_lock.store(false, AK::memory_order_release);
|
||||
return;
|
||||
}
|
||||
case Mode::Exclusive: {
|
||||
VERIFY(m_holder);
|
||||
if (m_holder != current_thread)
|
||||
break;
|
||||
VERIFY(m_shared_holders.is_empty());
|
||||
|
||||
if constexpr (LOCK_TRACE_DEBUG) {
|
||||
if (mode == Mode::Exclusive)
|
||||
dbgln("Lock::lock @ {} ({}): acquire {}, currently exclusive, holding: {}", this, m_name, mode_to_string(mode), m_times_locked);
|
||||
else
|
||||
dbgln("Lock::lock @ {} ({}): acquire exclusive (requested {}), currently exclusive, holding: {}", this, m_name, mode_to_string(mode), m_times_locked);
|
||||
}
|
||||
|
||||
VERIFY(mode == Mode::Exclusive || mode == Mode::Shared);
|
||||
VERIFY(m_times_locked > 0);
|
||||
m_times_locked++;
|
||||
ScopedSpinLock lock(m_lock);
|
||||
bool did_block = false;
|
||||
Mode current_mode = m_mode;
|
||||
switch (current_mode) {
|
||||
case Mode::Unlocked: {
|
||||
dbgln_if(LOCK_TRACE_DEBUG, "Lock::lock @ ({}) {}: acquire {}, currently unlocked", this, m_name, mode_to_string(mode));
|
||||
m_mode = mode;
|
||||
VERIFY(!m_holder);
|
||||
VERIFY(m_shared_holders.is_empty());
|
||||
if (mode == Mode::Exclusive) {
|
||||
m_holder = current_thread;
|
||||
} else {
|
||||
VERIFY(mode == Mode::Shared);
|
||||
m_shared_holders.set(current_thread, 1);
|
||||
}
|
||||
VERIFY(m_times_locked == 0);
|
||||
m_times_locked++;
|
||||
|
||||
#if LOCK_DEBUG
|
||||
if (current_thread) {
|
||||
current_thread->holding_lock(*this, 1, location);
|
||||
#endif
|
||||
m_lock.store(false, AK::memory_order_release);
|
||||
return;
|
||||
}
|
||||
case Mode::Shared: {
|
||||
VERIFY(!m_holder);
|
||||
if (mode != Mode::Shared)
|
||||
break;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
case Mode::Exclusive: {
|
||||
VERIFY(m_holder);
|
||||
if (m_holder != current_thread) {
|
||||
block(*current_thread, mode, lock, 1);
|
||||
did_block = true;
|
||||
}
|
||||
|
||||
dbgln_if(LOCK_TRACE_DEBUG, "Lock::lock @ {} ({}): acquire {}, currently shared, locks held {}", this, m_name, mode_to_string(mode), m_times_locked);
|
||||
VERIFY(m_shared_holders.is_empty());
|
||||
|
||||
VERIFY(m_times_locked > 0);
|
||||
if constexpr (LOCK_TRACE_DEBUG) {
|
||||
if (mode == Mode::Exclusive)
|
||||
dbgln("Lock::lock @ {} ({}): acquire {}, currently exclusive, holding: {}", this, m_name, mode_to_string(mode), m_times_locked);
|
||||
else
|
||||
dbgln("Lock::lock @ {} ({}): acquire exclusive (requested {}), currently exclusive, holding: {}", this, m_name, mode_to_string(mode), m_times_locked);
|
||||
}
|
||||
|
||||
VERIFY(mode == Mode::Exclusive || mode == Mode::Shared);
|
||||
VERIFY(m_times_locked > 0);
|
||||
if (!did_block)
|
||||
m_times_locked++;
|
||||
|
||||
#if LOCK_DEBUG
|
||||
current_thread->holding_lock(*this, 1, location);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
case Mode::Shared: {
|
||||
VERIFY(!m_holder);
|
||||
if (mode == Mode::Exclusive && m_shared_holders.size() == 1) {
|
||||
auto it = m_shared_holders.begin();
|
||||
if (it->key == current_thread) {
|
||||
it->value++;
|
||||
m_times_locked++;
|
||||
m_mode = Mode::Exclusive;
|
||||
m_holder = current_thread;
|
||||
m_shared_holders.clear();
|
||||
dbgln_if(LOCK_TRACE_DEBUG, "Lock::lock @ {} ({}): acquire {}, converted shared to exclusive lock, locks held {}", this, m_name, mode_to_string(mode), m_times_locked);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (mode != Mode::Shared) {
|
||||
block(*current_thread, mode, lock, 1);
|
||||
did_block = true;
|
||||
}
|
||||
|
||||
dbgln_if(LOCK_TRACE_DEBUG, "Lock::lock @ {} ({}): acquire {}, currently shared, locks held {}", this, m_name, mode_to_string(mode), m_times_locked);
|
||||
|
||||
VERIFY(m_times_locked > 0);
|
||||
if (did_block) {
|
||||
VERIFY(m_shared_holders.contains(current_thread));
|
||||
} else {
|
||||
m_times_locked++;
|
||||
VERIFY(!m_shared_holders.is_empty());
|
||||
auto it = m_shared_holders.find(current_thread);
|
||||
|
@ -95,20 +108,15 @@ void Lock::lock(Mode mode)
|
|||
it->value++;
|
||||
else
|
||||
m_shared_holders.set(current_thread, 1);
|
||||
}
|
||||
|
||||
#if LOCK_DEBUG
|
||||
current_thread->holding_lock(*this, 1, location);
|
||||
current_thread->holding_lock(*this, 1, location);
|
||||
#endif
|
||||
m_lock.store(false, AK::memory_order_release);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
m_lock.store(false, AK::memory_order_release);
|
||||
dbgln_if(LOCK_TRACE_DEBUG, "Lock::lock @ {} ({}) waiting...", this, m_name);
|
||||
m_queue.wait_forever(m_name);
|
||||
dbgln_if(LOCK_TRACE_DEBUG, "Lock::lock @ {} ({}) waited", this, m_name);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -118,66 +126,107 @@ void Lock::unlock()
|
|||
// and also from within critical sections!
|
||||
VERIFY(!Processor::current().in_irq());
|
||||
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) {
|
||||
Mode current_mode = m_mode;
|
||||
if constexpr (LOCK_TRACE_DEBUG) {
|
||||
if (current_mode == Mode::Shared)
|
||||
dbgln("Lock::unlock @ {} ({}): release {}, locks held: {}", this, m_name, mode_to_string(current_mode), m_times_locked);
|
||||
else
|
||||
dbgln("Lock::unlock @ {} ({}): release {}, holding: {}", this, m_name, mode_to_string(current_mode), m_times_locked);
|
||||
}
|
||||
ScopedSpinLock lock(m_lock);
|
||||
Mode current_mode = m_mode;
|
||||
if constexpr (LOCK_TRACE_DEBUG) {
|
||||
if (current_mode == Mode::Shared)
|
||||
dbgln("Lock::unlock @ {} ({}): release {}, locks held: {}", this, m_name, mode_to_string(current_mode), m_times_locked);
|
||||
else
|
||||
dbgln("Lock::unlock @ {} ({}): release {}, holding: {}", this, m_name, mode_to_string(current_mode), m_times_locked);
|
||||
}
|
||||
|
||||
VERIFY(current_mode != Mode::Unlocked);
|
||||
VERIFY(current_mode != Mode::Unlocked);
|
||||
|
||||
VERIFY(m_times_locked > 0);
|
||||
m_times_locked--;
|
||||
VERIFY(m_times_locked > 0);
|
||||
m_times_locked--;
|
||||
|
||||
switch (current_mode) {
|
||||
case Mode::Exclusive:
|
||||
VERIFY(m_holder == current_thread);
|
||||
VERIFY(m_shared_holders.is_empty());
|
||||
if (m_times_locked == 0)
|
||||
m_holder = nullptr;
|
||||
break;
|
||||
case Mode::Shared: {
|
||||
VERIFY(!m_holder);
|
||||
auto it = m_shared_holders.find(current_thread);
|
||||
VERIFY(it != m_shared_holders.end());
|
||||
if (it->value > 1) {
|
||||
it->value--;
|
||||
} else {
|
||||
VERIFY(it->value > 0);
|
||||
m_shared_holders.remove(it);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
bool unlocked_last = (m_times_locked == 0);
|
||||
if (unlocked_last) {
|
||||
VERIFY(current_mode == Mode::Exclusive ? !m_holder : m_shared_holders.is_empty());
|
||||
m_mode = Mode::Unlocked;
|
||||
m_queue.should_block(false);
|
||||
}
|
||||
switch (current_mode) {
|
||||
case Mode::Exclusive:
|
||||
VERIFY(m_holder == current_thread);
|
||||
VERIFY(m_shared_holders.is_empty());
|
||||
if (m_times_locked == 0)
|
||||
m_holder = nullptr;
|
||||
break;
|
||||
case Mode::Shared: {
|
||||
VERIFY(!m_holder);
|
||||
auto it = m_shared_holders.find(current_thread);
|
||||
VERIFY(it != m_shared_holders.end());
|
||||
if (it->value > 1) {
|
||||
it->value--;
|
||||
} else {
|
||||
VERIFY(it->value > 0);
|
||||
m_shared_holders.remove(it);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
#if LOCK_DEBUG
|
||||
if (current_thread) {
|
||||
current_thread->holding_lock(*this, -1, {});
|
||||
}
|
||||
if (current_thread) {
|
||||
current_thread->holding_lock(*this, -1, {});
|
||||
}
|
||||
#endif
|
||||
m_lock.store(false, AK::memory_order_release);
|
||||
if (unlocked_last) {
|
||||
u32 did_wake = m_queue.wake_one();
|
||||
dbgln_if(LOCK_TRACE_DEBUG, "Lock::unlock @ {} ({}) wake one ({})", this, m_name, did_wake);
|
||||
}
|
||||
return;
|
||||
|
||||
if (m_times_locked == 0) {
|
||||
VERIFY(current_mode == Mode::Exclusive ? !m_holder : m_shared_holders.is_empty());
|
||||
|
||||
m_mode = Mode::Unlocked;
|
||||
unblock_waiters(current_mode);
|
||||
}
|
||||
}
|
||||
|
||||
void Lock::block(Thread& current_thread, Mode mode, ScopedSpinLock<SpinLock<u8>>& lock, u32 requested_locks)
|
||||
{
|
||||
auto& blocked_thread_list = thread_list_for_mode(mode);
|
||||
VERIFY(!blocked_thread_list.contains(current_thread));
|
||||
blocked_thread_list.append(current_thread);
|
||||
|
||||
dbgln_if(LOCK_TRACE_DEBUG, "Lock::lock @ {} ({}) waiting...", this, m_name);
|
||||
current_thread.block(*this, lock, requested_locks);
|
||||
dbgln_if(LOCK_TRACE_DEBUG, "Lock::lock @ {} ({}) waited", this, m_name);
|
||||
|
||||
VERIFY(blocked_thread_list.contains(current_thread));
|
||||
blocked_thread_list.remove(current_thread);
|
||||
}
|
||||
|
||||
void Lock::unblock_waiters(Mode previous_mode)
|
||||
{
|
||||
VERIFY(m_times_locked == 0);
|
||||
VERIFY(m_mode == Mode::Unlocked);
|
||||
|
||||
if (m_blocked_threads_list_exclusive.is_empty() && m_blocked_threads_list_shared.is_empty())
|
||||
return;
|
||||
|
||||
auto unblock_shared = [&]() {
|
||||
if (m_blocked_threads_list_shared.is_empty())
|
||||
return false;
|
||||
m_mode = Mode::Shared;
|
||||
for (auto& thread : m_blocked_threads_list_shared) {
|
||||
auto requested_locks = thread.unblock_from_lock(*this);
|
||||
auto set_result = m_shared_holders.set(&thread, requested_locks);
|
||||
VERIFY(set_result == AK::HashSetResult::InsertedNewEntry);
|
||||
m_times_locked += requested_locks;
|
||||
}
|
||||
// I don't know *who* is using "m_lock", so just yield.
|
||||
Scheduler::yield_from_critical();
|
||||
return true;
|
||||
};
|
||||
auto unblock_exclusive = [&]() {
|
||||
if (auto* next_exclusive_thread = m_blocked_threads_list_exclusive.first()) {
|
||||
m_mode = Mode::Exclusive;
|
||||
m_times_locked = next_exclusive_thread->unblock_from_lock(*this);
|
||||
m_holder = next_exclusive_thread;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
if (previous_mode == Mode::Exclusive) {
|
||||
if (!unblock_shared())
|
||||
unblock_exclusive();
|
||||
} else {
|
||||
if (!unblock_exclusive())
|
||||
unblock_shared();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -187,77 +236,61 @@ auto Lock::force_unlock_if_locked(u32& lock_count_to_restore) -> Mode
|
|||
// and also from within critical sections!
|
||||
VERIFY(!Processor::current().in_irq());
|
||||
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) {
|
||||
Mode previous_mode;
|
||||
auto current_mode = m_mode.load(AK::MemoryOrder::memory_order_relaxed);
|
||||
switch (current_mode) {
|
||||
case Mode::Exclusive: {
|
||||
if (m_holder != current_thread) {
|
||||
m_lock.store(false, AK::MemoryOrder::memory_order_release);
|
||||
lock_count_to_restore = 0;
|
||||
return Mode::Unlocked;
|
||||
}
|
||||
|
||||
dbgln_if(LOCK_RESTORE_DEBUG, "Lock::force_unlock_if_locked @ {}: unlocking exclusive with lock count: {}", this, m_times_locked);
|
||||
#if LOCK_DEBUG
|
||||
m_holder->holding_lock(*this, -(int)m_times_locked, {});
|
||||
#endif
|
||||
m_holder = nullptr;
|
||||
VERIFY(m_times_locked > 0);
|
||||
lock_count_to_restore = m_times_locked;
|
||||
m_times_locked = 0;
|
||||
m_mode = Mode::Unlocked;
|
||||
m_queue.should_block(false);
|
||||
m_lock.store(false, AK::memory_order_release);
|
||||
previous_mode = Mode::Exclusive;
|
||||
break;
|
||||
}
|
||||
case Mode::Shared: {
|
||||
VERIFY(!m_holder);
|
||||
auto it = m_shared_holders.find(current_thread);
|
||||
if (it == m_shared_holders.end()) {
|
||||
m_lock.store(false, AK::MemoryOrder::memory_order_release);
|
||||
lock_count_to_restore = 0;
|
||||
return Mode::Unlocked;
|
||||
}
|
||||
|
||||
dbgln_if(LOCK_RESTORE_DEBUG, "Lock::force_unlock_if_locked @ {}: unlocking exclusive with lock count: {}, total locks: {}",
|
||||
this, it->value, m_times_locked);
|
||||
|
||||
VERIFY(it->value > 0);
|
||||
lock_count_to_restore = it->value;
|
||||
VERIFY(lock_count_to_restore > 0);
|
||||
#if LOCK_DEBUG
|
||||
m_holder->holding_lock(*this, -(int)lock_count_to_restore, {});
|
||||
#endif
|
||||
m_shared_holders.remove(it);
|
||||
VERIFY(m_times_locked >= lock_count_to_restore);
|
||||
m_times_locked -= lock_count_to_restore;
|
||||
if (m_times_locked == 0) {
|
||||
m_mode = Mode::Unlocked;
|
||||
m_queue.should_block(false);
|
||||
}
|
||||
m_lock.store(false, AK::memory_order_release);
|
||||
previous_mode = Mode::Shared;
|
||||
break;
|
||||
}
|
||||
case Mode::Unlocked: {
|
||||
m_lock.store(false, AK::memory_order_relaxed);
|
||||
lock_count_to_restore = 0;
|
||||
previous_mode = Mode::Unlocked;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
m_queue.wake_one();
|
||||
return previous_mode;
|
||||
ScopedSpinLock lock(m_lock);
|
||||
auto current_mode = m_mode;
|
||||
switch (current_mode) {
|
||||
case Mode::Exclusive: {
|
||||
if (m_holder != current_thread) {
|
||||
lock_count_to_restore = 0;
|
||||
return Mode::Unlocked;
|
||||
}
|
||||
// I don't know *who* is using "m_lock", so just yield.
|
||||
Scheduler::yield_from_critical();
|
||||
|
||||
dbgln_if(LOCK_RESTORE_DEBUG, "Lock::force_unlock_if_locked @ {}: unlocking exclusive with lock count: {}", this, m_times_locked);
|
||||
#if LOCK_DEBUG
|
||||
m_holder->holding_lock(*this, -(int)m_times_locked, {});
|
||||
#endif
|
||||
m_holder = nullptr;
|
||||
VERIFY(m_times_locked > 0);
|
||||
lock_count_to_restore = m_times_locked;
|
||||
m_times_locked = 0;
|
||||
m_mode = Mode::Unlocked;
|
||||
unblock_waiters(Mode::Exclusive);
|
||||
break;
|
||||
}
|
||||
case Mode::Shared: {
|
||||
VERIFY(!m_holder);
|
||||
auto it = m_shared_holders.find(current_thread);
|
||||
if (it == m_shared_holders.end()) {
|
||||
lock_count_to_restore = 0;
|
||||
return Mode::Unlocked;
|
||||
}
|
||||
|
||||
dbgln_if(LOCK_RESTORE_DEBUG, "Lock::force_unlock_if_locked @ {}: unlocking exclusive with lock count: {}, total locks: {}",
|
||||
this, it->value, m_times_locked);
|
||||
|
||||
VERIFY(it->value > 0);
|
||||
lock_count_to_restore = it->value;
|
||||
VERIFY(lock_count_to_restore > 0);
|
||||
#if LOCK_DEBUG
|
||||
m_holder->holding_lock(*this, -(int)lock_count_to_restore, {});
|
||||
#endif
|
||||
m_shared_holders.remove(it);
|
||||
VERIFY(m_times_locked >= lock_count_to_restore);
|
||||
m_times_locked -= lock_count_to_restore;
|
||||
if (m_times_locked == 0) {
|
||||
m_mode = Mode::Unlocked;
|
||||
unblock_waiters(Mode::Shared);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Mode::Unlocked: {
|
||||
lock_count_to_restore = 0;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
return current_mode;
|
||||
}
|
||||
|
||||
#if LOCK_DEBUG
|
||||
|
@ -270,68 +303,66 @@ void Lock::restore_lock(Mode mode, u32 lock_count)
|
|||
VERIFY(lock_count > 0);
|
||||
VERIFY(!Processor::current().in_irq());
|
||||
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) {
|
||||
switch (mode) {
|
||||
case Mode::Exclusive: {
|
||||
auto expected_mode = Mode::Unlocked;
|
||||
if (!m_mode.compare_exchange_strong(expected_mode, Mode::Exclusive))
|
||||
break;
|
||||
|
||||
dbgln_if(LOCK_RESTORE_DEBUG, "Lock::restore_lock @ {}: restoring {} with lock count {}, was unlocked", this, mode_to_string(mode), lock_count);
|
||||
|
||||
VERIFY(m_times_locked == 0);
|
||||
m_times_locked = lock_count;
|
||||
VERIFY(!m_holder);
|
||||
VERIFY(m_shared_holders.is_empty());
|
||||
m_holder = current_thread;
|
||||
m_queue.should_block(true);
|
||||
m_lock.store(false, AK::memory_order_release);
|
||||
|
||||
#if LOCK_DEBUG
|
||||
m_holder->holding_lock(*this, (int)lock_count, location);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
case Mode::Shared: {
|
||||
auto expected_mode = Mode::Unlocked;
|
||||
if (!m_mode.compare_exchange_strong(expected_mode, Mode::Shared) && expected_mode != Mode::Shared)
|
||||
break;
|
||||
|
||||
dbgln_if(LOCK_RESTORE_DEBUG, "Lock::restore_lock @ {}: restoring {} with lock count {}, was {}",
|
||||
this, mode_to_string(mode), lock_count, mode_to_string(expected_mode));
|
||||
|
||||
VERIFY(expected_mode == Mode::Shared || m_times_locked == 0);
|
||||
m_times_locked += lock_count;
|
||||
VERIFY(!m_holder);
|
||||
VERIFY((expected_mode == Mode::Unlocked) == m_shared_holders.is_empty());
|
||||
auto set_result = m_shared_holders.set(current_thread, lock_count);
|
||||
// There may be other shared lock holders already, but we should not have an entry yet
|
||||
VERIFY(set_result == AK::HashSetResult::InsertedNewEntry);
|
||||
m_queue.should_block(true);
|
||||
m_lock.store(false, AK::memory_order_release);
|
||||
|
||||
#if LOCK_DEBUG
|
||||
m_holder->holding_lock(*this, (int)lock_count, location);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
m_lock.store(false, AK::memory_order_relaxed);
|
||||
bool did_block = false;
|
||||
ScopedSpinLock lock(m_lock);
|
||||
switch (mode) {
|
||||
case Mode::Exclusive: {
|
||||
if (m_mode != Mode::Unlocked) {
|
||||
block(*current_thread, Mode::Exclusive, lock, lock_count);
|
||||
did_block = true;
|
||||
}
|
||||
// I don't know *who* is using "m_lock", so just yield.
|
||||
Scheduler::yield_from_critical();
|
||||
|
||||
dbgln_if(LOCK_RESTORE_DEBUG, "Lock::restore_lock @ {}: restoring {} with lock count {}, was unlocked", this, mode_to_string(mode), lock_count);
|
||||
|
||||
VERIFY(m_shared_holders.is_empty());
|
||||
if (did_block) {
|
||||
VERIFY(m_mode == Mode::Exclusive);
|
||||
VERIFY(m_times_locked > 0);
|
||||
VERIFY(m_holder == current_thread);
|
||||
} else {
|
||||
m_mode = Mode::Exclusive;
|
||||
VERIFY(m_times_locked == 0);
|
||||
m_times_locked = lock_count;
|
||||
VERIFY(!m_holder);
|
||||
m_holder = current_thread;
|
||||
}
|
||||
|
||||
#if LOCK_DEBUG
|
||||
m_holder->holding_lock(*this, (int)lock_count, location);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
case Mode::Shared: {
|
||||
auto previous_mode = m_mode;
|
||||
if (m_mode != Mode::Unlocked && m_mode != Mode::Shared) {
|
||||
block(*current_thread, Mode::Shared, lock, lock_count);
|
||||
did_block = true;
|
||||
}
|
||||
|
||||
dbgln_if(LOCK_RESTORE_DEBUG, "Lock::restore_lock @ {}: restoring {} with lock count {}, was {}",
|
||||
this, mode_to_string(mode), lock_count, mode_to_string(previous_mode));
|
||||
|
||||
VERIFY(!m_holder);
|
||||
if (did_block) {
|
||||
VERIFY(m_mode == Mode::Shared);
|
||||
VERIFY(m_times_locked > 0);
|
||||
VERIFY(m_shared_holders.contains(current_thread));
|
||||
} else {
|
||||
m_mode = Mode::Shared;
|
||||
m_times_locked += lock_count;
|
||||
auto set_result = m_shared_holders.set(current_thread, lock_count);
|
||||
// There may be other shared lock holders already, but we should not have an entry yet
|
||||
VERIFY(set_result == AK::HashSetResult::InsertedNewEntry);
|
||||
}
|
||||
|
||||
#if LOCK_DEBUG
|
||||
m_holder->holding_lock(*this, (int)lock_count, location);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
void Lock::clear_waiters()
|
||||
{
|
||||
VERIFY(m_mode != Mode::Shared);
|
||||
m_queue.wake_all();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue