From c7104e7512608cb208b80e8ec9d3368919406653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Sun, 29 Aug 2021 02:03:50 +0200 Subject: [PATCH] LibThreading: Add ConditionVariable wrapper ConditionVariable is a thin wrapper over the pthread_cond_* APIs, just as Mutex is a wrapper over pthread_mutex. Because ConditionVariable might want to wait on a high-level Mutex, it needs to be friends with it. --- .../LibThreading/ConditionVariable.h | 64 +++++++++++++++++++ Userland/Libraries/LibThreading/Mutex.h | 2 + 2 files changed, 66 insertions(+) create mode 100644 Userland/Libraries/LibThreading/ConditionVariable.h diff --git a/Userland/Libraries/LibThreading/ConditionVariable.h b/Userland/Libraries/LibThreading/ConditionVariable.h new file mode 100644 index 00000000000..5d7951be54b --- /dev/null +++ b/Userland/Libraries/LibThreading/ConditionVariable.h @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2021, kleines Filmröllchen . + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include +#include + +namespace Threading { + +// A signaling condition variable that wraps over the pthread_cond_* APIs. +class ConditionVariable { + friend class Mutex; + +public: + ConditionVariable(Mutex& to_wait_on) + : m_to_wait_on(to_wait_on) + { + auto result = pthread_cond_init(&m_condition, nullptr); + VERIFY(result == 0); + } + + ALWAYS_INLINE ~ConditionVariable() + { + auto result = pthread_cond_destroy(&m_condition); + VERIFY(result == 0); + } + + // As with pthread APIs, the mutex must be locked or undefined behavior ensues. + ALWAYS_INLINE void wait() + { + auto result = pthread_cond_wait(&m_condition, &m_to_wait_on.m_mutex); + VERIFY(result == 0); + } + ALWAYS_INLINE void wait_while(Function condition) + { + while (condition()) + wait(); + } + // Release at least one of the threads waiting on this variable. + ALWAYS_INLINE void signal() + { + auto result = pthread_cond_signal(&m_condition); + VERIFY(result == 0); + } + // Release all of the threads waiting on this variable. + ALWAYS_INLINE void broadcast() + { + auto result = pthread_cond_broadcast(&m_condition); + VERIFY(result == 0); + } + +private: + pthread_cond_t m_condition; + Mutex& m_to_wait_on; +}; + +} diff --git a/Userland/Libraries/LibThreading/Mutex.h b/Userland/Libraries/LibThreading/Mutex.h index 77700b1cf1d..9bd961545f1 100644 --- a/Userland/Libraries/LibThreading/Mutex.h +++ b/Userland/Libraries/LibThreading/Mutex.h @@ -13,6 +13,8 @@ namespace Threading { class Mutex { + friend class ConditionVariable; + public: Mutex() {