Files
ladybird/Libraries/LibSync/ConditionVariablePOSIX.cpp
R-Goc 50be9493d2 LibSync: Abstract away Mutex implementation
This commit adds in-place pimpl to abstract away the implementation of
Mutex. It also adds policies to configure the type of mutex desired.

Because of the tight integration between mutex and condition variables
they also needed to be reworked and the changes have to be in one commit
to retain atomicity.

A win32 and pthread implemenation is provided to make sure the api
works with both.
2026-05-08 18:58:35 -05:00

69 lines
1.9 KiB
C++

/*
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>.
* Copyright (c) 2025, Ryszard Goc <ryszardgoc@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibSync/ConditionVariable.h>
#include <LibSync/Export.h>
#include <LibSync/Mutex.h>
#include <pthread.h>
namespace Sync {
namespace {
ALWAYS_INLINE pthread_cond_t* to_impl(void* ptr)
{
return reinterpret_cast<pthread_cond_t*>(ptr);
}
}
template<typename MutexType>
requires Detail::IsIntraprocess<MutexType> && Detail::IsNonRecursive<MutexType>
ConditionVariableBase<MutexType>::ConditionVariableBase(MutexType& to_wait_on)
: m_to_wait_on(to_wait_on)
{
static_assert(sizeof(m_storage) == sizeof(pthread_cond_t));
int result = pthread_cond_init(to_impl(m_storage), nullptr);
VERIFY(result == 0);
}
template<typename MutexType>
requires Detail::IsIntraprocess<MutexType> && Detail::IsNonRecursive<MutexType>
ConditionVariableBase<MutexType>::~ConditionVariableBase()
{
int result = pthread_cond_destroy(to_impl(m_storage));
VERIFY(result == 0);
}
template<typename MutexType>
requires Detail::IsIntraprocess<MutexType> && Detail::IsNonRecursive<MutexType>
void ConditionVariableBase<MutexType>::wait()
{
int result = pthread_cond_wait(to_impl(m_storage), reinterpret_cast<pthread_mutex_t*>(m_to_wait_on.m_storage));
VERIFY(result == 0);
}
template<typename MutexType>
requires Detail::IsIntraprocess<MutexType> && Detail::IsNonRecursive<MutexType>
void ConditionVariableBase<MutexType>::signal()
{
int result = pthread_cond_signal(to_impl(m_storage));
VERIFY(result == 0);
}
template<typename MutexType>
requires Detail::IsIntraprocess<MutexType> && Detail::IsNonRecursive<MutexType>
void ConditionVariableBase<MutexType>::broadcast()
{
int result = pthread_cond_broadcast(to_impl(m_storage));
VERIFY(result == 0);
}
template class SYNC_API ConditionVariableBase<Mutex>;
}