2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
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>
|
2020-12-14 23:36:22 +00:00
|
|
|
#include <Kernel/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;
|
|
|
|
|
2021-07-17 19:09:51 +00:00
|
|
|
Mutex(const char* name = nullptr)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_name(name)
|
|
|
|
{
|
|
|
|
}
|
2021-07-17 19:09:51 +00:00
|
|
|
~Mutex() = default;
|
2018-10-23 21:32:53 +00:00
|
|
|
|
2021-01-23 22:29:11 +00:00
|
|
|
#if LOCK_DEBUG
|
2021-04-24 22:17:02 +00:00
|
|
|
void lock(Mode mode = Mode::Exclusive, const SourceLocation& location = SourceLocation::current());
|
|
|
|
void restore_lock(Mode, u32, const SourceLocation& location = SourceLocation::current());
|
|
|
|
#else
|
|
|
|
void lock(Mode = Mode::Exclusive);
|
|
|
|
void restore_lock(Mode, u32);
|
2020-12-01 02:04:36 +00:00
|
|
|
#endif
|
2021-04-24 22:17:02 +00:00
|
|
|
|
2019-01-16 15:03:50 +00:00
|
|
|
void unlock();
|
2020-12-14 23:36:22 +00:00
|
|
|
[[nodiscard]] Mode force_unlock_if_locked(u32&);
|
2021-07-15 22:10:52 +00:00
|
|
|
[[nodiscard]] bool is_locked() const
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
return m_mode != Mode::Unlocked;
|
|
|
|
}
|
|
|
|
[[nodiscard]] bool own_lock() const
|
|
|
|
{
|
|
|
|
ScopedSpinLock lock(m_lock);
|
|
|
|
if (m_mode == Mode::Exclusive)
|
|
|
|
return m_holder == Thread::current();
|
|
|
|
if (m_mode == Mode::Shared)
|
|
|
|
return m_shared_holders.contains(Thread::current());
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-23 21:32:53 +00:00
|
|
|
|
2021-02-14 23:24:44 +00:00
|
|
|
[[nodiscard]] const char* name() const { return m_name; }
|
2019-02-07 10:12:23 +00:00
|
|
|
|
2020-12-14 23:36:22 +00:00
|
|
|
static const char* mode_to_string(Mode mode)
|
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case Mode::Unlocked:
|
|
|
|
return "unlocked";
|
|
|
|
case Mode::Exclusive:
|
|
|
|
return "exclusive";
|
|
|
|
case Mode::Shared:
|
|
|
|
return "shared";
|
|
|
|
default:
|
|
|
|
return "invalid";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:32:53 +00:00
|
|
|
private:
|
2021-07-10 16:23:16 +00:00
|
|
|
typedef IntrusiveList<Thread, RawPtr<Thread>, &Thread::m_blocked_threads_list_node> BlockedThreadList;
|
|
|
|
|
|
|
|
ALWAYS_INLINE BlockedThreadList& thread_list_for_mode(Mode mode)
|
|
|
|
{
|
|
|
|
VERIFY(mode == Mode::Exclusive || mode == Mode::Shared);
|
|
|
|
return mode == Mode::Exclusive ? m_blocked_threads_list_exclusive : m_blocked_threads_list_shared;
|
|
|
|
}
|
|
|
|
|
|
|
|
void block(Thread&, Mode, ScopedSpinLock<SpinLock<u8>>&, u32);
|
|
|
|
void unblock_waiters(Mode);
|
|
|
|
|
2019-02-07 10:12:23 +00:00
|
|
|
const char* m_name { nullptr };
|
2021-07-10 16:23:16 +00:00
|
|
|
Mode m_mode { Mode::Unlocked };
|
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 };
|
|
|
|
|
|
|
|
// One of the threads that hold this lock, or nullptr. When locked in shared
|
|
|
|
// mode, this is stored on best effort basis: nullptr value does *not* mean
|
|
|
|
// the lock is unlocked, it just means we don't know which threads hold it.
|
|
|
|
// When locked exclusively, this is always the one thread that holds the
|
|
|
|
// lock.
|
2020-11-30 00:09:14 +00:00
|
|
|
RefPtr<Thread> m_holder;
|
2020-12-14 23:36:22 +00:00
|
|
|
HashMap<Thread*, u32> m_shared_holders;
|
2021-07-10 16:23:16 +00:00
|
|
|
|
|
|
|
BlockedThreadList m_blocked_threads_list_exclusive;
|
|
|
|
BlockedThreadList m_blocked_threads_list_shared;
|
|
|
|
|
2021-07-15 22:10:52 +00:00
|
|
|
mutable SpinLock<u8> m_lock;
|
2018-10-23 21:32:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Locker {
|
|
|
|
public:
|
2021-01-23 22:29:11 +00:00
|
|
|
#if LOCK_DEBUG
|
2021-07-17 19:09:51 +00:00
|
|
|
ALWAYS_INLINE explicit Locker(Mutex& l, Mutex::Mode mode = Mutex::Mode::Exclusive, const SourceLocation& location = SourceLocation::current())
|
2021-04-24 22:17:02 +00:00
|
|
|
#else
|
2021-07-17 19:09:51 +00:00
|
|
|
ALWAYS_INLINE explicit Locker(Mutex& l, Mutex::Mode mode = Mutex::Mode::Exclusive)
|
2021-04-24 22:17:02 +00:00
|
|
|
#endif
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_lock(l)
|
|
|
|
{
|
2021-04-24 22:17:02 +00:00
|
|
|
#if LOCK_DEBUG
|
|
|
|
m_lock.lock(mode, location);
|
|
|
|
#else
|
2020-04-18 09:21:00 +00:00
|
|
|
m_lock.lock(mode);
|
2021-04-24 22:17:02 +00:00
|
|
|
#endif
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2021-04-24 22:17:02 +00:00
|
|
|
|
2021-01-15 02:14:10 +00:00
|
|
|
ALWAYS_INLINE ~Locker()
|
|
|
|
{
|
|
|
|
if (m_locked)
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE void unlock()
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_locked);
|
2021-01-15 02:14:10 +00:00
|
|
|
m_locked = false;
|
|
|
|
m_lock.unlock();
|
|
|
|
}
|
2021-04-24 22:17:02 +00:00
|
|
|
|
|
|
|
#if LOCK_DEBUG
|
2021-07-17 19:09:51 +00:00
|
|
|
ALWAYS_INLINE void lock(Mutex::Mode mode = Mutex::Mode::Exclusive, const SourceLocation& location = SourceLocation::current())
|
2021-04-24 22:17:02 +00:00
|
|
|
#else
|
2021-07-17 19:09:51 +00:00
|
|
|
ALWAYS_INLINE void lock(Mutex::Mode mode = Mutex::Mode::Exclusive)
|
2021-04-24 22:17:02 +00:00
|
|
|
#endif
|
2021-01-15 02:14:10 +00:00
|
|
|
{
|
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
|
|
|
|
|
|
|
#if LOCK_DEBUG
|
|
|
|
m_lock.lock(mode, location);
|
|
|
|
#else
|
2021-01-15 02:14:10 +00:00
|
|
|
m_lock.lock(mode);
|
2021-04-24 22:17:02 +00:00
|
|
|
#endif
|
2021-01-15 02:14:10 +00:00
|
|
|
}
|
2018-10-23 21:32:53 +00:00
|
|
|
|
2021-07-17 19:09:51 +00:00
|
|
|
Mutex& get_lock() { return m_lock; }
|
|
|
|
const Mutex& get_lock() const { return m_lock; }
|
2021-02-06 18:33:31 +00:00
|
|
|
|
2018-10-23 21:32:53 +00:00
|
|
|
private:
|
2021-07-17 19:09:51 +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
|
|
|
};
|
|
|
|
|
2019-02-08 08:46:13 +00:00
|
|
|
template<typename T>
|
|
|
|
class Lockable {
|
|
|
|
public:
|
2021-02-28 13:42:08 +00:00
|
|
|
Lockable() = default;
|
2019-05-28 09:53:16 +00:00
|
|
|
Lockable(T&& resource)
|
|
|
|
: m_resource(move(resource))
|
|
|
|
{
|
|
|
|
}
|
2021-07-17 19:09:51 +00:00
|
|
|
[[nodiscard]] Mutex& lock() { return m_lock; }
|
2021-02-14 23:24:44 +00:00
|
|
|
[[nodiscard]] T& resource() { return m_resource; }
|
2019-02-08 08:46:13 +00:00
|
|
|
|
2021-02-14 23:24:44 +00:00
|
|
|
[[nodiscard]] T lock_and_copy()
|
2019-02-08 15:18:24 +00:00
|
|
|
{
|
2021-04-24 22:27:32 +00:00
|
|
|
Locker locker(m_lock);
|
2019-02-08 15:18:24 +00:00
|
|
|
return m_resource;
|
|
|
|
}
|
|
|
|
|
2019-02-08 08:46:13 +00:00
|
|
|
private:
|
|
|
|
T m_resource;
|
2021-07-17 19:09:51 +00:00
|
|
|
Mutex m_lock;
|
2019-02-08 08:46:13 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2021-02-06 18:33:31 +00:00
|
|
|
class ScopedLockRelease {
|
|
|
|
AK_MAKE_NONCOPYABLE(ScopedLockRelease);
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedLockRelease& operator=(ScopedLockRelease&&) = delete;
|
|
|
|
|
2021-07-17 19:09:51 +00:00
|
|
|
ScopedLockRelease(Mutex& lock)
|
2021-02-06 18:33:31 +00:00
|
|
|
: m_lock(&lock)
|
|
|
|
, m_previous_mode(lock.force_unlock_if_locked(m_previous_recursions))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ScopedLockRelease(ScopedLockRelease&& from)
|
|
|
|
: m_lock(exchange(from.m_lock, nullptr))
|
2021-07-17 19:09:51 +00:00
|
|
|
, m_previous_mode(exchange(from.m_previous_mode, Mutex::Mode::Unlocked))
|
2021-02-06 18:33:31 +00:00
|
|
|
, m_previous_recursions(exchange(from.m_previous_recursions, 0))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedLockRelease()
|
|
|
|
{
|
2021-07-17 19:09:51 +00:00
|
|
|
if (m_lock && m_previous_mode != Mutex::Mode::Unlocked)
|
2021-02-06 18:33:31 +00:00
|
|
|
m_lock->restore_lock(m_previous_mode, m_previous_recursions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void restore_lock()
|
|
|
|
{
|
|
|
|
VERIFY(m_lock);
|
2021-07-17 19:09:51 +00:00
|
|
|
if (m_previous_mode != Mutex::Mode::Unlocked) {
|
2021-02-06 18:33:31 +00:00
|
|
|
m_lock->restore_lock(m_previous_mode, m_previous_recursions);
|
2021-07-17 19:09:51 +00:00
|
|
|
m_previous_mode = Mutex::Mode::Unlocked;
|
2021-02-06 18:33:31 +00:00
|
|
|
m_previous_recursions = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_not_restore()
|
|
|
|
{
|
|
|
|
VERIFY(m_lock);
|
2021-07-17 19:09:51 +00:00
|
|
|
m_previous_mode = Mutex::Mode::Unlocked;
|
2021-02-06 18:33:31 +00:00
|
|
|
m_previous_recursions = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-17 19:09:51 +00:00
|
|
|
Mutex* m_lock;
|
|
|
|
Mutex::Mode m_previous_mode;
|
2021-02-06 18:33:31 +00:00
|
|
|
u32 m_previous_recursions;
|
|
|
|
};
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|