-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathreader_writer_lock.cpp
More file actions
347 lines (309 loc) · 12.6 KB
/
reader_writer_lock.cpp
File metadata and controls
347 lines (309 loc) · 12.6 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
Copyright (c) 2005-2017 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "tbb/reader_writer_lock.h"
#include "tbb/tbb_machine.h"
#include "tbb/tbb_exception.h"
#include "itt_notify.h"
#if defined(_MSC_VER) && defined(_Wp64)
// Workaround for overzealous compiler warnings in /Wp64 mode
#pragma warning (disable: 4244)
#endif
namespace tbb {
namespace interface5 {
const uintptr_t WFLAG1 = 0x1; // writer interested or active
const uintptr_t WFLAG2 = 0x2; // writers interested, no entering readers
const uintptr_t RFLAG = 0x4; // reader interested but not active
const uintptr_t RC_INCR = 0x8; // to adjust reader count
// Perform an atomic bitwise-OR on the operand, and return its previous value.
inline uintptr_t fetch_and_or(atomic<uintptr_t>& operand, uintptr_t value) {
for (tbb::internal::atomic_backoff b;;b.pause()) {
uintptr_t old = operand;
uintptr_t result = operand.compare_and_swap(old|value, old);
if (result==old) return result;
}
}
// Perform an atomic bitwise-AND on the operand, and return its previous value.
inline uintptr_t fetch_and_and(atomic<uintptr_t>& operand, uintptr_t value) {
for (tbb::internal::atomic_backoff b;;b.pause()) {
uintptr_t old = operand;
uintptr_t result = operand.compare_and_swap(old&value, old);
if (result==old) return result;
}
}
//! Spin WHILE the value at the location is greater than or equal to a given value
/** T and U should be comparable types. */
template<typename T, typename U>
void spin_wait_while_geq( const volatile T& location, U value ) {
tbb::internal::atomic_backoff backoff;
while( location>=value ) backoff.pause();
}
//! Spin UNTIL (location & value) is true.
/** T and U should be comparable types. */
template<typename T, typename U>
void spin_wait_until_and( const volatile T& location, U value ) {
tbb::internal::atomic_backoff backoff;
while( !(location & value) ) backoff.pause();
}
void reader_writer_lock::internal_construct() {
reader_head = NULL;
writer_head = NULL;
writer_tail = NULL;
rdr_count_and_flags = 0;
my_current_writer = tbb_thread::id();
#if TBB_USE_THREADING_TOOLS
ITT_SYNC_CREATE(this, _T("tbb::reader_writer_lock"), _T(""));
#endif /* TBB_USE_THREADING_TOOLS */
}
void reader_writer_lock::internal_destroy() {
__TBB_ASSERT(rdr_count_and_flags==0, "reader_writer_lock destroyed with pending readers/writers.");
__TBB_ASSERT(reader_head==NULL, "reader_writer_lock destroyed with pending readers.");
__TBB_ASSERT(writer_tail==NULL, "reader_writer_lock destroyed with pending writers.");
__TBB_ASSERT(writer_head==NULL, "reader_writer_lock destroyed with pending/active writers.");
}
// Acquires the reader_writer_lock for write. If the lock is currently held in write
// mode by another context, the writer will block by spinning on a local variable.
// Throws exception improper_lock if the context tries to acquire a
// reader_writer_lock that it already has write ownership of.
void reader_writer_lock::lock() {
if (is_current_writer()) { // recursive lock attempt
// we don't support recursive writer locks; throw exception
tbb::internal::throw_exception(tbb::internal::eid_improper_lock);
}
else {
scoped_lock *a_writer_lock = new scoped_lock();
(void) start_write(a_writer_lock);
}
}
// Tries to acquire the reader_writer_lock for write. This function does not block.
// Return Value: True or false, depending on whether the lock is acquired or not.
// If the lock is already held by this acquiring context, try_lock() returns false.
bool reader_writer_lock::try_lock() {
if (is_current_writer()) { // recursive lock attempt
return false;
}
else {
scoped_lock *a_writer_lock = new scoped_lock();
a_writer_lock->status = waiting_nonblocking;
return start_write(a_writer_lock);
}
}
bool reader_writer_lock::start_write(scoped_lock *I) {
tbb_thread::id id = this_tbb_thread::get_id();
scoped_lock *pred = NULL;
if (I->status == waiting_nonblocking) {
if ((pred = writer_tail.compare_and_swap(I, NULL)) != NULL) {
delete I;
return false;
}
}
else {
ITT_NOTIFY(sync_prepare, this);
pred = writer_tail.fetch_and_store(I);
}
if (pred)
pred->next = I;
else {
set_next_writer(I);
if (I->status == waiting_nonblocking) {
if (I->next) { // potentially more writers
set_next_writer(I->next);
}
else { // no more writers
writer_head.fetch_and_store(NULL);
if (I != writer_tail.compare_and_swap(NULL, I)) { // an incoming writer is in the process of being added
spin_wait_while_eq(I->next, (scoped_lock *)NULL); // wait for new writer to be added
__TBB_ASSERT(I->next, "There should be a node following the last writer.");
set_next_writer(I->next);
}
}
delete I;
return false;
}
}
spin_wait_while_eq(I->status, waiting);
ITT_NOTIFY(sync_acquired, this);
my_current_writer = id;
return true;
}
void reader_writer_lock::set_next_writer(scoped_lock *W) {
writer_head = W;
if (W->status == waiting_nonblocking) {
if (rdr_count_and_flags.compare_and_swap(WFLAG1+WFLAG2, 0) == 0) {
W->status = active;
}
}
else {
if (fetch_and_or(rdr_count_and_flags, WFLAG1) & RFLAG) { // reader present
spin_wait_until_and(rdr_count_and_flags, WFLAG2); // block until readers set WFLAG2
}
else { // no reader in timing window
__TBB_AtomicOR(&rdr_count_and_flags, WFLAG2);
}
spin_wait_while_geq(rdr_count_and_flags, RC_INCR); // block until readers finish
W->status = active;
}
}
// Acquires the reader_writer_lock for read. If the lock is currently held by a writer,
// this reader will block and wait until the writers are done.
// Throws exception improper_lock when the context tries to acquire a reader_writer_lock
// that it already has write ownership of.
void reader_writer_lock::lock_read() {
if (is_current_writer()) { // recursive lock attempt
// we don't support writer->reader downgrade; throw exception
tbb::internal::throw_exception(tbb::internal::eid_improper_lock);
}
else {
scoped_lock_read a_reader_lock;
start_read(&a_reader_lock);
}
}
// Tries to acquire the reader_writer_lock for read. This function does not block.
// Return Value: True or false, depending on whether the lock is acquired or not.
bool reader_writer_lock::try_lock_read() {
if (is_current_writer()) { // recursive lock attempt
return false;
}
else {
if (rdr_count_and_flags.fetch_and_add(RC_INCR) & (WFLAG1+WFLAG2)) { // writers present
rdr_count_and_flags -= RC_INCR;
return false;
}
else { // no writers
ITT_NOTIFY(sync_acquired, this);
return true;
}
}
}
void reader_writer_lock::start_read(scoped_lock_read *I) {
ITT_NOTIFY(sync_prepare, this);
I->next = reader_head.fetch_and_store(I);
if (!I->next) { // first arriving reader in my group; set RFLAG, test writer flags
// unblock and/or update statuses of non-blocking readers
if (!(fetch_and_or(rdr_count_and_flags, RFLAG) & (WFLAG1+WFLAG2))) { // no writers
unblock_readers();
}
}
__TBB_ASSERT(I->status == waiting || I->status == active, "Lock requests should be waiting or active before blocking.");
spin_wait_while_eq(I->status, waiting); // block
if (I->next) {
__TBB_ASSERT(I->next->status == waiting, NULL);
rdr_count_and_flags += RC_INCR;
I->next->status = active; // wake successor
}
ITT_NOTIFY(sync_acquired, this);
}
void reader_writer_lock::unblock_readers() {
// clear rdr interest flag, increment rdr count
__TBB_ASSERT(rdr_count_and_flags&RFLAG, NULL);
rdr_count_and_flags += RC_INCR-RFLAG;
__TBB_ASSERT(rdr_count_and_flags >= RC_INCR, NULL);
// indicate clear of window
if (rdr_count_and_flags & WFLAG1 && !(rdr_count_and_flags & WFLAG2)) {
__TBB_AtomicOR(&rdr_count_and_flags, WFLAG2);
}
// unblock waiting readers
scoped_lock_read *head = reader_head.fetch_and_store(NULL);
__TBB_ASSERT(head, NULL);
__TBB_ASSERT(head->status == waiting, NULL);
head->status = active;
}
// Releases the reader_writer_lock
void reader_writer_lock::unlock() {
if( my_current_writer!=tbb_thread::id() ) {
// A writer owns the lock
__TBB_ASSERT(is_current_writer(), "caller of reader_writer_lock::unlock() does not own the lock.");
__TBB_ASSERT(writer_head, NULL);
__TBB_ASSERT(writer_head->status==active, NULL);
scoped_lock *a_writer_lock = writer_head;
end_write(a_writer_lock);
__TBB_ASSERT(a_writer_lock != writer_head, "Internal error: About to turn writer_head into dangling reference.");
delete a_writer_lock;
} else {
end_read();
}
}
void reader_writer_lock::end_write(scoped_lock *I) {
__TBB_ASSERT(I==writer_head, "Internal error: can't unlock a thread that is not holding the lock.");
my_current_writer = tbb_thread::id();
ITT_NOTIFY(sync_releasing, this);
if (I->next) { // potentially more writers
writer_head = I->next;
writer_head->status = active;
}
else { // No more writers; clear writer flag, test reader interest flag
__TBB_ASSERT(writer_head, NULL);
if (fetch_and_and(rdr_count_and_flags, ~(WFLAG1+WFLAG2)) & RFLAG) {
unblock_readers();
}
writer_head.fetch_and_store(NULL);
if (I != writer_tail.compare_and_swap(NULL, I)) { // an incoming writer is in the process of being added
spin_wait_while_eq(I->next, (scoped_lock *)NULL); // wait for new writer to be added
__TBB_ASSERT(I->next, "There should be a node following the last writer.");
set_next_writer(I->next);
}
}
}
void reader_writer_lock::end_read() {
ITT_NOTIFY(sync_releasing, this);
__TBB_ASSERT(rdr_count_and_flags >= RC_INCR, "unlock() called but no readers hold the lock.");
rdr_count_and_flags -= RC_INCR;
}
inline bool reader_writer_lock::is_current_writer() {
return my_current_writer==this_tbb_thread::get_id();
}
// Construct with a blocking attempt to acquire a write lock on the passed reader_writer_lock
void reader_writer_lock::scoped_lock::internal_construct (reader_writer_lock& lock) {
mutex = &lock;
next = NULL;
status = waiting;
if (mutex->is_current_writer()) { // recursive lock attempt
// we don't support recursive writer locks; throw exception
tbb::internal::throw_exception(tbb::internal::eid_improper_lock);
}
else { // this thread holds no locks
(void) mutex->start_write(this);
}
}
inline reader_writer_lock::scoped_lock::scoped_lock() : mutex(NULL), next(NULL) {
status = waiting;
}
// Construct with a blocking attempt to acquire a write lock on the passed reader_writer_lock
void reader_writer_lock::scoped_lock_read::internal_construct (reader_writer_lock& lock) {
mutex = &lock;
next = NULL;
status = waiting;
if (mutex->is_current_writer()) { // recursive lock attempt
// we don't support writer->reader downgrade; throw exception
tbb::internal::throw_exception(tbb::internal::eid_improper_lock);
}
else { // this thread holds no locks
mutex->start_read(this);
}
}
inline reader_writer_lock::scoped_lock_read::scoped_lock_read() : mutex(NULL), next(NULL) {
status = waiting;
}
void reader_writer_lock::scoped_lock::internal_destroy() {
if (mutex) {
__TBB_ASSERT(mutex->is_current_writer(), "~scoped_lock() destroyed by thread different than thread that holds lock.");
mutex->end_write(this);
}
status = invalid;
}
void reader_writer_lock::scoped_lock_read::internal_destroy() {
if (mutex)
mutex->end_read();
status = invalid;
}
} // namespace interface5
} // namespace tbb