summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibThreading/ConditionVariable.h
diff options
context:
space:
mode:
authorkleines Filmröllchen <malu.bertsch@gmail.com>2021-08-29 02:03:50 +0200
committerAndreas Kling <kling@serenityos.org>2021-09-12 23:38:57 +0200
commitc7104e7512608cb208b80e8ec9d3368919406653 (patch)
treee50fbd4dafc88cc9b1b3d36ad55a15d821e1b83c /Userland/Libraries/LibThreading/ConditionVariable.h
parentbd17da9f9e9227c37aa1b0da48311db2819f723d (diff)
downloadserenity-c7104e7512608cb208b80e8ec9d3368919406653.zip
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.
Diffstat (limited to 'Userland/Libraries/LibThreading/ConditionVariable.h')
-rw-r--r--Userland/Libraries/LibThreading/ConditionVariable.h64
1 files changed, 64 insertions, 0 deletions
diff --git a/Userland/Libraries/LibThreading/ConditionVariable.h b/Userland/Libraries/LibThreading/ConditionVariable.h
new file mode 100644
index 0000000000..5d7951be54
--- /dev/null
+++ b/Userland/Libraries/LibThreading/ConditionVariable.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Function.h>
+#include <LibThreading/Mutex.h>
+#include <pthread.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+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<bool()> 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;
+};
+
+}