2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <andreas@ladybird.org>
|
2022-01-02 13:49:53 +00:00
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
|
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
|
|
|
*/
|
|
|
|
|
2019-03-25 12:03:49 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-04-28 10:26:36 +00:00
|
|
|
#include <AK/Assertions.h>
|
2021-09-30 21:24:42 +00:00
|
|
|
#include <AK/Noncopyable.h>
|
2021-04-28 10:26:36 +00:00
|
|
|
#include <AK/Types.h>
|
2021-07-05 20:56:54 +00:00
|
|
|
#include <pthread.h>
|
2019-03-25 12:03:49 +00:00
|
|
|
|
2021-05-22 16:47:42 +00:00
|
|
|
namespace Threading {
|
2019-08-25 20:51:27 +00:00
|
|
|
|
2021-07-09 09:14:57 +00:00
|
|
|
class Mutex {
|
2021-09-16 07:19:27 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(Mutex);
|
|
|
|
AK_MAKE_NONMOVABLE(Mutex);
|
2021-08-29 00:03:50 +00:00
|
|
|
friend class ConditionVariable;
|
|
|
|
|
2019-03-25 12:03:49 +00:00
|
|
|
public:
|
2021-07-09 09:14:57 +00:00
|
|
|
Mutex()
|
2022-01-02 13:49:53 +00:00
|
|
|
: m_lock_count(0)
|
2021-07-05 22:14:19 +00:00
|
|
|
{
|
|
|
|
pthread_mutexattr_t attr;
|
|
|
|
pthread_mutexattr_init(&attr);
|
|
|
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
|
|
|
pthread_mutex_init(&m_mutex, &attr);
|
2021-07-05 22:06:32 +00:00
|
|
|
}
|
2022-01-02 13:49:53 +00:00
|
|
|
~Mutex()
|
|
|
|
{
|
|
|
|
VERIFY(m_lock_count == 0);
|
|
|
|
// FIXME: pthread_mutex_destroy() is not implemented.
|
|
|
|
}
|
2019-03-25 12:03:49 +00:00
|
|
|
|
|
|
|
void lock();
|
|
|
|
void unlock();
|
|
|
|
|
|
|
|
private:
|
2021-07-05 22:06:32 +00:00
|
|
|
pthread_mutex_t m_mutex;
|
2022-01-02 13:49:53 +00:00
|
|
|
unsigned m_lock_count { 0 };
|
2019-03-25 12:03:49 +00:00
|
|
|
};
|
|
|
|
|
2021-07-09 09:14:57 +00:00
|
|
|
class MutexLocker {
|
2021-09-16 07:19:27 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(MutexLocker);
|
|
|
|
AK_MAKE_NONMOVABLE(MutexLocker);
|
|
|
|
|
2019-03-25 12:03:49 +00:00
|
|
|
public:
|
2021-07-09 09:14:57 +00:00
|
|
|
ALWAYS_INLINE explicit MutexLocker(Mutex& mutex)
|
|
|
|
: m_mutex(mutex)
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
lock();
|
|
|
|
}
|
2022-01-02 13:49:53 +00:00
|
|
|
ALWAYS_INLINE ~MutexLocker()
|
|
|
|
{
|
|
|
|
unlock();
|
|
|
|
}
|
2021-07-09 09:14:57 +00:00
|
|
|
ALWAYS_INLINE void unlock() { m_mutex.unlock(); }
|
|
|
|
ALWAYS_INLINE void lock() { m_mutex.lock(); }
|
2019-03-25 12:03:49 +00:00
|
|
|
|
|
|
|
private:
|
2021-07-09 09:14:57 +00:00
|
|
|
Mutex& m_mutex;
|
2019-03-25 12:03:49 +00:00
|
|
|
};
|
|
|
|
|
2021-07-09 09:14:57 +00:00
|
|
|
ALWAYS_INLINE void Mutex::lock()
|
2019-03-25 12:03:49 +00:00
|
|
|
{
|
2021-07-05 20:56:54 +00:00
|
|
|
pthread_mutex_lock(&m_mutex);
|
2022-01-02 13:49:53 +00:00
|
|
|
m_lock_count++;
|
2019-03-25 12:03:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 09:14:57 +00:00
|
|
|
ALWAYS_INLINE void Mutex::unlock()
|
2019-03-25 12:03:49 +00:00
|
|
|
{
|
2022-01-02 13:49:53 +00:00
|
|
|
VERIFY(m_lock_count > 0);
|
|
|
|
// FIXME: We need to protect the lock count with the mutex itself.
|
|
|
|
// This may be bad because we're not *technically* unlocked yet,
|
|
|
|
// but we're not handling any errors from pthread_mutex_unlock anyways.
|
|
|
|
m_lock_count--;
|
2021-07-05 20:56:54 +00:00
|
|
|
pthread_mutex_unlock(&m_mutex);
|
2019-03-25 12:03:49 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 20:51:27 +00:00
|
|
|
}
|