2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-28 23:47:18 +00:00
|
|
|
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-23 21:32:53 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-16 12:18:22 +00:00
|
|
|
#include <AK/Assertions.h>
|
2019-10-12 17:17:34 +00:00
|
|
|
#include <AK/Atomic.h>
|
2020-12-14 23:36:22 +00:00
|
|
|
#include <AK/HashMap.h>
|
2019-12-01 10:57:20 +00:00
|
|
|
#include <AK/Types.h>
|
2020-02-16 00:50:16 +00:00
|
|
|
#include <Kernel/Forward.h>
|
2021-08-07 11:19:39 +00:00
|
|
|
#include <Kernel/Locking/LockLocation.h>
|
2021-07-18 07:09:04 +00:00
|
|
|
#include <Kernel/Locking/LockMode.h>
|
2019-12-01 10:57:20 +00:00
|
|
|
#include <Kernel/WaitQueue.h>
|
2019-01-16 15:03:50 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-07-17 19:09:51 +00:00
|
|
|
class Mutex {
|
2021-07-10 16:23:16 +00:00
|
|
|
friend class Thread;
|
|
|
|
|
2021-07-17 19:09:51 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(Mutex);
|
|
|
|
AK_MAKE_NONMOVABLE(Mutex);
|
2020-12-14 23:36:22 +00:00
|
|
|
|
2018-10-23 21:32:53 +00:00
|
|
|
public:
|
2020-12-14 23:36:22 +00:00
|
|
|
using Mode = LockMode;
|
|
|
|
|
2022-04-05 23:49:30 +00:00
|
|
|
// FIXME: remove this after annihilating Process::m_big_lock
|
|
|
|
enum class MutexBehavior {
|
|
|
|
Regular,
|
|
|
|
BigLock,
|
|
|
|
};
|
|
|
|
|
|
|
|
Mutex(StringView name = {}, MutexBehavior behavior = MutexBehavior::Regular)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_name(name)
|
2022-04-05 23:49:30 +00:00
|
|
|
, m_behavior(behavior)
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
}
|
2021-07-17 19:09:51 +00:00
|
|
|
~Mutex() = default;
|
2018-10-23 21:32:53 +00:00
|
|
|
|
2021-08-07 11:58:07 +00:00
|
|
|
void lock(Mode mode = Mode::Exclusive, LockLocation const& location = LockLocation::current());
|
2022-01-28 23:47:18 +00:00
|
|
|
void restore_exclusive_lock(u32, LockLocation const& location = LockLocation::current());
|
2021-04-24 22:17:02 +00:00
|
|
|
|
2019-01-16 15:03:50 +00:00
|
|
|
void unlock();
|
2022-01-28 23:47:18 +00:00
|
|
|
[[nodiscard]] Mode force_unlock_exclusive_if_locked(u32&);
|
2021-07-15 22:10:52 +00:00
|
|
|
[[nodiscard]] bool is_locked() const
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-07-15 22:10:52 +00:00
|
|
|
return m_mode != Mode::Unlocked;
|
|
|
|
}
|
2022-01-28 23:47:18 +00:00
|
|
|
|
|
|
|
[[nodiscard]] bool is_exclusively_locked_by_current_thread() const
|
2021-07-15 22:10:52 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_lock);
|
2022-01-28 23:47:18 +00:00
|
|
|
VERIFY(m_mode != Mode::Shared); // This method should only be used on exclusively-held locks
|
|
|
|
if (m_mode == Mode::Unlocked)
|
|
|
|
return false;
|
2023-04-02 19:36:39 +00:00
|
|
|
return m_holder == bit_cast<uintptr_t>(Thread::current());
|
2021-07-15 22:10:52 +00:00
|
|
|
}
|
2018-10-23 21:32:53 +00:00
|
|
|
|
2021-08-29 00:07:57 +00:00
|
|
|
[[nodiscard]] StringView name() const { return m_name; }
|
2019-02-07 10:12:23 +00:00
|
|
|
|
2021-08-29 00:07:57 +00:00
|
|
|
static StringView mode_to_string(Mode mode)
|
2020-12-14 23:36:22 +00:00
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case Mode::Unlocked:
|
2021-08-29 00:07:57 +00:00
|
|
|
return "unlocked"sv;
|
2020-12-14 23:36:22 +00:00
|
|
|
case Mode::Exclusive:
|
2021-08-29 00:07:57 +00:00
|
|
|
return "exclusive"sv;
|
2020-12-14 23:36:22 +00:00
|
|
|
case Mode::Shared:
|
2021-08-29 00:07:57 +00:00
|
|
|
return "shared"sv;
|
2020-12-14 23:36:22 +00:00
|
|
|
default:
|
2021-08-29 00:07:57 +00:00
|
|
|
return "invalid"sv;
|
2020-12-14 23:36:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:32:53 +00:00
|
|
|
private:
|
2021-09-09 12:00:59 +00:00
|
|
|
using BlockedThreadList = IntrusiveList<&Thread::m_blocked_threads_list_node>;
|
2021-07-10 16:23:16 +00:00
|
|
|
|
2022-04-05 23:49:30 +00:00
|
|
|
// FIXME: remove this after annihilating Process::m_big_lock
|
|
|
|
using BigLockBlockedThreadList = IntrusiveList<&Thread::m_big_lock_blocked_threads_list_node>;
|
|
|
|
|
2022-11-09 10:39:58 +00:00
|
|
|
// FIXME: Allow any lock rank.
|
|
|
|
void block(Thread&, Mode, SpinlockLocker<Spinlock<LockRank::None>>&, u32);
|
2021-07-10 16:23:16 +00:00
|
|
|
void unblock_waiters(Mode);
|
|
|
|
|
2021-08-29 00:07:57 +00:00
|
|
|
StringView m_name;
|
2021-07-10 16:23:16 +00:00
|
|
|
Mode m_mode { Mode::Unlocked };
|
2020-04-18 09:21:00 +00:00
|
|
|
|
2022-04-05 23:49:30 +00:00
|
|
|
// FIXME: remove this after annihilating Process::m_big_lock
|
|
|
|
MutexBehavior m_behavior;
|
|
|
|
|
2020-04-18 09:21:00 +00:00
|
|
|
// When locked exclusively, only the thread already holding the lock can
|
|
|
|
// lock it again. When locked in shared mode, any thread can do that.
|
|
|
|
u32 m_times_locked { 0 };
|
|
|
|
|
2023-04-02 19:36:39 +00:00
|
|
|
// The address of one of the threads that hold this lock, or 0.
|
|
|
|
// When locked in shared mode, this is stored on best effort basis: 0 does *not* mean
|
2020-04-18 09:21:00 +00:00
|
|
|
// the lock is unlocked, it just means we don't know which threads hold it.
|
2023-04-02 19:36:39 +00:00
|
|
|
// When locked exclusively, this is always the one thread that holds the lock.
|
|
|
|
uintptr_t m_holder { 0 };
|
2022-01-28 23:47:18 +00:00
|
|
|
size_t m_shared_holders { 0 };
|
2021-07-10 16:23:16 +00:00
|
|
|
|
2022-04-05 12:44:50 +00:00
|
|
|
struct BlockedThreadLists {
|
|
|
|
BlockedThreadList exclusive;
|
|
|
|
BlockedThreadList shared;
|
|
|
|
|
2022-04-05 23:49:30 +00:00
|
|
|
// FIXME: remove this after annihilating Process::m_big_lock
|
|
|
|
BigLockBlockedThreadList exclusive_big_lock;
|
|
|
|
|
2022-04-05 12:44:50 +00:00
|
|
|
ALWAYS_INLINE BlockedThreadList& list_for_mode(Mode mode)
|
|
|
|
{
|
|
|
|
VERIFY(mode == Mode::Exclusive || mode == Mode::Shared);
|
|
|
|
return mode == Mode::Exclusive ? exclusive : shared;
|
|
|
|
}
|
|
|
|
};
|
2022-08-18 19:46:28 +00:00
|
|
|
// FIXME: Use a specific lock rank passed by constructor.
|
2022-11-09 10:39:58 +00:00
|
|
|
SpinlockProtected<BlockedThreadLists, LockRank::None> m_blocked_thread_lists {};
|
2021-07-10 16:23:16 +00:00
|
|
|
|
2022-08-18 19:46:28 +00:00
|
|
|
// FIXME: See above.
|
2022-11-09 10:39:58 +00:00
|
|
|
mutable Spinlock<LockRank::None> m_lock {};
|
2022-01-28 23:47:18 +00:00
|
|
|
|
|
|
|
#if LOCK_SHARED_UPGRADE_DEBUG
|
2023-04-02 19:36:39 +00:00
|
|
|
HashMap<uintptr_t, u32> m_shared_holders_map;
|
2022-01-28 23:47:18 +00:00
|
|
|
#endif
|
2018-10-23 21:32:53 +00:00
|
|
|
};
|
|
|
|
|
2021-07-17 23:13:34 +00:00
|
|
|
class MutexLocker {
|
2021-07-19 17:32:55 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(MutexLocker);
|
|
|
|
|
2018-10-23 21:32:53 +00:00
|
|
|
public:
|
2021-07-19 17:32:55 +00:00
|
|
|
ALWAYS_INLINE explicit MutexLocker()
|
|
|
|
: m_lock(nullptr)
|
|
|
|
, m_locked(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-08-07 11:58:07 +00:00
|
|
|
ALWAYS_INLINE explicit MutexLocker(Mutex& l, Mutex::Mode mode = Mutex::Mode::Exclusive, LockLocation const& location = LockLocation::current())
|
2021-07-19 17:32:55 +00:00
|
|
|
: m_lock(&l)
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2021-07-19 17:32:55 +00:00
|
|
|
m_lock->lock(mode, location);
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2021-04-24 22:17:02 +00:00
|
|
|
|
2021-07-17 23:13:34 +00:00
|
|
|
ALWAYS_INLINE ~MutexLocker()
|
2021-01-15 02:14:10 +00:00
|
|
|
{
|
|
|
|
if (m_locked)
|
|
|
|
unlock();
|
|
|
|
}
|
2021-07-19 17:32:55 +00:00
|
|
|
|
2021-01-15 02:14:10 +00:00
|
|
|
ALWAYS_INLINE void unlock()
|
|
|
|
{
|
2021-07-19 17:32:55 +00:00
|
|
|
VERIFY(m_lock);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_locked);
|
2021-01-15 02:14:10 +00:00
|
|
|
m_locked = false;
|
2021-07-19 17:32:55 +00:00
|
|
|
m_lock->unlock();
|
|
|
|
}
|
|
|
|
|
2021-08-07 11:58:07 +00:00
|
|
|
ALWAYS_INLINE void attach_and_lock(Mutex& lock, Mutex::Mode mode = Mutex::Mode::Exclusive, LockLocation const& location = LockLocation::current())
|
2021-07-19 17:32:55 +00:00
|
|
|
{
|
|
|
|
VERIFY(!m_locked);
|
|
|
|
m_lock = &lock;
|
|
|
|
m_locked = true;
|
|
|
|
|
|
|
|
m_lock->lock(mode, location);
|
2021-01-15 02:14:10 +00:00
|
|
|
}
|
2021-04-24 22:17:02 +00:00
|
|
|
|
2021-08-07 11:58:07 +00:00
|
|
|
ALWAYS_INLINE void lock(Mutex::Mode mode = Mutex::Mode::Exclusive, LockLocation const& location = LockLocation::current())
|
2021-01-15 02:14:10 +00:00
|
|
|
{
|
2021-07-19 17:32:55 +00:00
|
|
|
VERIFY(m_lock);
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!m_locked);
|
2021-01-15 02:14:10 +00:00
|
|
|
m_locked = true;
|
2021-04-24 22:17:02 +00:00
|
|
|
|
2021-07-19 17:32:55 +00:00
|
|
|
m_lock->lock(mode, location);
|
2021-01-15 02:14:10 +00:00
|
|
|
}
|
2018-10-23 21:32:53 +00:00
|
|
|
|
|
|
|
private:
|
2021-07-19 17:32:55 +00:00
|
|
|
Mutex* m_lock;
|
2021-01-15 02:14:10 +00:00
|
|
|
bool m_locked { true };
|
2018-10-23 21:32:53 +00:00
|
|
|
};
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|