diff options
author | Jack Lloyd <[email protected]> | 2016-11-06 11:57:01 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-11-06 11:57:01 -0500 |
commit | fac0ef22da62ee757d23e3fcfabcac699ec40668 (patch) | |
tree | 24162fde15a7150f9ed13d3dd9e47b5c88215866 /src/lib/utils/barrier.cpp | |
parent | c4da8e6545adb36a4c398fc3f872565e420e4aad (diff) | |
parent | 6a00ecd2c027c884da509bce22b1d8a72c3be928 (diff) |
Merge GH #716 Add Barrier type, correct Threaded_Fork
Diffstat (limited to 'src/lib/utils/barrier.cpp')
-rw-r--r-- | src/lib/utils/barrier.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/lib/utils/barrier.cpp b/src/lib/utils/barrier.cpp new file mode 100644 index 000000000..81c578b72 --- /dev/null +++ b/src/lib/utils/barrier.cpp @@ -0,0 +1,39 @@ +/* +* 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) + { + unsigned current_syncs = m_syncs; + m_cond.wait(lock, [this, ¤t_syncs] { return m_syncs != current_syncs; }); + } + else + { + m_value = 0; + ++m_syncs; + m_cond.notify_all(); + } + } + +} + +#endif |