123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <Kernel/Debug.h>
- #include <Kernel/KSyms.h>
- #include <Kernel/Locking/LockLocation.h>
- #include <Kernel/Locking/Mutex.h>
- #include <Kernel/Locking/Spinlock.h>
- #include <Kernel/Thread.h>
- namespace Kernel {
- void Mutex::lock(Mode mode, [[maybe_unused]] LockLocation const& location)
- {
- // NOTE: This may be called from an interrupt handler (not an IRQ handler)
- // and also from within critical sections!
- VERIFY(!Processor::current_in_irq());
- if constexpr (LOCK_IN_CRITICAL_DEBUG)
- VERIFY_INTERRUPTS_ENABLED();
- VERIFY(mode != Mode::Unlocked);
- auto* current_thread = Thread::current();
- SpinlockLocker lock(m_lock);
- bool did_block = false;
- Mode current_mode = m_mode;
- switch (current_mode) {
- case Mode::Unlocked: {
- dbgln_if(LOCK_TRACE_DEBUG, "Mutex::lock @ ({}) {}: acquire {}, currently unlocked", this, m_name, mode_to_string(mode));
- m_mode = mode;
- VERIFY(!m_holder);
- VERIFY(m_shared_holders == 0);
- if (mode == Mode::Exclusive) {
- m_holder = current_thread;
- } else {
- VERIFY(mode == Mode::Shared);
- ++m_shared_holders;
- #if LOCK_SHARED_UPGRADE_DEBUG
- m_shared_holders_map.set(current_thread, 1);
- #endif
- }
- VERIFY(m_times_locked == 0);
- m_times_locked++;
- #if LOCK_DEBUG
- if (current_thread) {
- current_thread->holding_lock(*this, 1, location);
- }
- #endif
- return;
- }
- case Mode::Exclusive: {
- VERIFY(m_holder);
- if (m_holder != current_thread) {
- block(*current_thread, mode, lock, 1);
- did_block = true;
- // If we blocked then m_mode should have been updated to what we requested
- VERIFY(m_mode == mode);
- }
- if (m_mode == Mode::Exclusive) {
- VERIFY(m_holder == current_thread);
- VERIFY(m_shared_holders == 0);
- } else if (did_block && mode == Mode::Shared) {
- // Only if we blocked trying to acquire a shared lock the lock would have been converted
- VERIFY(!m_holder);
- VERIFY(m_shared_holders > 0);
- }
- if constexpr (LOCK_TRACE_DEBUG) {
- if (mode == Mode::Exclusive)
- dbgln("Mutex::lock @ {} ({}): acquire {}, currently exclusive, holding: {}", this, m_name, mode_to_string(mode), m_times_locked);
- else
- dbgln("Mutex::lock @ {} ({}): acquire exclusive (requested {}), currently exclusive, holding: {}", this, m_name, mode_to_string(mode), m_times_locked);
- }
- VERIFY(m_times_locked > 0);
- if (!did_block) {
- // if we didn't block we must still be an exclusive lock
- VERIFY(m_mode == Mode::Exclusive);
- 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) {
- dbgln_if(LOCK_TRACE_DEBUG, "Mutex::lock @ {} ({}): blocking for exclusive access, currently shared, locks held {}", this, m_name, m_times_locked);
- #if LOCK_SHARED_UPGRADE_DEBUG
- VERIFY(m_shared_holders_map.size() != 1 || m_shared_holders_map.begin()->key != current_thread);
- #endif
- // WARNING: The following block will deadlock if the current thread is the only shared locker of this Mutex
- // and is asking to upgrade the lock to be exclusive without first releasing the shared lock. We have no
- // allocation-free way to detect such a scenario, so if you suspect that this is the cause of your deadlock,
- // try turning on LOCK_SHARED_UPGRADE_DEBUG.
- block(*current_thread, mode, lock, 1);
- did_block = true;
- VERIFY(m_mode == mode);
- }
- dbgln_if(LOCK_TRACE_DEBUG, "Mutex::lock @ {} ({}): acquire {}, currently shared, locks held {}", this, m_name, mode_to_string(mode), m_times_locked);
- VERIFY(m_times_locked > 0);
- if (m_mode == Mode::Shared) {
- VERIFY(!m_holder);
- VERIFY(!did_block);
- } else if (did_block) {
- VERIFY(mode == Mode::Exclusive);
- VERIFY(m_holder == current_thread);
- VERIFY(m_shared_holders == 0);
- }
- if (!did_block) {
- // if we didn't block we must still be a shared lock
- VERIFY(m_mode == Mode::Shared);
- m_times_locked++;
- VERIFY(m_shared_holders > 0);
- ++m_shared_holders;
- #if LOCK_SHARED_UPGRADE_DEBUG
- auto it = m_shared_holders_map.find(current_thread);
- if (it != m_shared_holders_map.end())
- it->value++;
- else
- m_shared_holders_map.set(current_thread, 1);
- #endif
- }
- #if LOCK_DEBUG
- current_thread->holding_lock(*this, 1, location);
- #endif
- return;
- }
- default:
- VERIFY_NOT_REACHED();
- }
- }
- void Mutex::unlock()
- {
- // NOTE: This may be called from an interrupt handler (not an IRQ handler)
- // and also from within critical sections!
- if constexpr (LOCK_IN_CRITICAL_DEBUG)
- VERIFY_INTERRUPTS_ENABLED();
- VERIFY(!Processor::current_in_irq());
- auto* current_thread = Thread::current();
- SpinlockLocker lock(m_lock);
- Mode current_mode = m_mode;
- if constexpr (LOCK_TRACE_DEBUG) {
- if (current_mode == Mode::Shared)
- dbgln("Mutex::unlock @ {} ({}): release {}, locks held: {}", this, m_name, mode_to_string(current_mode), m_times_locked);
- else
- dbgln("Mutex::unlock @ {} ({}): release {}, holding: {}", this, m_name, mode_to_string(current_mode), m_times_locked);
- }
- VERIFY(current_mode != Mode::Unlocked);
- VERIFY(m_times_locked > 0);
- m_times_locked--;
- switch (current_mode) {
- case Mode::Exclusive:
- VERIFY(m_holder == current_thread);
- VERIFY(m_shared_holders == 0);
- if (m_times_locked == 0)
- m_holder = nullptr;
- break;
- case Mode::Shared: {
- VERIFY(!m_holder);
- VERIFY(m_shared_holders > 0);
- --m_shared_holders;
- #if LOCK_SHARED_UPGRADE_DEBUG
- auto it = m_shared_holders_map.find(current_thread);
- if (it->value > 1)
- it->value--;
- else
- m_shared_holders_map.remove(it);
- #endif
- break;
- }
- default:
- VERIFY_NOT_REACHED();
- }
- #if LOCK_DEBUG
- if (current_thread) {
- current_thread->holding_lock(*this, -1, {});
- }
- #endif
- if (m_times_locked == 0) {
- VERIFY(current_mode == Mode::Exclusive ? !m_holder : m_shared_holders == 0);
- m_mode = Mode::Unlocked;
- unblock_waiters(current_mode);
- }
- }
- void Mutex::block(Thread& current_thread, Mode mode, SpinlockLocker<Spinlock>& lock, u32 requested_locks)
- {
- if constexpr (LOCK_IN_CRITICAL_DEBUG)
- VERIFY_INTERRUPTS_ENABLED();
- 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, "Mutex::lock @ {} ({}) waiting...", this, m_name);
- current_thread.block(*this, lock, requested_locks);
- dbgln_if(LOCK_TRACE_DEBUG, "Mutex::lock @ {} ({}) waited", this, m_name);
- VERIFY(blocked_thread_list.contains(current_thread));
- blocked_thread_list.remove(current_thread);
- }
- void Mutex::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_mutex(*this);
- m_shared_holders += requested_locks;
- #if LOCK_SHARED_UPGRADE_DEBUG
- auto set_result = m_shared_holders_map.set(&thread, requested_locks);
- VERIFY(set_result == AK::HashSetResult::InsertedNewEntry);
- #endif
- m_times_locked += requested_locks;
- }
- 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_mutex(*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();
- }
- }
- auto Mutex::force_unlock_exclusive_if_locked(u32& lock_count_to_restore) -> Mode
- {
- // NOTE: This may be called from an interrupt handler (not an IRQ handler)
- // and also from within critical sections!
- VERIFY(!Processor::current_in_irq());
- auto* current_thread = Thread::current();
- SpinlockLocker 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;
- }
- dbgln_if(LOCK_RESTORE_DEBUG, "Mutex::force_unlock_exclusive_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::Unlocked: {
- lock_count_to_restore = 0;
- break;
- }
- default:
- VERIFY_NOT_REACHED();
- }
- return current_mode;
- }
- void Mutex::restore_exclusive_lock(u32 lock_count, [[maybe_unused]] LockLocation const& location)
- {
- VERIFY(lock_count > 0);
- VERIFY(!Processor::current_in_irq());
- auto* current_thread = Thread::current();
- bool did_block = false;
- SpinlockLocker lock(m_lock);
- [[maybe_unused]] auto previous_mode = m_mode;
- if (m_mode == Mode::Exclusive && m_holder != current_thread) {
- block(*current_thread, Mode::Exclusive, lock, lock_count);
- did_block = true;
- // If we blocked then m_mode should have been updated to what we requested
- VERIFY(m_mode == Mode::Exclusive);
- }
- dbgln_if(LOCK_RESTORE_DEBUG, "Mutex::restore_exclusive_lock @ {}: restoring exclusive with lock count {}, was {}", this, lock_count, mode_to_string(previous_mode));
- VERIFY(m_mode != Mode::Shared);
- VERIFY(m_shared_holders == 0);
- if (did_block) {
- VERIFY(m_times_locked > 0);
- VERIFY(m_holder == current_thread);
- } else {
- if (m_mode == Mode::Unlocked) {
- m_mode = Mode::Exclusive;
- VERIFY(m_times_locked == 0);
- m_times_locked = lock_count;
- VERIFY(!m_holder);
- m_holder = current_thread;
- } else {
- VERIFY(m_mode == Mode::Exclusive);
- VERIFY(m_holder == current_thread);
- VERIFY(m_times_locked > 0);
- m_times_locked += lock_count;
- }
- }
- #if LOCK_DEBUG
- m_holder->holding_lock(*this, (int)lock_count, location);
- #endif
- }
- }
|