diff options
author | lloyd <[email protected]> | 2013-02-02 23:03:47 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2013-02-02 23:03:47 +0000 |
commit | 06fc6aa688dcb4d4b1d742c7978c020f94b82e5b (patch) | |
tree | 0ed8d2e6b68d7adbfdce4e2cd04b506cca5be7f8 /src/utils | |
parent | cc0765f2946f7aee146e4df370460a4d06fda3ae (diff) |
Add Threaded_Fork, which acts like a normal Fork filter except that
each subchain of filters will run in its own thread.
Written and contributed by Joel Low.
A thread on botan-devel contains the original patch and some
discussion:
http://lists.randombit.net/pipermail/botan-devel/2013-January/001699.html
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/info.txt | 5 | ||||
-rw-r--r-- | src/utils/semaphore.cpp | 39 | ||||
-rw-r--r-- | src/utils/semaphore.h | 34 |
3 files changed, 77 insertions, 1 deletions
diff --git a/src/utils/info.txt b/src/utils/info.txt index 5bf1b9a54..cc056fc69 100644 --- a/src/utils/info.txt +++ b/src/utils/info.txt @@ -8,6 +8,7 @@ calendar.cpp charset.cpp cpuid.cpp parsing.cpp +semaphore.cpp version.cpp zero_mem.cpp </source> @@ -17,6 +18,7 @@ assert.h bit_ops.h prefetch.h rounding.h +semaphore.h stl_util.h xor_buf.h </header:internal> @@ -27,13 +29,14 @@ calendar.h charset.h cpuid.h exceptn.h +get_byte.h loadstor.h mem_ops.h parsing.h rotate.h +semaphore.h types.h version.h -get_byte.h </header:public> <libs> diff --git a/src/utils/semaphore.cpp b/src/utils/semaphore.cpp new file mode 100644 index 000000000..f4f5b2b53 --- /dev/null +++ b/src/utils/semaphore.cpp @@ -0,0 +1,39 @@ +/* +* Semaphore +* by Pierre Gaston (http://p9as.blogspot.com/2012/06/c11-semaphores.html) +* modified by Joel Low for Botan +* +*/ + +#include <botan/internal/semaphore.h> + +namespace Botan { + +void Semaphore::release(size_t n) + { + for(size_t i = 0; i != n; ++i) + { + std::lock_guard<std::mutex> lock(m_mutex); + + ++m_value; + + if(m_value <= 0) + { + ++m_wakeups; + m_cond.notify_one(); + } + } + } + +void Semaphore::acquire() + { + std::unique_lock<std::mutex> lock(m_mutex); + --m_value; + if(m_value < 0) + { + m_cond.wait(lock, [this] { return m_wakeups > 0; }); + --m_wakeups; + } + } + +} diff --git a/src/utils/semaphore.h b/src/utils/semaphore.h new file mode 100644 index 000000000..c3ce73680 --- /dev/null +++ b/src/utils/semaphore.h @@ -0,0 +1,34 @@ +/* +* Semaphore +* by Pierre Gaston (http://p9as.blogspot.com/2012/06/c11-semaphores.html) +* modified by Joel Low for Botan +* +*/ + +#ifndef BOTAN_SEMAPHORE_H__ +#define BOTAN_SEMAPHORE_H__ + +#include <mutex> +#include <condition_variable> + +namespace Botan { + +class Semaphore + { + public: + Semaphore(int value = 0) : m_value(value), m_wakeups(0) {} + + void acquire(); + + void release(size_t n = 1); + + private: + int m_value; + int m_wakeups; + std::mutex m_mutex; + std::condition_variable m_cond; + }; + +} + +#endif |