aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/barrier.cpp
diff options
context:
space:
mode:
authorJoel Low <[email protected]>2016-11-05 22:11:55 +0800
committerJoel Low <[email protected]>2016-11-06 12:45:14 +0800
commit54a951b6c67922b108488465bc062eda42877fd7 (patch)
treef8eaa44d52cba907698f3c34462f6d6245a8bc5b /src/lib/utils/barrier.cpp
parentc4da8e6545adb36a4c398fc3f872565e420e4aad (diff)
Implement barriers for Threaded Fork
This commit introduces a concept of a barrier, where all threads must synchronise before continuing. Threaded Fork uses this to ensure that all input is consumed by each sink exactly once. Fixes #695.
Diffstat (limited to 'src/lib/utils/barrier.cpp')
-rw-r--r--src/lib/utils/barrier.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/lib/utils/barrier.cpp b/src/lib/utils/barrier.cpp
new file mode 100644
index 000000000..eb2ab978c
--- /dev/null
+++ b/src/lib/utils/barrier.cpp
@@ -0,0 +1,35 @@
+/*
+* Barrier
+* (C) 2016 Joel Low
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#include <botan/internal/barrier.h>
+
+#if defined(BOTAN_TARGET_OS_HAS_THREADS)
+
+namespace Botan {
+
+void Barrier::wait(unsigned delta)
+ {
+ lock_guard_type<mutex_type> lock(m_mutex);
+ m_value += delta;
+ }
+
+void Barrier::sync()
+ {
+ std::unique_lock<mutex_type> lock(m_mutex);
+ --m_value;
+ if(m_value > 0)
+ m_cond.wait(lock, [this] { return m_value <= 0; });
+ else
+ {
+ m_value = 0;
+ m_cond.notify_all();
+ }
+ }
+
+}
+
+#endif