diff options
author | Andreas Kling <kling@serenityos.org> | 2021-07-09 11:14:57 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-07-09 11:15:50 +0200 |
commit | b8a204c5b9c1bc132a2995f751121b3cc93bddb7 (patch) | |
tree | d19361ca8a0e4cef487f0710d83d3410b9d9dff1 /Userland/Libraries/LibThreading/Mutex.h | |
parent | ce5d2b4f16aec72614ca4abec0dff7b6c757d2ac (diff) | |
download | serenity-b8a204c5b9c1bc132a2995f751121b3cc93bddb7.zip |
LibThreading: Rename Lock => Mutex
Diffstat (limited to 'Userland/Libraries/LibThreading/Mutex.h')
-rw-r--r-- | Userland/Libraries/LibThreading/Mutex.h | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/Userland/Libraries/LibThreading/Mutex.h b/Userland/Libraries/LibThreading/Mutex.h new file mode 100644 index 0000000000..77700b1cf1 --- /dev/null +++ b/Userland/Libraries/LibThreading/Mutex.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/Assertions.h> +#include <AK/Types.h> +#include <pthread.h> + +namespace Threading { + +class Mutex { +public: + Mutex() + { +#ifndef __serenity__ + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&m_mutex, &attr); +#endif + } + ~Mutex() { } + + void lock(); + void unlock(); + +private: +#ifdef __serenity__ + pthread_mutex_t m_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; +#else + pthread_mutex_t m_mutex; +#endif +}; + +class 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); +} + +ALWAYS_INLINE void Mutex::unlock() +{ + pthread_mutex_unlock(&m_mutex); +} + +} |