forked from yavfast/dbg-spider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuRWLock.pas
605 lines (471 loc) · 12.2 KB
/
uRWLock.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
unit uRWLock;
interface
{.$DEFINE RWLOCK_DEBUG}
uses
System.Classes, System.SyncObjs, System.SysUtils, System.Types;
type
TRWNodeState = (nsReader, nsWriter);
{$IFDEF RWLOCK_DEBUG}
TRWLock = class;
TRWThreadNode = class;
TStack = Array[0..63] of Pointer;
TLockInfo = class
public
ThreadID: TThreadID;
EnterTime: TDateTime;
Stack: TStack;
function Clone: TLockInfo;
function StackToStr: String;
end;
TLockInfoList = class(TList)
protected
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
public
function Clone: TLockInfoList;
end;
TRWThreadLockInfoList = class(TList)
protected
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
end;
TLockEvent = procedure(Sender: TRWLock; const LockInfo: TLockInfo) of object;
TLockWaitEvent = procedure(Sender: TRWLock) of object;
{$ENDIF}
TRWThreadNode = class
strict private
FThreadID: TThreadID;
FReaderAcquires: Integer;
FWriterAcquires: Integer;
FWakeUpEvent: TEvent;
FState: TRWNodeState;
FIsWait: Boolean;
function GetAcquires: Integer; inline;
protected
function Clone: TRWThreadNode;
procedure IncAcquires;
procedure DecAcquires;
public
constructor Create(const AThreadID: TThreadID);
destructor Destroy; override;
procedure WakeUp; inline;
procedure Wait; inline;
property ThreadID: TThreadID read FThreadID;
property ReaderAcquires: Integer read FReaderAcquires;
property WriterAcquires: Integer read FWriterAcquires;
property Acquires: Integer read GetAcquires;
property State: TRWNodeState read FState write FState;
property IsWait: Boolean read FIsWait write FIsWait;
end;
ERWLockError = class(Exception);
TRWLock = class(TObject)
strict private
FLock: TCriticalSection;
FWaiters: TList;
{$IFDEF RWLOCK_DEBUG}
FLockInfoList: TLockInfoList;
FLockEvent: TLockEvent;
FLockWaitEvent: TLockWaitEvent;
procedure AddLockInfo(const ThreadID: TThreadID);
procedure DelLockInfo(const ThreadID: TThreadID);
{$ENDIF}
function GetRWNode(const Index: Integer): TRWThreadNode; inline;
function GetRWNodeCount: Integer; inline;
function GetNodeIndex(const ThreadID: TThreadID): Integer;
function HasActiveWriter(CurThreadNode: TRWThreadNode): Boolean;
function HasActiveNode(CurThreadNode: TRWThreadNode): Boolean;
function AddRWNode(const AThreadID: TThreadID): TRWThreadNode;
function GetRWThreadNode(const AThreadID: TThreadID): TRWThreadNode;
procedure WakeUpWaiters;
procedure Wait(ThreadNode: TRWThreadNode);
procedure WaitForReader(ThreadNode: TRWThreadNode);
procedure WaitForWriter(ThreadNode: TRWThreadNode);
property RWNode[const Index: Integer]: TRWThreadNode read GetRWNode;
property RWNodeCount: Integer read GetRWNodeCount;
protected
procedure RaiseRWLockException(const Msg: String);
public
constructor Create;
destructor Destroy; override;
procedure Lock(const AState: TRWNodeState);
function TryLock(const AState: TRWNodeState): Boolean;
procedure UnLock;
{$IFDEF RWLOCK_DEBUG}
function GetStatus: TRWThreadLockInfoList;
function GetLockStack: TLockInfoList;
property LockEvent: TLockEvent read FLockEvent write FLockEvent;
property LockWaitEvent: TLockWaitEvent read FLockWaitEvent write FLockWaitEvent;
{$ENDIF}
end;
implementation
type
TWaitersList = class(TList)
protected
procedure Notify(Ptr: Pointer; Action: TListNotification); override;
public
constructor Create;
end;
{ TRWNode }
function TRWThreadNode.Clone: TRWThreadNode;
begin
Result := TRWThreadNode.Create(FThreadID);
Result.FReaderAcquires := FReaderAcquires;
Result.FWriterAcquires := FWriterAcquires;
Result.FState := FState;
Result.FIsWait := FIsWait;
end;
constructor TRWThreadNode.Create(const AThreadID: TThreadID);
begin
inherited Create;
FThreadID := AThreadID;
FReaderAcquires := 0;
FWriterAcquires := 0;
FWakeUpEvent := TEvent.Create(nil, False, True, '');
FState := nsReader;
FIsWait := True;
end;
procedure TRWThreadNode.IncAcquires;
begin
case FState of
nsReader:
Inc(FReaderAcquires);
nsWriter:
Inc(FWriterAcquires);
end;
end;
procedure TRWThreadNode.Wait; // inline;
begin
FWakeUpEvent.WaitFor(INFINITE);
end;
procedure TRWThreadNode.WakeUp; // inline;
begin
FWakeUpEvent.SetEvent;
end;
procedure TRWThreadNode.DecAcquires;
begin
case FState of
nsReader:
Dec(FReaderAcquires);
nsWriter:
begin
Dec(FWriterAcquires);
if FWriterAcquires = 0 then
FState := nsReader;
end;
end;
end;
destructor TRWThreadNode.Destroy;
begin
FreeAndNil(FWakeUpEvent);
inherited;
end;
function TRWThreadNode.GetAcquires: Integer; // inline
begin
Result := FReaderAcquires + FWriterAcquires;
end;
{ TRWLock }
constructor TRWLock.Create;
begin
inherited Create;
FLock := TCriticalSection.Create;
FWaiters := TWaitersList.Create;
{$IFDEF RWLOCK_DEBUG}
FLockInfoList := TLockInfoList.Create;
FLockEvent := Nil;
FLockWaitEvent := Nil;
{$ENDIF}
end;
destructor TRWLock.Destroy;
begin
{$IFDEF RWLOCK_DEBUG}
if FWaiters.Count > 0 then
RaiseRWLockException('lock in use');
FreeAndNil(FLockInfoList);
{$ENDIF}
FreeAndNil(FWaiters);
FreeAndNil(FLock);
inherited;
end;
function TRWLock.HasActiveNode(CurThreadNode: TRWThreadNode): Boolean;
var
Idx: Integer;
Node: TRWThreadNode;
begin
Result := True;
Idx := 0;
while Idx < RWNodeCount do
begin
Node := RWNode[Idx];
if Node <> CurThreadNode then
if (Node.WriterAcquires > 0) or ((Node.ReaderAcquires > 0) and not(Node.IsWait)) then
Exit;
Inc(Idx);
end;
Result := False;
end;
function TRWLock.HasActiveWriter(CurThreadNode: TRWThreadNode): Boolean;
var
Idx: Integer;
Node: TRWThreadNode;
begin
Result := True;
Idx := 0;
while Idx < RWNodeCount do
begin
Node := RWNode[Idx];
if Node <> CurThreadNode then
if (Node.State = nsWriter) and (Node.WriterAcquires > 0) then
Exit;
Inc(Idx);
end;
Result := False;
end;
function TRWLock.GetNodeIndex(const ThreadID: TThreadID): Integer;
var
Node: TRWThreadNode;
begin
Result := 0;
while Result < RWNodeCount do
begin
Node := RWNode[Result];
if (Node.ThreadID = ThreadID) then
Exit;
Inc(Result);
end;
Result := -1;
end;
function TRWLock.GetRWNode(const Index: Integer): TRWThreadNode; // inline
begin
Result := TRWThreadNode(FWaiters.List[Index]);
end;
function TRWLock.GetRWNodeCount: Integer; // inline
begin
Result := FWaiters.Count;
end;
function TRWLock.AddRWNode(const AThreadID: TThreadID): TRWThreadNode;
begin
Result := TRWThreadNode.Create(AThreadID);
FWaiters.Add(Result);
end;
function TRWLock.GetRWThreadNode(const AThreadID: TThreadID): TRWThreadNode;
var
Idx: Integer;
begin
Idx := GetNodeIndex(AThreadID);
if Idx >= 0 then
Result := RWNode[Idx]
else
Result := AddRWNode(AThreadID);
end;
{$IFDEF RWLOCK_DEBUG}
procedure TRWLock.AddLockInfo(const ThreadID: TThreadID);
var
LockInfo: TLockInfo;
begin
LockInfo := TLockInfo.Create;
LockInfo.ThreadID := ThreadID;
LockInfo.EnterTime := Now;
if Assigned(FLockEvent) then
FLockEvent(Self, LockInfo);
FLockInfoList.Add(LockInfo);
end;
procedure TRWLock.DelLockInfo(const ThreadID: TThreadID);
var
I: Integer;
LockInfo: TLockInfo;
begin
for I := FLockInfoList.Count - 1 downto 0 do
begin
LockInfo := FLockInfoList[I];
if LockInfo.ThreadID = ThreadID then
begin
FLockInfoList.Delete(I);
Exit;
end;
end;
end;
function TRWLock.GetStatus: TRWThreadLockInfoList;
var
I: Integer;
begin
Result := TRWThreadLockInfoList.Create;
FLock.Enter;
for I := 0 to GetRWNodeCount - 1 do
Result.Add(GetRWNode(I).Clone);
FLock.Leave;
end;
function TRWLock.GetLockStack: TLockInfoList;
begin
FLock.Enter;
Result := FLockInfoList.Clone;
FLock.Leave;
end;
{$ENDIF}
function TRWLock.TryLock(const AState: TRWNodeState): Boolean;
var
ThreadNode: TRWThreadNode;
begin
Result := False;
FLock.Enter;
ThreadNode := GetRWThreadNode(TThread.CurrentThread.ThreadID);
case AState of
nsReader:
Result := not(HasActiveWriter(ThreadNode));
nsWriter:
begin
Result := not(HasActiveNode(ThreadNode));
if Result then
ThreadNode.State := nsWriter;
end;
end;
if Result then
begin
ThreadNode.IncAcquires;
{$IFDEF RWLOCK_DEBUG}
AddLockInfo(ThreadNode.ThreadID);
{$ENDIF}
end
else
begin
if ThreadNode.Acquires = 0 then
FWaiters.Remove(ThreadNode);
end;
FLock.Leave;
end;
procedure TRWLock.Lock(const AState: TRWNodeState);
var
ThreadNode: TRWThreadNode;
begin
FLock.Enter;
ThreadNode := GetRWThreadNode(TThread.CurrentThread.ThreadID);
ThreadNode.IsWait := True;
case AState of
nsReader:
begin
WaitForReader(ThreadNode);
end;
nsWriter:
begin
ThreadNode.State := nsWriter;
WaitForWriter(ThreadNode);
end;
end;
ThreadNode.IncAcquires;
ThreadNode.IsWait := False;
{$IFDEF RWLOCK_DEBUG}
AddLockInfo(ThreadNode.ThreadID);
{$ENDIF}
FLock.Leave;
end;
procedure TRWLock.WaitForReader(ThreadNode: TRWThreadNode);
begin
while HasActiveWriter(ThreadNode) do
Wait(ThreadNode);
end;
procedure TRWLock.WaitForWriter(ThreadNode: TRWThreadNode);
begin
while HasActiveNode(ThreadNode) do
Wait(ThreadNode);
end;
procedure TRWLock.WakeUpWaiters;
var
Idx: Integer;
begin
FLock.Enter;
for Idx := 0 to RWNodeCount - 1 do
RWNode[Idx].WakeUp;
FLock.Leave;
End;
procedure TRWLock.Wait(ThreadNode: TRWThreadNode);
begin
{$IFDEF RWLOCK_DEBUG}
if Assigned(FLockWaitEvent) then
FLockWaitEvent(Self);
{$ENDIF}
FLock.Leave;
ThreadNode.Wait;
FLock.Enter;
end;
procedure TRWLock.UnLock;
var
Idx: Integer;
ThreadNode: TRWThreadNode;
begin
FLock.Enter;
Idx := GetNodeIndex(TThread.CurrentThread.ThreadID);
if Idx >= 0 then
begin
ThreadNode := RWNode[Idx];
ThreadNode.DecAcquires;
if ThreadNode.Acquires = 0 then
FWaiters.Delete(Idx);
end;
{$IFDEF RWLOCK_DEBUG}
if Idx < 0 then
RaiseRWLockException('lock already released');
DelLockInfo(TThread.CurrentThread.ThreadID);
{$ENDIF}
FLock.Leave;
WakeUpWaiters;
end;
procedure TRWLock.RaiseRWLockException(const Msg: String);
begin
FLock.Leave;
raise ERWLockError.Create(Msg);
end;
{$IFDEF RWLOCK_DEBUG}
{ TRWThreadLockInfoList }
procedure TRWThreadLockInfoList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if Action = lnDeleted then
TObject(Ptr).Free;
end;
{ TLockInfoList }
function TLockInfoList.Clone: TLockInfoList;
var
I: Integer;
LockInfo: TLockInfo;
begin
Result := TLockInfoList.Create;
for I := 0 to Count - 1 do
begin
LockInfo := TLockInfo(Items[I]);
Result.Add(LockInfo.Clone);
end;
end;
procedure TLockInfoList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if Action = lnDeleted then
TObject(Ptr).Free;
end;
{ TLockInfo }
function TLockInfo.Clone: TLockInfo;
begin
Result := TLockInfo.Create;
Result.ThreadID := ThreadID;
Result.EnterTime := EnterTime;
Result.Stack := Stack;
end;
function TLockInfo.StackToStr: String;
var
I: Integer;
begin
Result := '';
for I := 0 to High(Stack) do
begin
if Stack[I] = Nil then
Break;
Result := Result + Format(' $%p', [Stack[I]])
end;
end;
{$ENDIF}
{ TWaitersList }
constructor TWaitersList.Create;
begin
inherited;
Capacity := 8;
end;
procedure TWaitersList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if Action = lnDeleted then
TObject(Ptr).Free;
end;
end.