Repository navigation
Expand file tree
/
Copy pathrwlock.h
More file actions
158 lines (136 loc) · 3.96 KB
/
Copy pathrwlock.h
File metadata and controls
158 lines (136 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//---------------------------------------------------------
// For conditions of distribution and use, see
// https://github.com/preshing/cpp11-on-multicore/blob/master/LICENSE
//---------------------------------------------------------
#ifndef __CPP11OM_RWLOCK_H__
#define __CPP11OM_RWLOCK_H__
#include <cassert>
#include <atomic>
#include <random>
#include "sema.h"
#include "bitfield.h"
//---------------------------------------------------------
// NonRecursiveRWLock
//---------------------------------------------------------
class NonRecursiveRWLock
{
private:
BEGIN_BITFIELD_TYPE(Status, uint32_t)
ADD_BITFIELD_MEMBER(readers, 0, 10)
ADD_BITFIELD_MEMBER(waitToRead, 10, 10)
ADD_BITFIELD_MEMBER(writers, 20, 10)
END_BITFIELD_TYPE()
std::atomic<uint32_t> m_status;
DefaultSemaphoreType m_readSema;
DefaultSemaphoreType m_writeSema;
public:
NonRecursiveRWLock() : m_status(0) {}
void lockReader()
{
Status oldStatus = m_status.load(std::memory_order_relaxed);
Status newStatus;
do
{
newStatus = oldStatus;
if (oldStatus.writers > 0)
{
newStatus.waitToRead++;
}
else
{
newStatus.readers++;
}
// CAS until successful. On failure, oldStatus will be updated with the latest value.
}
while (!m_status.compare_exchange_weak(oldStatus, newStatus,
std::memory_order_acquire, std::memory_order_relaxed));
if (oldStatus.writers > 0)
{
m_readSema.wait();
}
}
void unlockReader()
{
Status oldStatus = m_status.fetch_sub(Status().readers.one(), std::memory_order_release);
assert(oldStatus.readers > 0);
if (oldStatus.readers == 1 && oldStatus.writers > 0)
{
m_writeSema.signal();
}
}
void lockWriter()
{
Status oldStatus = m_status.fetch_add(Status().writers.one(), std::memory_order_acquire);
assert(oldStatus.writers + 1 <= Status().writers.maximum());
if (oldStatus.readers > 0 || oldStatus.writers > 0)
{
m_writeSema.wait();
}
}
void unlockWriter()
{
Status oldStatus = m_status.load(std::memory_order_relaxed);
Status newStatus;
uint32_t waitToRead = 0;
do
{
assert(oldStatus.readers == 0);
newStatus = oldStatus;
newStatus.writers--;
waitToRead = oldStatus.waitToRead;
if (waitToRead > 0)
{
newStatus.waitToRead = 0;
newStatus.readers = waitToRead;
}
// CAS until successful. On failure, oldStatus will be updated with the latest value.
}
while (!m_status.compare_exchange_weak(oldStatus, newStatus,
std::memory_order_release, std::memory_order_relaxed));
if (waitToRead > 0)
{
m_readSema.signal(waitToRead);
}
else if (oldStatus.writers > 1)
{
m_writeSema.signal();
}
}
};
//---------------------------------------------------------
// ReadLockGuard
//---------------------------------------------------------
template <class LockType>
class ReadLockGuard
{
private:
LockType& m_lock;
public:
ReadLockGuard(LockType& lock) : m_lock(lock)
{
m_lock.lockReader();
}
~ReadLockGuard()
{
m_lock.unlockReader();
}
};
//---------------------------------------------------------
// WriteLockGuard
//---------------------------------------------------------
template <class LockType>
class WriteLockGuard
{
private:
LockType& m_lock;
public:
WriteLockGuard(LockType& lock) : m_lock(lock)
{
m_lock.lockWriter();
}
~WriteLockGuard()
{
m_lock.unlockWriter();
}
};
#endif // __CPP11OM_RWLOCK_H__