2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2018-10-24 00:32:53 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-16 15:18:22 +03:00
|
|
|
#include <AK/Assertions.h>
|
2019-10-12 20:17:34 +03:00
|
|
|
#include <AK/Atomic.h>
|
2020-12-15 02:36:22 +03:00
|
|
|
#include <AK/HashMap.h>
|
2019-12-01 13:57:20 +03:00
|
|
|
#include <AK/Types.h>
|
2020-02-16 03:50:16 +03:00
|
|
|
#include <Kernel/Forward.h>
|
2020-12-15 02:36:22 +03:00
|
|
|
#include <Kernel/LockMode.h>
|
2019-12-01 13:57:20 +03:00
|
|
|
#include <Kernel/WaitQueue.h>
|
2019-01-16 18:03:50 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-07-17 22:09:51 +03:00
|
|
|
class Mutex {
|
2021-07-10 19:23:16 +03:00
|
|
|
friend class Thread;
|
|
|
|
|
2021-07-17 22:09:51 +03:00
|
|
|
AK_MAKE_NONCOPYABLE(Mutex);
|
|
|
|
AK_MAKE_NONMOVABLE(Mutex);
|
2020-12-15 02:36:22 +03:00
|
|
|
|
2018-10-24 00:32:53 +03:00
|
|
|
public:
|
2020-12-15 02:36:22 +03:00
|
|
|
using Mode = LockMode;
|
|
|
|
|
2021-07-17 22:09:51 +03:00
|
|
|
Mutex(const char* name = nullptr)
|
2019-05-28 12:53:16 +03:00
|
|
|
: m_name(name)
|
|
|
|
{
|
|
|
|
}
|
2021-07-17 22:09:51 +03:00
|
|
|
~Mutex() = default;
|
2018-10-24 00:32:53 +03:00
|
|
|
|
2021-01-24 01:29:11 +03:00
|
|
|
#if LOCK_DEBUG
|
2021-04-25 01:17:02 +03: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 05:04:36 +03:00
|
|
|
#endif
|
2021-04-25 01:17:02 +03:00
|
|
|
|
2019-01-16 18:03:50 +03:00
|
|
|
void unlock();
|
2020-12-15 02:36:22 +03:00
|
|
|
[[nodiscard]] Mode force_unlock_if_locked(u32&);
|
2021-07-16 01:10:52 +03: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-24 00:32:53 +03:00
|
|
|
|
2021-02-15 02:24:44 +03:00
|
|
|
[[nodiscard]] const char* name() const { return m_name; }
|
2019-02-07 13:12:23 +03:00
|
|
|
|
2020-12-15 02:36:22 +03: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-24 00:32:53 +03:00
|
|
|
private:
|
2021-07-10 19:23:16 +03: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 13:12:23 +03:00
|
|
|
const char* m_name { nullptr };
|
2021-07-10 19:23:16 +03:00
|
|
|
Mode m_mode { Mode::Unlocked };
|
2020-04-18 12:21:00 +03: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 03:09:14 +03:00
|
|
|
RefPtr<Thread> m_holder;
|
2020-12-15 02:36:22 +03:00
|
|
|
HashMap<Thread*, u32> m_shared_holders;
|
2021-07-10 19:23:16 +03:00
|
|
|
|
|
|
|
BlockedThreadList m_blocked_threads_list_exclusive;
|
|
|
|
BlockedThreadList m_blocked_threads_list_shared;
|
|
|
|
|
2021-07-16 01:10:52 +03:00
|
|
|
mutable SpinLock<u8> m_lock;
|
2018-10-24 00:32:53 +03:00
|
|
|
};
|
|
|
|
|
2021-07-18 02:13:34 +03:00
|
|
|
class MutexLocker {
|
2021-07-19 20:32:55 +03:00
|
|
|
AK_MAKE_NONCOPYABLE(MutexLocker);
|
|
|
|
|
2018-10-24 00:32:53 +03:00
|
|
|
public:
|
2021-07-19 20:32:55 +03:00
|
|
|
ALWAYS_INLINE explicit MutexLocker()
|
|
|
|
: m_lock(nullptr)
|
|
|
|
, m_locked(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-24 01:29:11 +03:00
|
|
|
#if LOCK_DEBUG
|
2021-07-18 02:13:34 +03:00
|
|
|
ALWAYS_INLINE explicit MutexLocker(Mutex& l, Mutex::Mode mode = Mutex::Mode::Exclusive, const SourceLocation& location = SourceLocation::current())
|
2021-04-25 01:17:02 +03:00
|
|
|
#else
|
2021-07-18 02:13:34 +03:00
|
|
|
ALWAYS_INLINE explicit MutexLocker(Mutex& l, Mutex::Mode mode = Mutex::Mode::Exclusive)
|
2021-04-25 01:17:02 +03:00
|
|
|
#endif
|
2021-07-19 20:32:55 +03:00
|
|
|
: m_lock(&l)
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
2021-04-25 01:17:02 +03:00
|
|
|
#if LOCK_DEBUG
|
2021-07-19 20:32:55 +03:00
|
|
|
m_lock->lock(mode, location);
|
2021-04-25 01:17:02 +03:00
|
|
|
#else
|
2021-07-19 20:32:55 +03:00
|
|
|
m_lock->lock(mode);
|
2021-04-25 01:17:02 +03:00
|
|
|
#endif
|
2019-05-28 12:53:16 +03:00
|
|
|
}
|
2021-04-25 01:17:02 +03:00
|
|
|
|
2021-07-18 02:13:34 +03:00
|
|
|
ALWAYS_INLINE ~MutexLocker()
|
2021-01-15 05:14:10 +03:00
|
|
|
{
|
|
|
|
if (m_locked)
|
|
|
|
unlock();
|
|
|
|
}
|
2021-07-19 20:32:55 +03:00
|
|
|
|
2021-01-15 05:14:10 +03:00
|
|
|
ALWAYS_INLINE void unlock()
|
|
|
|
{
|
2021-07-19 20:32:55 +03:00
|
|
|
VERIFY(m_lock);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_locked);
|
2021-01-15 05:14:10 +03:00
|
|
|
m_locked = false;
|
2021-07-19 20:32:55 +03:00
|
|
|
m_lock->unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
#if LOCK_DEBUG
|
|
|
|
ALWAYS_INLINE void attach_and_lock(Mutex& lock, Mutex::Mode mode = Mutex::Mode::Exclusive, const SourceLocation& location = SourceLocation::current())
|
|
|
|
#else
|
|
|
|
ALWAYS_INLINE void attach_and_lock(Mutex& lock, Mutex::Mode mode = Mutex::Mode::Exclusive)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
VERIFY(!m_locked);
|
|
|
|
m_lock = &lock;
|
|
|
|
m_locked = true;
|
|
|
|
|
|
|
|
#if LOCK_DEBUG
|
|
|
|
m_lock->lock(mode, location);
|
|
|
|
#else
|
|
|
|
m_lock->lock(mode);
|
|
|
|
#endif
|
2021-01-15 05:14:10 +03:00
|
|
|
}
|
2021-04-25 01:17:02 +03:00
|
|
|
|
|
|
|
#if LOCK_DEBUG
|
2021-07-17 22:09:51 +03:00
|
|
|
ALWAYS_INLINE void lock(Mutex::Mode mode = Mutex::Mode::Exclusive, const SourceLocation& location = SourceLocation::current())
|
2021-04-25 01:17:02 +03:00
|
|
|
#else
|
2021-07-17 22:09:51 +03:00
|
|
|
ALWAYS_INLINE void lock(Mutex::Mode mode = Mutex::Mode::Exclusive)
|
2021-04-25 01:17:02 +03:00
|
|
|
#endif
|
2021-01-15 05:14:10 +03:00
|
|
|
{
|
2021-07-19 20:32:55 +03:00
|
|
|
VERIFY(m_lock);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_locked);
|
2021-01-15 05:14:10 +03:00
|
|
|
m_locked = true;
|
2021-04-25 01:17:02 +03:00
|
|
|
|
|
|
|
#if LOCK_DEBUG
|
2021-07-19 20:32:55 +03:00
|
|
|
m_lock->lock(mode, location);
|
2021-04-25 01:17:02 +03:00
|
|
|
#else
|
2021-07-19 20:32:55 +03:00
|
|
|
m_lock->lock(mode);
|
2021-04-25 01:17:02 +03:00
|
|
|
#endif
|
2021-01-15 05:14:10 +03:00
|
|
|
}
|
2018-10-24 00:32:53 +03:00
|
|
|
|
|
|
|
private:
|
2021-07-19 20:32:55 +03:00
|
|
|
Mutex* m_lock;
|
2021-01-15 05:14:10 +03:00
|
|
|
bool m_locked { true };
|
2018-10-24 00:32:53 +03:00
|
|
|
};
|
|
|
|
|
2019-02-08 11:46:13 +03:00
|
|
|
template<typename T>
|
|
|
|
class Lockable {
|
|
|
|
public:
|
2021-02-28 16:42:08 +03:00
|
|
|
Lockable() = default;
|
2019-05-28 12:53:16 +03:00
|
|
|
Lockable(T&& resource)
|
|
|
|
: m_resource(move(resource))
|
|
|
|
{
|
|
|
|
}
|
2021-07-17 22:09:51 +03:00
|
|
|
[[nodiscard]] Mutex& lock() { return m_lock; }
|
2021-02-15 02:24:44 +03:00
|
|
|
[[nodiscard]] T& resource() { return m_resource; }
|
2019-02-08 11:46:13 +03:00
|
|
|
|
2021-02-15 02:24:44 +03:00
|
|
|
[[nodiscard]] T lock_and_copy()
|
2019-02-08 18:18:24 +03:00
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_lock);
|
2019-02-08 18:18:24 +03:00
|
|
|
return m_resource;
|
|
|
|
}
|
|
|
|
|
2019-02-08 11:46:13 +03:00
|
|
|
private:
|
|
|
|
T m_resource;
|
2021-07-17 22:09:51 +03:00
|
|
|
Mutex m_lock;
|
2019-02-08 11:46:13 +03:00
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-02-06 21:33:31 +03:00
|
|
|
class ScopedLockRelease {
|
|
|
|
AK_MAKE_NONCOPYABLE(ScopedLockRelease);
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedLockRelease& operator=(ScopedLockRelease&&) = delete;
|
|
|
|
|
2021-07-17 22:09:51 +03:00
|
|
|
ScopedLockRelease(Mutex& lock)
|
2021-02-06 21:33:31 +03: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 22:09:51 +03:00
|
|
|
, m_previous_mode(exchange(from.m_previous_mode, Mutex::Mode::Unlocked))
|
2021-02-06 21:33:31 +03:00
|
|
|
, m_previous_recursions(exchange(from.m_previous_recursions, 0))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScopedLockRelease()
|
|
|
|
{
|
2021-07-17 22:09:51 +03:00
|
|
|
if (m_lock && m_previous_mode != Mutex::Mode::Unlocked)
|
2021-02-06 21:33:31 +03:00
|
|
|
m_lock->restore_lock(m_previous_mode, m_previous_recursions);
|
|
|
|
}
|
|
|
|
|
|
|
|
void restore_lock()
|
|
|
|
{
|
|
|
|
VERIFY(m_lock);
|
2021-07-17 22:09:51 +03:00
|
|
|
if (m_previous_mode != Mutex::Mode::Unlocked) {
|
2021-02-06 21:33:31 +03:00
|
|
|
m_lock->restore_lock(m_previous_mode, m_previous_recursions);
|
2021-07-17 22:09:51 +03:00
|
|
|
m_previous_mode = Mutex::Mode::Unlocked;
|
2021-02-06 21:33:31 +03:00
|
|
|
m_previous_recursions = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_not_restore()
|
|
|
|
{
|
|
|
|
VERIFY(m_lock);
|
2021-07-17 22:09:51 +03:00
|
|
|
m_previous_mode = Mutex::Mode::Unlocked;
|
2021-02-06 21:33:31 +03:00
|
|
|
m_previous_recursions = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-17 22:09:51 +03:00
|
|
|
Mutex* m_lock;
|
|
|
|
Mutex::Mode m_previous_mode;
|
2021-02-06 21:33:31 +03:00
|
|
|
u32 m_previous_recursions;
|
|
|
|
};
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|