/
RWLock.pas
420 lines (379 loc) · 9.88 KB
/
RWLock.pas
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
{ Implementation of Slim read-write lock based on https://github.com/neosmart/RWLock
Copyright (C) 2018-2020 Red_prig
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
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. See the GNU Library General Public License
for more details.
}
unit RWLock;
{$mode objfpc}{$H+}
interface
{$IFDEF UNIX}
{$DEFINE SMART} //Force NeoSmart algoritm
{$ENDIF}
{/$DEFINE SMART}
{$IF DEFINED(UNIX)) and (not DEFINED(SMART))}
uses
UnixType,pthreads;
type
TRWLock=pthread_rwlock_t;
{$ELSE}
{$IF DEFINED(WINDOWS) and (not DEFINED(SMART))}
uses
Windows;
{$ENDIF}
type
TRWLock=packed record
Case Byte of
0:(SRWLock:Pointer;Mode:SizeInt);
1:(Event:PRTLEvent;vLock:DWORD);
end;
{$ENDIF}
Procedure rwlock_init(Var L:TRWLock);
Procedure rwlock_destroy(Var L:TRWLock);
Procedure rwlock_rdlock(Var L:TRWLock);
Procedure rwlock_wrlock(Var L:TRWLock);
function rwlock_tryrdlock(Var L:TRWLock):Boolean;
function rwlock_trywrlock(Var L:TRWLock):Boolean;
Procedure rwlock_unlock(Var L:TRWLock);
implementation
{$IF DEFINED(WINDOWS) and (not DEFINED(SMART))}
type
TSRWLockProc=procedure(Var SRWLock:Pointer); stdcall;
TSRWLockTryFunc=Function(Var SRWLock:Pointer):Boolean; stdcall;
Var
InitializeSRWLock,
AcquireSRWLockShared,
ReleaseSRWLockShared,
AcquireSRWLockExclusive,
ReleaseSRWLockExclusive:TSRWLockProc;
TryAcquireSRWLockShared,
TryAcquireSRWLockExclusive:TSRWLockTryFunc;
Procedure LoadSRW;
Var
Handle:THandle;
begin
InitializeSRWLock :=nil;
AcquireSRWLockShared :=nil;
ReleaseSRWLockShared :=nil;
AcquireSRWLockExclusive :=nil;
ReleaseSRWLockExclusive :=nil;
TryAcquireSRWLockShared :=nil;
TryAcquireSRWLockExclusive:=nil;
Handle:=GetModuleHandle('kernel32.dll');
if Handle<>INVALID_HANDLE_VALUE then
begin
Pointer(InitializeSRWLock ):=GetProcAddress(Handle,'InitializeSRWLock');
if InitializeSRWLock<>nil then
begin
Pointer(AcquireSRWLockShared ):=GetProcAddress(Handle,'AcquireSRWLockShared');
Pointer(ReleaseSRWLockShared ):=GetProcAddress(Handle,'ReleaseSRWLockShared');
Pointer(AcquireSRWLockExclusive ):=GetProcAddress(Handle,'AcquireSRWLockExclusive');
Pointer(ReleaseSRWLockExclusive ):=GetProcAddress(Handle,'ReleaseSRWLockExclusive');
Pointer(TryAcquireSRWLockShared ):=GetProcAddress(Handle,'TryAcquireSRWLockShared');
Pointer(TryAcquireSRWLockExclusive):=GetProcAddress(Handle,'TryAcquireSRWLockExclusive');
end;
end;
end;
{$ENDIF}
{$IF (not DEFINED(UNIX)) or DEFINED(SMART)}
Const
MAX_SPIN=50000;
function ReaderCount(lock:DWORD):WORD; inline;
begin
Result:=WORD(lock and $00007FFF);
end;
function SetReaders(lock:DWORD;readers:WORD):DWORD; inline;
begin
Result:=(lock and (not $00007FFF)) or readers;
end;
function WaitingCount(lock:DWORD):WORD; inline;
begin
Result:=WORD((lock and $3FFF8000) shr 15);
end;
function SetWaiting(lock:DWORD;waiting:WORD):DWORD; inline;
begin
Result:=(lock and (not $3FFF8000)) or (waiting shl 15);
end;
function Writer(lock:DWORD):Boolean; inline;
begin
Result:=(lock and $40000000)<>0;
end;
function SetWriter(lock:DWORD;writer:Boolean):DWORD; inline;
begin
if writer then
Result:=lock or $40000000
else
Result:=lock and (not $40000000);
end;
function AllClear(lock:DWORD):Boolean; inline;
begin
Result:=(lock and $40007FFF)=0;
end;
function Initialized(lock:DWORD):Boolean; inline;
begin
Result:=(lock and $80000000)<>0;
end;
function SetInitialized(lock:DWORD;init:Boolean):DWORD; inline;
begin
if init then
Result:=lock or $80000000
else
Result:=lock and (not $80000000);
end;
Procedure _rdlock(Var vLock:DWORD;Event:PRTLEvent); inline;
Var
i:SizeUInt;
temp:DWORD;
begin
i:=0;
repeat
temp:=vLock;
if not Writer(temp) then
begin
if System.InterlockedCompareExchange(vLock,SetReaders(temp,ReaderCount(temp)+1),temp)=temp then
Break
else
Continue;
end else
begin
if (i<MAX_SPIN) then
begin
ThreadSwitch;
Continue;
end;
if System.InterlockedCompareExchange(vLock,SetWaiting(temp,WaitingCount(temp)+1),temp)<>temp then
Continue;
RTLeventWaitFor(Event);
i:=0;
repeat
temp:=vLock;
if (i>MAX_SPIN) then
begin
ThreadSwitch;
Continue;
end;
Inc(i);
until System.InterlockedCompareExchange(vLock,SetWaiting(temp,WaitingCount(temp)-1),temp)=temp;
i:=0;
end;
Inc(i);
until False;
end;
Procedure _wrlock(Var vLock:DWORD;Event:PRTLEvent); inline;
Var
i:SizeUInt;
temp:DWORD;
begin
i:=0;
repeat
temp:=vLock;
if AllClear(temp) then
begin
if System.InterlockedCompareExchange(vLock,SetWriter(temp,true),temp)=temp then
Break
else
Continue;
end else
begin
if (i<MAX_SPIN) then
begin
ThreadSwitch;
Continue;
end;
if System.InterlockedCompareExchange(vLock,SetWaiting(temp,WaitingCount(temp)+1),temp)<>temp then
Continue;
RTLeventWaitFor(Event);
i:=0;
repeat
temp:=vLock;
if (i>MAX_SPIN) then
begin
ThreadSwitch;
Continue;
end;
Inc(i);
until System.InterlockedCompareExchange(vLock,SetWaiting(temp,WaitingCount(temp)-1),temp)=temp;
i:=0;
end;
Inc(i);
until False;
end;
Procedure _unlock(Var vLock:DWORD;Event:PRTLEvent); inline;
Var
temp:DWORD;
begin
if ReaderCount(vLock)=0 then
begin
repeat
repeat
temp:=vLock;
if (WaitingCount(temp)=0) then break;
RTLeventSetEvent(Event);
until False;
until System.InterlockedCompareExchange(vLock,SetWriter(temp,false),temp)=temp;
end else
begin
repeat
temp:=vLock;
if (ReaderCount(temp)=1) and (WaitingCount(temp)<>0) then
RTLeventSetEvent(Event);
until System.InterlockedCompareExchange(vLock,SetReaders(temp,ReaderCount(temp)-1),temp)=temp;
end;
end;
function _tryrdlock(Var vLock:DWORD):Boolean; inline;
Var
temp:DWORD;
begin
Result:=False;
temp:=vLock;
if not Writer(temp) then
begin
if System.InterlockedCompareExchange(vLock,SetReaders(temp,ReaderCount(temp)+1),temp)=temp then
Result:=True;
end;
end;
function _trywrlock(Var vLock:DWORD):Boolean; inline;
Var
temp:DWORD;
begin
Result:=False;
temp:=vLock;
if AllClear(temp) then
begin
if System.InterlockedCompareExchange(vLock,SetWriter(temp,true),temp)=temp then
Result:=True;
end;
end;
{$ENDIF}
Procedure rwlock_init(Var L:TRWLock);
begin
{$IF DEFINED(UNIX)) and (not DEFINED(SMART))}
pthread_rwlock_init(@L,nil);
{$ELSE}
L:=Default(TRWLock);
{$IF DEFINED(WINDOWS) and (not DEFINED(SMART))}
if InitializeSRWLock<>nil then
begin
InitializeSRWLock(L.SRWLock);
L.Mode:=0;
end else
{$ENDIF}
begin
L.vLock:=SetInitialized(0,true);
L.Event:=RTLEventCreate;
end;
{$ENDIF}
end;
Procedure rwlock_destroy(Var L:TRWLock);
begin
{$IF DEFINED(UNIX)) and (not DEFINED(SMART))}
pthread_rwlock_destroy(@L);
{$ELSE}
{$IF DEFINED(WINDOWS) and (not defined(SMART))}
if InitializeSRWLock=nil then
{$ENDIF}
RTLEventDestroy(L.Event);
{$ENDIF}
end;
Procedure rwlock_rdlock(Var L:TRWLock);
begin
{$IF DEFINED(UNIX)) and (not DEFINED(SMART))}
pthread_rwlock_rdlock(@L);
{$ELSE}
{$IF DEFINED(WINDOWS) and (not DEFINED(SMART))}
if InitializeSRWLock<>nil then
begin
AcquireSRWLockShared(L.SRWLock);
L.Mode:=1;
end else
{$ENDIF}
_rdlock(L.vLock,L.Event);
{$ENDIF}
end;
Procedure rwlock_wrlock(Var L:TRWLock);
begin
{$IF DEFINED(UNIX)) and (not DEFINED(SMART))}
pthread_rwlock_wrlock(@L);
{$ELSE}
{$IF DEFINED(WINDOWS) and (not DEFINED(SMART))}
if InitializeSRWLock<>nil then
begin
AcquireSRWLockExclusive(L.SRWLock);
L.Mode:=2;
end else
{$ENDIF}
_wrlock(L.vLock,L.Event);
{$ENDIF}
end;
function rwlock_tryrdlock(Var L:TRWLock):Boolean;
begin
{$IF DEFINED(UNIX)) and (not DEFINED(SMART))}
Result:=pthread_rwlock_tryrdlock(@L)=0;
{$ELSE}
{$IF DEFINED(WINDOWS) and (not DEFINED(SMART))}
if InitializeSRWLock<>nil then
begin
Result:=TryAcquireSRWLockShared(L.SRWLock);
if Result then
L.Mode:=1;
end else
{$ENDIF}
Result:=_tryrdlock(L.vLock);
{$ENDIF}
end;
function rwlock_trywrlock(Var L:TRWLock):Boolean;
begin
{$IF DEFINED(UNIX)) and (not DEFINED(SMART))}
Result:=pthread_rwlock_trywrlock(@L)=0;
{$ELSE}
{$IF DEFINED(WINDOWS) and (not DEFINED(SMART))}
if InitializeSRWLock<>nil then
begin
Result:=TryAcquireSRWLockExclusive(L.SRWLock);
if Result then
L.Mode:=2;
end else
{$ENDIF}
Result:=_trywrlock(L.vLock);
{$ENDIF}
end;
Procedure rwlock_unlock(Var L:TRWLock);
begin
{$IF DEFINED(UNIX)) and (not DEFINED(SMART))}
pthread_rwlock_unlock(@L);
{$ELSE}
{$IF DEFINED(WINDOWS) and (not DEFINED(SMART))}
if InitializeSRWLock<>nil then
begin
if L.SRWLock<>nil then
Case L.Mode of
1:ReleaseSRWLockShared(L.SRWLock);
2:ReleaseSRWLockExclusive(L.SRWLock);
end;
end else
{$ENDIF}
_unlock(L.vLock,L.Event);
{$ENDIF}
end;
{$IF DEFINED(WINDOWS) and (not DEFINED(SMART))}
initialization
begin
LoadSRW;
end;
{$ENDIF}
end.