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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2005,2009 by the Free Pascal development team
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
{$ifdef FPC_HAS_FEATURE_THREADING}
constructor TSimpleRWSync.Create;
begin
System.InitCriticalSection(Crit);
end;
destructor TSimpleRWSync.Destroy;
begin
System.DoneCriticalSection(Crit);
end;
function TSimpleRWSync.Beginwrite : boolean;
begin
System.EnterCriticalSection(Crit);
result:=true;
end;
procedure TSimpleRWSync.Endwrite;
begin
System.LeaveCriticalSection(Crit);
end;
procedure TSimpleRWSync.Beginread;
begin
System.EnterCriticalSection(Crit);
end;
procedure TSimpleRWSync.Endread;
begin
System.LeaveCriticalSection(Crit);
end;
type
PMREWThreadInfo = ^TMREWThreadInfo;
TMREWThreadInfo = record
Next: PMREWThreadInfo;
Active: longint;
RefCount: LongInt;
ThreadID: TThreadID;
end;
const
cInUse: LongInt = MaxInt;
cAvail: LongInt = 0;
const
cNewReader : LongInt = - $1;
cNewWriter : LongInt = $10000;
cReadMask : LongInt = $0000FFFF;
cWriteMask : LongInt = $7FFF0000;
constructor TMultiReadExclusiveWriteSynchronizer.Create;
begin
System.InitCriticalSection(fwritelock);
fwaitingwriterlock:=RTLEventCreate;
RTLEventResetEvent(fwaitingwriterlock);
fwriterequests:=0;
factivethreads:=0;
freaderqueue:=BasicEventCreate(nil,true,false,'');
{ synchronize initialization with later reads/writes }
ReadWriteBarrier;
end;
destructor TMultiReadExclusiveWriteSynchronizer.Destroy;
var
p,q: PMREWThreadInfo;
i: integer;
begin
System.DoneCriticalSection(fwritelock);
RtlEventDestroy(fwaitingwriterlock);
BasicEventDestroy(freaderqueue);
{ Clean up thread info }
for i:=Low(fThreadList) to High(fThreadList) do
begin
q:=fThreadList[i];
fThreadList[i]:=nil;
while q<>nil do
begin
p:=q;
q:=q^.Next;
FreeMem(p);
end;
end;
end;
function TMultiReadExclusiveWriteSynchronizer.Beginwrite : boolean;
var
p: PMREWThreadInfo;
begin
{ Result indicates whether the protected memory was not modified,
that is, it is set to false if another writer could have chagned
memory}
Result:=True;
{ for quick checking by candidate-readers -- use interlockedincrement/
decrement instead of setting 1/0, because a single thread can
recursively acquire the write lock multiple times }
{ Count pending not granted requests so writes always take priority over
read requests}
System.InterlockedIncrement(fwriterequests);
{ Get per thread counter }
p:=PMREWThreadInfo( GetThreadInfo(True) );
if System.TryEnterCriticalSection(fwritelock)=0 then
begin
{ TryEnterCriticalSection failed, so not first in the write lock queue }
Result:=False;
{ If we hold a read lock, then a deadlock will result.. This is because
the first thread in the write queue (holding fwritelock) does not have
mutexes write lock, as it must be waiting on this thread to release
its' read lock }
if p^.RefCount > 0 then
begin
System.InterlockedDecrement(fwriterequests);
raise TMREWException.Create('Deadlock detected');
end;
{ wait for any other writers that may be in progress }
System.EnterCriticalSection(fwritelock);
end;
{ Need to synchronize with readers only when this is the first
write request by this thread }
if (p^.RefCount and cWriteMask)=0 then
begin
{ Count active threads rather than readers. A write request can
be granted whenever the count has reduced to one }
{ no order vs increment of fwriterequests needed, because we acquired
a critical section which orders for us }
if p^.RefCount=0 then
System.InterlockedIncrement(factivethreads);
{ new readers have to block from now on; writers get priority to avoid
writer starvation (since they have to compete with potentially many
concurrent readers and other writers) }
BasicEventResetEvent(freaderqueue);
{ it is possible that earlier on we missed waiting on the
fwaitingwriterlock and that it's still set (must be done
after acquiring the fwritelock, because otherwise one
writer could reset the fwaitingwriterlock of another one
that's about to wait on it) }
RTLeventResetEvent(fwaitingwriterlock);
{ wait until we are the only active thread (no need for memory order
barriers, because RTLeventResetEvent and RTLEventWaitFor imply a
full barrier) }
while System.InterlockedExchangeAdd(factivethreads,0) > 1 do
RTLEventWaitFor(fwaitingwriterlock);
{ Make sure that out-of-order execution cannot already perform reads
inside the critical section before the lock has been acquired }
ReadBarrier;
end;
{ Count write lock acquisitions by thread }
Inc(p^.RefCount,cNewWriter);
end;
procedure TMultiReadExclusiveWriteSynchronizer.Endwrite;
var
p: PMREWThreadInfo;
begin
p:=PMREWThreadInfo( GetThreadInfo(False) );
{ Protect against EndWrite called out of sequence blocking lock }
if (p<>nil) and
((p^.RefCount and cWriteMask)<>0) then
begin
{ Update per thread counter before releasing next write thread }
Dec(p^.RefCount,cNewWriter);
{ Finish all writes inside the section, and the update of the RefCount,
so that everything executing afterwards will certainly see these
results }
WriteBarrier;
{ Reduce active thread count assuming it was not previously a read lock }
if p^.RefCount=0 then
begin
System.InterlockedDecrement(factivethreads);
{ order w.r.t. decrement of fwriterequests below }
WriteBarrier;
end;
{ signal potential readers that the coast is clear if all recursive
write locks have been freed }
{ Also test for pending write requests }
if System.InterlockedDecrement(fwriterequests)=0 then
begin
{ No more writers pending, wake any pending readers }
BasicEventSetEvent(freaderqueue);
end;
{ free the writer lock so another writer can become active }
System.LeaveCriticalSection(fwritelock);
{ Remove reference to thread if not in use any more }
if p^.RefCount=0 then
RemoveThread(p);
end
else
raise TMREWException.Create('EndWrite called before BeginWrite');
end;
procedure TMultiReadExclusiveWriteSynchronizer.Beginread;
Const
wrSignaled = 0;
wrTimeout = 1;
wrAbandoned= 2;
wrError = 3;
var
p: PMREWThreadInfo;
begin
{ Check if we already have a lock, if so grant immediate access }
p:=PMREWThreadInfo( GetThreadInfo(True) );
if p^.RefCount=0 then
begin
{ Wanted non-recursive read lock, so increase active threads }
System.InterlockedIncrement(factivethreads);
{ wait until there are no more writer active or pending }
ReadWriteBarrier;
while System.InterlockedExchangeAdd(fwriterequests,0)<>0 do
begin
ReadWriteBarrier;
{ This thread is not active }
if System.InterlockedDecrement(factivethreads)<>0 then
RTLEventSetEvent(fwaitingwriterlock);
if (BasicEventWaitFor(high(cardinal),freaderqueue) in [wrAbandoned,wrError]) then
raise TMREWException.create('BasicEventWaitFor failed in TMultiReadExclusiveWriteSynchronizer.Beginread');
{ Try again to make this thread active }
System.InterlockedIncrement(factivethreads);
{ order w.r.t. reading fwriterequests }
ReadWriteBarrier;
end;
{ Make sure that out-of-order execution cannot perform reads
inside the critical section before the lock has been acquired }
ReadBarrier;
end;
{ Count read lock acquisitions by thread: Inc(p^.RefCount,cNewReader) }
Inc(p^.RefCount);
end;
procedure TMultiReadExclusiveWriteSynchronizer.Endread;
var
p: PMREWThreadInfo;
begin
p:=PMREWThreadInfo( GetThreadInfo(False) );
if (p<>nil) and
((p^.RefCount and cReadMask)<>0) then
begin
{ Update per thread counter: Dec(p^.RefCount,cNewReader) }
Dec(p^.RefCount);
{ if this is the last recursive call }
if p^.RefCount=0 then
begin
{ Thread no longer has an active lock }
{ If no more readers, wake writer in the ready-queue if any.
Every queued thread requesting a write lock increments fwriterequests,
and the first queued writer thread checks factivethreads is active
(will be set already during lock promotion) }
if System.InterlockedDecrement(factivethreads)=1 then
begin
{ order w.r.t. access to factivethreads }
ReadBarrier;
if fwriterequests>0 then
RTLEventSetEvent(fwaitingwriterlock);
end;
{ remove reference to this thread }
RemoveThread(p);
end;
end
else
raise TMREWException.Create('EndRead called before BeginRead');
end;
function TMultiReadExclusiveWriteSynchronizer.ThreadIDtoIndex(aThreadID: TThreadID): integer;
begin
Result:=
(
ptruint(aThreadID) xor (ptruint(aThreadID) shr 12)
{$ifdef cpu64}
xor (ptruint(aThreadID) shr 32) xor (ptruint(aThreadID) shr 36) xor (ptruint(aThreadID) shr 48)
{$endif}
) and $FFFF;
Result:=(Result xor (Result shr 4)) and $0F; // Return range 0..15
end;
function TMultiReadExclusiveWriteSynchronizer.GetThreadInfo(AutoCreate: Boolean): Pointer;
var
p: PMREWThreadInfo;
AThreadID: TThreadID;
FreeSlot: Boolean;
OldState: LongInt;
Index: integer;
begin
FreeSlot:=False;
AThreadID:=ThreadID;
Index:=ThreadIDtoIndex( AThreadID );
p:=PMREWThreadInfo(fThreadList[Index]);
while (p<>nil) and
(p^.ThreadID<>AThreadID) do
begin
if p^.Active=cAvail then // Is slot available for use
FreeSlot:=True; // Yes, remember in case we need it as this is a new thread
p:=p^.Next;
ReadBarrier;
end;
if p=nil then
begin
{ count threads with locks }
if FreeSlot then
begin
p:=fThreadList[Index];
while (p<>nil) do
begin
if p^.Active=cAvail then
begin
OldState:=InterlockedExchange( p^.Active, cInUse );
if OldState=cAvail then
begin
p^.ThreadID:=AThreadID; // Tag to thread
Break;
end;
end;
p:=p^.Next;
ReadBarrier;
end;
end;
if p=nil then
begin
p:=PMREWThreadInfo(AllocMem(SizeOf(TMREWThreadInfo)));
p^.ThreadID:=AThreadID;
p^.RefCount:=0;
p^.Active:=cInUse;
{ Now insert into the chain header }
p^.Next:=p;
WriteBarrier;
{ other threads will spin (loop) after the InterlockedExchange
until the field "next" is written, then this node is first in
the forward linked list }
p^.Next:=System.InterlockedExchange(fThreadList[Index],p);
end;
end;
Result:=p;
end;
procedure TMultiReadExclusiveWriteSynchronizer.RemoveThread(AThreadInfo: Pointer);
var
p: PMREWThreadInfo;
begin
p:=PMREWThreadInfo(AThreadInfo);
if p<>nil then
begin
{ Prevent matching during GetThreadInfo }
p^.ThreadID:=tthreadid(-1);
WriteBarrier;
{ Mark slot available }
p^.Active:=cAvail;
end;
end;
{$endif FPC_HAS_FEATURE_THREADING}
|