1 | unit UPool;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Contnrs, syncobjs;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TThreadedPoolItem }
|
---|
13 |
|
---|
14 | TThreadedPoolItem = class
|
---|
15 | Used: Boolean;
|
---|
16 | Item: TObject;
|
---|
17 | constructor Create;
|
---|
18 | destructor Destroy; override;
|
---|
19 | end;
|
---|
20 |
|
---|
21 | { TThreadedPool }
|
---|
22 |
|
---|
23 | TThreadedPool = class(TObjectList)
|
---|
24 | private
|
---|
25 | Lock: TCriticalSection;
|
---|
26 | ReleaseEvent: TEvent;
|
---|
27 | FTotalCount: Integer;
|
---|
28 | function IndexOfObjectItem(Item: TObject): Integer;
|
---|
29 | procedure SetTotalCount(const AValue: Integer);
|
---|
30 | public
|
---|
31 | UsedCount: Integer;
|
---|
32 | function GetUnused: TThreadedPoolItem;
|
---|
33 | function Acquire: TObject;
|
---|
34 | procedure Release(Item: TObject);
|
---|
35 | constructor Create;
|
---|
36 | destructor Destroy; override;
|
---|
37 | property TotalCount: Integer read FTotalCount write SetTotalCount;
|
---|
38 | end;
|
---|
39 |
|
---|
40 | implementation
|
---|
41 |
|
---|
42 | { TThreadedPool }
|
---|
43 |
|
---|
44 | function TThreadedPool.GetUnused: TThreadedPoolItem;
|
---|
45 | var
|
---|
46 | I: Integer;
|
---|
47 | begin
|
---|
48 | I := 0;
|
---|
49 | while (I < Count) and (TThreadedPoolItem(Items[I]).Used) do
|
---|
50 | Inc(I);
|
---|
51 | if I < Count then Result := TThreadedPoolItem(Items[I])
|
---|
52 | else Result := nil;
|
---|
53 | end;
|
---|
54 |
|
---|
55 | function TThreadedPool.IndexOfObjectItem(Item: TObject): Integer;
|
---|
56 | var
|
---|
57 | I: Integer;
|
---|
58 | begin
|
---|
59 | I := 0;
|
---|
60 | while (I < Count) and (TThreadedPoolItem(Items[I]).Item <> Item) do
|
---|
61 | Inc(I);
|
---|
62 | if I < Count then Result := I
|
---|
63 | else Result := -1;
|
---|
64 | end;
|
---|
65 |
|
---|
66 | procedure TThreadedPool.SetTotalCount(const AValue: Integer);
|
---|
67 | var
|
---|
68 | I: Integer;
|
---|
69 | begin
|
---|
70 | if AValue > FTotalCount then begin
|
---|
71 | for I := FTotalCount to AValue - 1 do
|
---|
72 | Add(TThreadedPoolItem.Create);
|
---|
73 | end else
|
---|
74 | if AValue < FTotalCount then begin
|
---|
75 | for I := AValue to FTotalCount - 1 do
|
---|
76 | TThreadedPoolItem(Items[I]).Destroy;
|
---|
77 | end;
|
---|
78 | FTotalCount := AValue;
|
---|
79 | end;
|
---|
80 |
|
---|
81 | function TThreadedPool.Acquire: TObject;
|
---|
82 | var
|
---|
83 | Item: TThreadedPoolItem;
|
---|
84 | begin
|
---|
85 | try
|
---|
86 | Lock.Acquire;
|
---|
87 | while UsedCount = TotalCount do begin
|
---|
88 | Lock.Release;
|
---|
89 | ReleaseEvent.WaitFor(1000);
|
---|
90 | Lock.Acquire;
|
---|
91 | end;
|
---|
92 | Item := GetUnused;
|
---|
93 | Item.Used := True;
|
---|
94 | Result := Item.Item;
|
---|
95 | Inc(UsedCount);
|
---|
96 | finally
|
---|
97 | Lock.Release;
|
---|
98 | end;
|
---|
99 | end;
|
---|
100 |
|
---|
101 | procedure TThreadedPool.Release(Item: TObject);
|
---|
102 | var
|
---|
103 | Index: Integer;
|
---|
104 | begin
|
---|
105 | try
|
---|
106 | Lock.Acquire;
|
---|
107 | Index := IndexOfObjectItem(Item);
|
---|
108 | with TThreadedPoolItem(Items[Index]) do begin
|
---|
109 | Used := False;
|
---|
110 | end;
|
---|
111 | Dec(UsedCount);
|
---|
112 | if UsedCount < TotalCount then ReleaseEvent.SetEvent;
|
---|
113 | finally
|
---|
114 | Lock.Release;
|
---|
115 | end;
|
---|
116 | end;
|
---|
117 |
|
---|
118 | constructor TThreadedPool.Create;
|
---|
119 | begin
|
---|
120 | inherited;
|
---|
121 | Lock := TCriticalSection.Create;
|
---|
122 | ReleaseEvent := TEvent.Create(nil, False, False, '');
|
---|
123 | end;
|
---|
124 |
|
---|
125 | destructor TThreadedPool.Destroy;
|
---|
126 | begin
|
---|
127 | ReleaseEvent.Destroy;
|
---|
128 | Lock.Destroy;
|
---|
129 | inherited Destroy;
|
---|
130 | end;
|
---|
131 |
|
---|
132 | { TThreadedPoolItem }
|
---|
133 |
|
---|
134 | constructor TThreadedPoolItem.Create;
|
---|
135 | begin
|
---|
136 | Item := nil;
|
---|
137 | end;
|
---|
138 |
|
---|
139 | destructor TThreadedPoolItem.Destroy;
|
---|
140 | begin
|
---|
141 | if Assigned(Item) then Item.Destroy;
|
---|
142 | inherited Destroy;
|
---|
143 | end;
|
---|
144 |
|
---|
145 | end.
|
---|
146 |
|
---|