aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/thread_utils/semaphore.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-01-12 11:21:09 -0500
committerJack Lloyd <[email protected]>2018-01-12 12:10:34 -0500
commite5c7809ae0734715f4c1ab5fe476e0b04c4b2a59 (patch)
tree3a236b0356d05869b43add9ae8b1bf25e1fbe8d4 /src/lib/utils/thread_utils/semaphore.cpp
parent3099e920495c8e881387363de8e1b0bf7d1d5292 (diff)
Move thread utils (barrier and semaphore) to a subpackage of util
They are not needed except by the filter code so being able to easily remove them from the build is nice; utils is always compiled in so that should be as small as possible.
Diffstat (limited to 'src/lib/utils/thread_utils/semaphore.cpp')
-rw-r--r--src/lib/utils/thread_utils/semaphore.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/lib/utils/thread_utils/semaphore.cpp b/src/lib/utils/thread_utils/semaphore.cpp
new file mode 100644
index 000000000..9a7af188a
--- /dev/null
+++ b/src/lib/utils/thread_utils/semaphore.cpp
@@ -0,0 +1,38 @@
+/*
+* Semaphore
+* (C) 2013 Joel Low
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#include <botan/internal/semaphore.h>
+
+// Based on code by Pierre Gaston (http://p9as.blogspot.com/2012/06/c11-semaphores.html)
+
+namespace Botan {
+
+void Semaphore::release(size_t n)
+ {
+ for(size_t i = 0; i != n; ++i)
+ {
+ lock_guard_type<mutex_type> lock(m_mutex);
+
+ if(m_value++ < 0)
+ {
+ ++m_wakeups;
+ m_cond.notify_one();
+ }
+ }
+ }
+
+void Semaphore::acquire()
+ {
+ std::unique_lock<mutex_type> lock(m_mutex);
+ if(m_value-- <= 0)
+ {
+ m_cond.wait(lock, [this] { return m_wakeups > 0; });
+ --m_wakeups;
+ }
+ }
+
+}