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>
|
2021-08-07 14:19:39 +03:00
|
|
|
#include <Kernel/Locking/LockLocation.h>
|
2021-07-18 10:09:04 +03:00
|
|
|
#include <Kernel/Locking/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-08-29 03:07:57 +03:00
|
|
|
Mutex(StringView name = {})
|
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-08-07 14:58:07 +03:00
|
|
|
void lock(Mode mode = Mode::Exclusive, LockLocation const& location = LockLocation::current());
|
|
|
|
void restore_lock(Mode, u32, LockLocation const& location = LockLocation::current());
|
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
|
|
|
|
{
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-07-16 01:10:52 +03:00
|
|
|
return m_mode != Mode::Unlocked;
|
|
|
|
}
|
2021-08-29 13:48:43 +03:00
|
|
|
[[nodiscard]] bool is_locked_by_current_thread() const
|
2021-07-16 01:10:52 +03:00
|
|
|
{
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(m_lock);
|
2021-07-16 01:10:52 +03:00
|
|
|
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-08-29 03:07:57 +03:00
|
|
|
[[nodiscard]] StringView name() const { return m_name; }
|
2019-02-07 13:12:23 +03:00
|
|
|
|
2021-08-29 03:07:57 +03:00
|
|
|
static StringView mode_to_string(Mode mode)
|
2020-12-15 02:36:22 +03:00
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case Mode::Unlocked:
|
2021-08-29 03:07:57 +03:00
|
|
|
return "unlocked"sv;
|
2020-12-15 02:36:22 +03:00
|
|
|
case Mode::Exclusive:
|
2021-08-29 03:07:57 +03:00
|
|
|
return "exclusive"sv;
|
2020-12-15 02:36:22 +03:00
|
|
|
case Mode::Shared:
|
2021-08-29 03:07:57 +03:00
|
|
|
return "shared"sv;
|
2020-12-15 02:36:22 +03:00
|
|
|
default:
|
2021-08-29 03:07:57 +03:00
|
|
|
return "invalid"sv;
|
2020-12-15 02:36:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 00:32:53 +03:00
|
|
|
private:
|
2021-09-09 15:00:59 +03:00
|
|
|
using BlockedThreadList = IntrusiveList<&Thread::m_blocked_threads_list_node>;
|
2021-07-10 19:23:16 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-05 20:02:03 +03:00
|
|
|
void block(Thread&, Mode, SpinlockLocker<Spinlock>&, u32);
|
2021-07-10 19:23:16 +03:00
|
|
|
void unblock_waiters(Mode);
|
|
|
|
|
2021-08-29 03:07:57 +03:00
|
|
|
StringView m_name;
|
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-09-05 20:02:03 +03:00
|
|
|
mutable Spinlock 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-08-07 14:58:07 +03:00
|
|
|
ALWAYS_INLINE explicit MutexLocker(Mutex& l, Mutex::Mode mode = Mutex::Mode::Exclusive, LockLocation const& location = LockLocation::current())
|
2021-07-19 20:32:55 +03:00
|
|
|
: m_lock(&l)
|
2019-05-28 12:53:16 +03:00
|
|
|
{
|
2021-07-19 20:32:55 +03:00
|
|
|
m_lock->lock(mode, location);
|
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();
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:58:07 +03:00
|
|
|
ALWAYS_INLINE void attach_and_lock(Mutex& lock, Mutex::Mode mode = Mutex::Mode::Exclusive, LockLocation const& location = LockLocation::current())
|
2021-07-19 20:32:55 +03:00
|
|
|
{
|
|
|
|
VERIFY(!m_locked);
|
|
|
|
m_lock = &lock;
|
|
|
|
m_locked = true;
|
|
|
|
|
|
|
|
m_lock->lock(mode, location);
|
2021-01-15 05:14:10 +03:00
|
|
|
}
|
2021-04-25 01:17:02 +03:00
|
|
|
|
2021-08-07 14:58:07 +03:00
|
|
|
ALWAYS_INLINE void lock(Mutex::Mode mode = Mutex::Mode::Exclusive, LockLocation const& location = LockLocation::current())
|
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
|
|
|
|
2021-07-19 20:32:55 +03:00
|
|
|
m_lock->lock(mode, location);
|
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
|
|
|
};
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|