Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

semaphore.cpp
Go to the documentation of this file.
1 /*
2 * Semaphore
3 * (C) 2013 Joel Low
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/internal/semaphore.h>
9
10 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
11
12 // Based on code by Pierre Gaston (http://p9as.blogspot.com/2012/06/c11-semaphores.html)
13
14 namespace Botan {
15
16 void Semaphore::release(size_t n)
17 {
18 for(size_t i = 0; i != n; ++i)
19 {
20 lock_guard_type<mutex_type> lock(m_mutex);
21
22 ++m_value;
23
24 if(m_value <= 0)
25 {
26 ++m_wakeups;
27 m_cond.notify_one();
28 }
29 }
30 }
31
32 void Semaphore::acquire()
33 {
34 std::unique_lock<mutex_type> lock(m_mutex);
35 --m_value;
36 if(m_value < 0)
37 {
38 m_cond.wait(lock, [this] { return m_wakeups > 0; });
39 --m_wakeups;
40 }
41 }
42
43 }
44
45 #endif
Botan
Definition: alg_id.cpp:13