Botan 3.4.0
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::RWLock Class Referencefinal

#include <rwlock.h>

Public Member Functions

void lock ()
 
void lock_shared ()
 
 RWLock ()
 
void unlock ()
 
void unlock_shared ()
 

Detailed Description

A read-write lock. Writers are favored.

Definition at line 19 of file rwlock.h.

Constructor & Destructor Documentation

◆ RWLock()

Botan::RWLock::RWLock ( )

Definition at line 11 of file rwlock.cpp.

11: m_state(0) {}

Member Function Documentation

◆ lock()

void Botan::RWLock::lock ( )

Definition at line 13 of file rwlock.cpp.

13 {
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}
void lock()
Definition rwlock.cpp:13

References lock().

Referenced by lock(), lock_shared(), unlock(), and unlock_shared().

◆ lock_shared()

void Botan::RWLock::lock_shared ( )

Definition at line 30 of file rwlock.cpp.

30 {
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}

References lock().

◆ unlock()

void Botan::RWLock::unlock ( )

Definition at line 24 of file rwlock.cpp.

24 {
25 std::unique_lock<std::mutex> lock(m_mutex);
26 m_state = 0;
27 m_gate1.notify_all();
28}

References lock().

◆ unlock_shared()

void Botan::RWLock::unlock_shared ( )

Definition at line 40 of file rwlock.cpp.

40 {
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}

References lock().


The documentation for this class was generated from the following files: