ladybird/Userland/Libraries/LibThreading/Mutex.h
Andreas Kling cc4b3cbacc
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-14, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run
Meta: Update my e-mail address everywhere
2024-10-04 13:19:50 +02:00

82 lines
1.7 KiB
C++

/*
* Copyright (c) 2018-2021, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Assertions.h>
#include <AK/Noncopyable.h>
#include <AK/Types.h>
#include <pthread.h>
namespace Threading {
class Mutex {
AK_MAKE_NONCOPYABLE(Mutex);
AK_MAKE_NONMOVABLE(Mutex);
friend class ConditionVariable;
public:
Mutex()
: m_lock_count(0)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&m_mutex, &attr);
}
~Mutex()
{
VERIFY(m_lock_count == 0);
// FIXME: pthread_mutex_destroy() is not implemented.
}
void lock();
void unlock();
private:
pthread_mutex_t m_mutex;
unsigned m_lock_count { 0 };
};
class MutexLocker {
AK_MAKE_NONCOPYABLE(MutexLocker);
AK_MAKE_NONMOVABLE(MutexLocker);
public:
ALWAYS_INLINE explicit MutexLocker(Mutex& mutex)
: m_mutex(mutex)
{
lock();
}
ALWAYS_INLINE ~MutexLocker()
{
unlock();
}
ALWAYS_INLINE void unlock() { m_mutex.unlock(); }
ALWAYS_INLINE void lock() { m_mutex.lock(); }
private:
Mutex& m_mutex;
};
ALWAYS_INLINE void Mutex::lock()
{
pthread_mutex_lock(&m_mutex);
m_lock_count++;
}
ALWAYS_INLINE void Mutex::unlock()
{
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--;
pthread_mutex_unlock(&m_mutex);
}
}