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