source: trunk/Packages/Common/USyncCounter.pas

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