-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitBlockList.pas
108 lines (87 loc) · 1.96 KB
/
UnitBlockList.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
unit UnitBlockList;
interface
uses
Windows, SysUtils, Classes, SyncObjs,
UnitThreadManager;
type
TBlockList = class
private
FList: TList;
FLockCS: TCriticalSection;
FLastNumber: integer;
FFileNameTemplate: string;
function GetBlockNumber(Index: integer): integer; // ïîñëåäíèé èñïîëüçîâàííûé èíäåêñ äëÿ ãåíåðàöèè èìåíè ôàéëà
public
constructor Create(const TemplateFileName: string);
destructor Destroy; override;
procedure Lock;
procedure UnLock;
function NextBlockNumber: integer;
function Add(const Thread: TProtoThread): string;
procedure Delete;
property BlockNumber[Index: integer]: integer
read GetBlockNumber;
function Count: integer;
function FileName(const Number: integer): string;
end;
var
Blocks: TBlockList;
implementation
{ TBlockList }
function TBlockList.Add(const Thread: TProtoThread): string;
begin
Result := Format(FFileNameTemplate, [Thread.BlockIndex]);
Lock;
try
FList.Add(pointer(Thread.BlockIndex));
finally
UnLock;
end;
end;
function TBlockList.Count: integer;
begin
Result := FList.Count;
end;
constructor TBlockList.Create(const TemplateFileName: string);
begin
FList := TList.Create;
FLockCS := TCriticalSection.Create;
FFileNameTemplate := ChangeFileExt(TemplateFileName, '.%.7d');
FLastNumber := 0;
end;
procedure TBlockList.Delete;
begin
Lock;
try
FList.Delete(0);
finally
Unlock;
end;
end;
destructor TBlockList.Destroy;
begin
FLockCS.Free;
FList.Free;
inherited;
end;
function TBlockList.FileName(const Number: integer): string;
begin
Result := Format(FFileNameTemplate, [Number])
end;
function TBlockList.GetBlockNumber(Index: integer): integer;
begin
Result := integer(FList[Index]);
end;
procedure TBlockList.Lock;
begin
FLockCS.Enter;
end;
function TBlockList.NextBlockNumber: integer;
begin
Result := InterlockedIncrement(FLastNumber);
end;
procedure TBlockList.UnLock;
begin
FLockCS.Leave;
end;
end.