diff --git a/Userland/Libraries/LibThreading/Lock.h b/Userland/Libraries/LibThreading/Lock.h index 9cc4bf9b2a..a0613c1800 100644 --- a/Userland/Libraries/LibThreading/Lock.h +++ b/Userland/Libraries/LibThreading/Lock.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,14 +7,8 @@ #pragma once #include -#include #include - -#ifdef __serenity__ -# include -#else -# include -#endif +#include namespace Threading { @@ -27,23 +21,7 @@ public: void unlock(); private: -#ifdef __serenity__ - using ThreadID = int; - - ALWAYS_INLINE static ThreadID self() - { - return gettid(); - } -#else - using ThreadID = pthread_t; - - ALWAYS_INLINE static ThreadID self() - { - return pthread_self(); - } -#endif - Atomic m_holder { 0 }; - u32 m_level { 0 }; + pthread_mutex_t m_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; }; class Locker { @@ -63,31 +41,12 @@ private: ALWAYS_INLINE void Lock::lock() { - ThreadID tid = self(); - if (m_holder == tid) { - ++m_level; - return; - } - for (;;) { - ThreadID expected = 0; - if (m_holder.compare_exchange_strong(expected, tid, AK::memory_order_acq_rel)) { - m_level = 1; - return; - } -#ifdef __serenity__ - donate(expected); -#endif - } + pthread_mutex_lock(&m_mutex); } inline void Lock::unlock() { - VERIFY(m_holder == self()); - VERIFY(m_level); - if (m_level == 1) - m_holder.store(0, AK::memory_order_release); - else - --m_level; + pthread_mutex_unlock(&m_mutex); } template