Line | |
---|
1 | unit SyncCounter;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, SyncObjs;
|
---|
7 |
|
---|
8 | type
|
---|
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 |
|
---|
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;
|
---|
71 | end;
|
---|
72 |
|
---|
73 | procedure TSyncCounter.Assign(Source: TSyncCounter);
|
---|
74 | begin
|
---|
75 | Current := Source.Current;
|
---|
76 | Top := Source.Top;
|
---|
77 | end;
|
---|
78 |
|
---|
79 | end.
|
---|
Note:
See
TracBrowser
for help on using the repository browser.