source: trunk/Packages/Common/SyncCounter.pas

Last change on this file was 30, checked in by chronos, 6 months ago
  • Modified: Updated Common package.
File size: 1.2 KB
Line 
1unit SyncCounter;
2
3interface
4
5uses
6 Classes, SysUtils, SyncObjs;
7
8type
9
10 { TSyncCounter }
11
12 TSyncCounter = class
13 private
14 Top: Integer;
15 Current: Integer;
16 public
17 Lock: TCriticalSection;
18 function Allocate: Integer;
19 function CurrentEqualTo(AValue: Integer): Boolean;
20 procedure Increment;
21 constructor Create;
22 destructor Destroy; override;
23 procedure Assign(Source: TSyncCounter);
24 end;
25
26
27implementation
28
29{ TSyncCounter }
30
31function TSyncCounter.Allocate: Integer;
32begin
33 try
34 Lock.Acquire;
35 Result := Top;
36 Inc(Top);
37 finally
38 Lock.Release;
39 end;
40end;
41
42function TSyncCounter.CurrentEqualTo(AValue: Integer): Boolean;
43begin
44 try
45 Lock.Acquire;
46 Result := AValue = Current;
47 finally
48 Lock.Release;
49 end;
50end;
51
52procedure TSyncCounter.Increment;
53begin
54 try
55 Lock.Acquire;
56 Inc(Current);
57 finally
58 Lock.Release;
59 end;
60end;
61
62constructor TSyncCounter.Create;
63begin
64 Lock := TCriticalSection.Create;
65end;
66
67destructor TSyncCounter.Destroy;
68begin
69 Lock.Free;
70 inherited;
71end;
72
73procedure TSyncCounter.Assign(Source: TSyncCounter);
74begin
75 Current := Source.Current;
76 Top := Source.Top;
77end;
78
79end.
Note: See TracBrowser for help on using the repository browser.