Line | |
---|
1 | unit USyncCounter;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, SyncObjs;
|
---|
9 |
|
---|
10 | type
|
---|
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 |
|
---|
27 | implementation
|
---|
28 |
|
---|
29 | { TSyncCounter }
|
---|
30 |
|
---|
31 | function TSyncCounter.Allocate: Integer;
|
---|
32 | begin
|
---|
33 | try
|
---|
34 | Lock.Acquire;
|
---|
35 | Result := Top;
|
---|
36 | Inc(Top);
|
---|
37 | finally
|
---|
38 | Lock.Release;
|
---|
39 | end;
|
---|
40 | end;
|
---|
41 |
|
---|
42 | function TSyncCounter.CurrentEqualTo(AValue: Integer): Boolean;
|
---|
43 | begin
|
---|
44 | try
|
---|
45 | Lock.Acquire;
|
---|
46 | Result := AValue = Current;
|
---|
47 | finally
|
---|
48 | Lock.Release;
|
---|
49 | end;
|
---|
50 | end;
|
---|
51 |
|
---|
52 | procedure TSyncCounter.Increment;
|
---|
53 | begin
|
---|
54 | try
|
---|
55 | Lock.Acquire;
|
---|
56 | Inc(Current);
|
---|
57 | finally
|
---|
58 | Lock.Release;
|
---|
59 | end;
|
---|
60 | end;
|
---|
61 |
|
---|
62 | constructor TSyncCounter.Create;
|
---|
63 | begin
|
---|
64 | Lock := TCriticalSection.Create;
|
---|
65 | end;
|
---|
66 |
|
---|
67 | destructor TSyncCounter.Destroy;
|
---|
68 | begin
|
---|
69 | Lock.Free;
|
---|
70 | inherited Destroy;
|
---|
71 | end;
|
---|
72 |
|
---|
73 | end.
|
---|
74 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.