AWS SDK for C++  1.8.181
AWS SDK for C++
ReaderWriterLock.h
Go to the documentation of this file.
1 
6 #pragma once
7 
10 #include <mutex>
11 #include <atomic>
12 #include <condition_variable>
13 #include <cstdint>
14 
15 namespace Aws
16 {
17  namespace Utils
18  {
19  namespace Threading
20  {
26  {
27  public:
33  void LockReader();
34 
40  void UnlockReader();
41 
46  void LockWriter();
47 
53  void UnlockWriter();
54  private:
55  std::atomic<int64_t> m_readers;
56  std::atomic<int64_t> m_holdouts;
57  Semaphore m_readerSem;
58  Semaphore m_writerSem;
59  std::mutex m_writerLock;
60  };
61 
63  {
64  public:
65  explicit ReaderLockGuard(ReaderWriterLock& rwl) : m_rwlock(rwl), m_upgraded(false)
66  {
67  m_rwlock.LockReader();
68  }
69 
71  {
72  m_rwlock.UnlockReader();
73  m_rwlock.LockWriter();
74  m_upgraded = true;
75  }
76 
78  {
79  if(m_upgraded)
80  {
81  m_rwlock.UnlockWriter();
82  }
83  else
84  {
85  m_rwlock.UnlockReader();
86  }
87  }
88  // for VS2013
89  ReaderLockGuard(const ReaderLockGuard&) = delete;
91  private:
92  ReaderWriterLock& m_rwlock;
93  bool m_upgraded;
94  };
95 
97  {
98  public:
99  explicit WriterLockGuard(ReaderWriterLock& rwl) : m_rwlock(rwl)
100  {
101  m_rwlock.LockWriter();
102  }
103 
105  {
106  m_rwlock.UnlockWriter();
107  }
108  // for VS2013
111  private:
112  ReaderWriterLock& m_rwlock;
113  };
114  }
115  }
116 }
Aws::Utils::Threading::ReaderLockGuard::ReaderLockGuard
ReaderLockGuard(const ReaderLockGuard &)=delete
Aws::Utils::Threading::WriterLockGuard::WriterLockGuard
WriterLockGuard(ReaderWriterLock &rwl)
Definition: ReaderWriterLock.h:99
Aws::Utils::Threading::ReaderWriterLock::UnlockWriter
void UnlockWriter()
AWS_CORE_API
#define AWS_CORE_API
Definition: Core_EXPORTS.h:25
Aws::Utils::Threading::Semaphore
Definition: Semaphore.h:18
Aws::Utils::Threading::ReaderWriterLock::UnlockReader
void UnlockReader()
Aws::Utils::Threading::WriterLockGuard::operator=
WriterLockGuard & operator=(const WriterLockGuard &)=delete
Aws::Utils::Threading::ReaderLockGuard::UpgradeToWriterLock
void UpgradeToWriterLock()
Definition: ReaderWriterLock.h:70
Aws::Utils::Threading::WriterLockGuard::~WriterLockGuard
~WriterLockGuard()
Definition: ReaderWriterLock.h:104
Semaphore.h
Aws::Utils::Threading::ReaderLockGuard::ReaderLockGuard
ReaderLockGuard(ReaderWriterLock &rwl)
Definition: ReaderWriterLock.h:65
Aws::Utils::Threading::ReaderWriterLock::LockWriter
void LockWriter()
Aws::Utils::Threading::ReaderWriterLock::ReaderWriterLock
ReaderWriterLock()
Aws::Utils::Threading::ReaderLockGuard::~ReaderLockGuard
~ReaderLockGuard()
Definition: ReaderWriterLock.h:77
Aws::Utils::Threading::ReaderWriterLock
Definition: ReaderWriterLock.h:26
Core_EXPORTS.h
Aws::Utils::Threading::ReaderWriterLock::LockReader
void LockReader()
Aws::Utils::Threading::ReaderLockGuard
Definition: ReaderWriterLock.h:63
Aws::Utils::Threading::WriterLockGuard::WriterLockGuard
WriterLockGuard(const WriterLockGuard &)=delete
Aws
Definition: AccessManagementClient.h:15
Aws::Utils::Threading::WriterLockGuard
Definition: ReaderWriterLock.h:97
Aws::Utils::Threading::ReaderLockGuard::operator=
ReaderLockGuard & operator=(const ReaderLockGuard &)=delete