Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

rwlock.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2019 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6
7 #include <botan/internal/rwlock.h>
8
9 namespace Botan {
10
11 RWLock::RWLock() : m_state(0) {}
12
13 void RWLock::lock()
14 {
15 std::unique_lock<std::mutex> lock(m_mutex);
16 while(m_state & is_writing)
17 m_gate1.wait(lock);
18 m_state |= is_writing;
19 while(m_state & readers_mask)
20 m_gate2.wait(lock);
21 }
22
23 void RWLock::unlock()
24 {
25 std::unique_lock<std::mutex> lock(m_mutex);
26 m_state = 0;
27 m_gate1.notify_all();
28 }
29
30 void RWLock::lock_shared()
31 {
32 std::unique_lock<std::mutex> lock(m_mutex);
33 while((m_state & is_writing) || (m_state & readers_mask) == readers_mask)
34 m_gate1.wait(lock);
35 const uint32_t num_readers = (m_state & readers_mask) + 1;
36 m_state &= ~readers_mask;
37 m_state |= num_readers;
38 }
39
40 void RWLock::unlock_shared()
41 {
42 std::unique_lock<std::mutex> lock(m_mutex);
43 const uint32_t num_readers = (m_state & readers_mask) - 1;
44 m_state &= ~readers_mask;
45 m_state |= num_readers;
46 if(m_state & is_writing)
47 {
48 if(num_readers == 0)
49 m_gate2.notify_one();
50 }
51 else
52 {
53 if(num_readers == readers_mask - 1)
54 m_gate1.notify_one();
55 }
56 }
57
58 }
Botan::RWLock::RWLock
RWLock()
Definition: rwlock.cpp:11
Botan::RWLock::unlock_shared
void unlock_shared()
Definition: rwlock.cpp:40
Botan::RWLock::unlock
void unlock()
Definition: rwlock.cpp:23
Botan::RWLock::lock
void lock()
Definition: rwlock.cpp:13
Botan
Definition: alg_id.cpp:13
Botan::RWLock::lock_shared
void lock_shared()
Definition: rwlock.cpp:30