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 | procedure Assign(Source: TSyncCounter);
|
---|
26 | end;
|
---|
27 |
|
---|
28 | implementation
|
---|
29 |
|
---|
30 | { TSyncCounter }
|
---|
31 |
|
---|
32 | function TSyncCounter.Allocate: Integer;
|
---|
33 | begin
|
---|
34 | try
|
---|
35 | Lock.Acquire;
|
---|
36 | Result := Top;
|
---|
37 | Inc(Top);
|
---|
38 | finally
|
---|
39 | Lock.Release;
|
---|
40 | end;
|
---|
41 | end;
|
---|
42 |
|
---|
43 | function TSyncCounter.CurrentEqualTo(AValue: Integer): Boolean;
|
---|
44 | begin
|
---|
45 | try
|
---|
46 | Lock.Acquire;
|
---|
47 | Result := AValue = Current;
|
---|
48 | finally
|
---|
49 | Lock.Release;
|
---|
50 | end;
|
---|
51 | end;
|
---|
52 |
|
---|
53 | procedure TSyncCounter.Increment;
|
---|
54 | begin
|
---|
55 | try
|
---|
56 | Lock.Acquire;
|
---|
57 | Inc(Current);
|
---|
58 | finally
|
---|
59 | Lock.Release;
|
---|
60 | end;
|
---|
61 | end;
|
---|
62 |
|
---|
63 | constructor TSyncCounter.Create;
|
---|
64 | begin
|
---|
65 | Lock := TCriticalSection.Create;
|
---|
66 | end;
|
---|
67 |
|
---|
68 | destructor TSyncCounter.Destroy;
|
---|
69 | begin
|
---|
70 | Lock.Free;
|
---|
71 | inherited Destroy;
|
---|
72 | end;
|
---|
73 |
|
---|
74 | procedure TSyncCounter.Assign(Source: TSyncCounter);
|
---|
75 | begin
|
---|
76 | Current := Source.Current;
|
---|
77 | Top := Source.Top;
|
---|
78 | end;
|
---|
79 |
|
---|
80 | end.
|
---|
81 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.