Botan 3.6.1
Crypto and TLS for C&
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
9namespace Botan {
10
11RWLock::RWLock() : m_state(0) {}
12
14 std::unique_lock<std::mutex> lock(m_mutex);
15 while(m_state & is_writing) {
16 m_gate1.wait(lock);
17 }
18 m_state |= is_writing;
19 while(m_state & readers_mask) {
20 m_gate2.wait(lock);
21 }
22}
23
25 std::unique_lock<std::mutex> lock(m_mutex);
26 m_state = 0;
27 m_gate1.notify_all();
28}
29
31 std::unique_lock<std::mutex> lock(m_mutex);
32 while((m_state & is_writing) || (m_state & readers_mask) == readers_mask) {
33 m_gate1.wait(lock);
34 }
35 const uint32_t num_readers = (m_state & readers_mask) + 1;
36 m_state &= ~readers_mask;
37 m_state |= num_readers;
38}
39
41 std::unique_lock<std::mutex> lock(m_mutex);
42 const uint32_t num_readers = (m_state & readers_mask) - 1;
43 m_state &= ~readers_mask;
44 m_state |= num_readers;
45 if(m_state & is_writing) {
46 if(num_readers == 0) {
47 m_gate2.notify_one();
48 }
49 } else {
50 if(num_readers == readers_mask - 1) {
51 m_gate1.notify_one();
52 }
53 }
54}
55
56} // namespace Botan
void lock()
Definition rwlock.cpp:13
void lock_shared()
Definition rwlock.cpp:30
void unlock()
Definition rwlock.cpp:24
void unlock_shared()
Definition rwlock.cpp:40