source: trunk/Demo/Packages/Common/USyncCounter.pas

Last change on this file was 60, checked in by chronos, 12 years ago
File size: 1.1 KB
Line 
1unit USyncCounter;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, SyncObjs;
9
10type
11
12 { TSyncCounter }
13
14 TSyncCounter = class
15 private
16 Top: Integer;
17 Current: Integer;
18 public
19 Lock: TCriticalSection;
20 function Allocate: Integer;
21 function CurrentEqualTo(AValue: Integer): Boolean;
22 procedure Increment;
23 constructor Create;
24 destructor Destroy; override;
25 end;
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 Destroy;
71end;
72
73end.
74
Note: See TracBrowser for help on using the repository browser.