AWS SDK for C++

AWS SDK for C++ Version 1.11.440

Loading...
Searching...
No Matches
ReaderWriterLock.h
1
6#pragma once
7
8#include <aws/core/Core_EXPORTS.h>
9#include <aws/core/utils/threading/Semaphore.h>
10#include <mutex>
11#include <atomic>
12#include <condition_variable>
13#include <cstdint>
14
15namespace Aws
16{
17 namespace Utils
18 {
19 namespace Threading
20 {
25 class AWS_CORE_API ReaderWriterLock
26 {
27 public:
33 void LockReader();
34
41
46 void LockWriter();
47
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
62 class AWS_CORE_API ReaderLockGuard
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
91 private:
92 ReaderWriterLock& m_rwlock;
93 bool m_upgraded;
94 };
95
96 class AWS_CORE_API WriterLockGuard
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}
ReaderLockGuard & operator=(const ReaderLockGuard &)=delete
ReaderLockGuard(const ReaderLockGuard &)=delete
WriterLockGuard & operator=(const WriterLockGuard &)=delete
WriterLockGuard(const WriterLockGuard &)=delete