1 | unit UPool;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, syncobjs, Generics.Collections, UThreading;
|
---|
7 |
|
---|
8 | type
|
---|
9 | TPoolItemClass = class of TObject;
|
---|
10 |
|
---|
11 | { TPool }
|
---|
12 |
|
---|
13 | TPool = class
|
---|
14 | private
|
---|
15 | FReleaseEvent: TEvent;
|
---|
16 | function GetTotalCount: Integer;
|
---|
17 | function GetUsedCount: Integer; virtual;
|
---|
18 | procedure SetTotalCount(const AValue: Integer);
|
---|
19 | protected
|
---|
20 | function NewItemObject: TObject; virtual;
|
---|
21 | public
|
---|
22 | Items: TObjectList<TObject>;
|
---|
23 | FreeItems: TObjectList<TObject>;
|
---|
24 | function Acquire: TObject; virtual;
|
---|
25 | procedure Release(Item: TObject); virtual;
|
---|
26 | constructor Create; virtual;
|
---|
27 | destructor Destroy; override;
|
---|
28 | property TotalCount: Integer read GetTotalCount write SetTotalCount;
|
---|
29 | property UsedCount: Integer read GetUsedCount;
|
---|
30 | end;
|
---|
31 |
|
---|
32 | { TThreadedPool }
|
---|
33 |
|
---|
34 | TThreadedPool = class(TPool)
|
---|
35 | private
|
---|
36 | procedure SetTotalCount(const AValue: Integer);
|
---|
37 | function GetUsedCount: Integer; override;
|
---|
38 | public
|
---|
39 | Lock: TCriticalSection;
|
---|
40 | function Acquire: TObject; override;
|
---|
41 | procedure Release(Item: TObject); override;
|
---|
42 | constructor Create; override;
|
---|
43 | destructor Destroy; override;
|
---|
44 | end;
|
---|
45 |
|
---|
46 | resourcestring
|
---|
47 | SObjectPoolEmpty = 'Object pool is empty';
|
---|
48 | SReleaseError = 'Unknown object for release from pool';
|
---|
49 |
|
---|
50 |
|
---|
51 | implementation
|
---|
52 |
|
---|
53 | { TThreadedPool }
|
---|
54 |
|
---|
55 | procedure TThreadedPool.SetTotalCount(const AValue: Integer);
|
---|
56 | begin
|
---|
57 | try
|
---|
58 | Lock.Acquire;
|
---|
59 | inherited SetTotalCount(AValue);
|
---|
60 | finally
|
---|
61 | Lock.Release;
|
---|
62 | end;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | function TThreadedPool.GetUsedCount: Integer;
|
---|
66 | begin
|
---|
67 | try
|
---|
68 | Lock.Acquire;
|
---|
69 | Result := inherited GetUsedCount;
|
---|
70 | finally
|
---|
71 | Lock.Release;
|
---|
72 | end;
|
---|
73 | end;
|
---|
74 |
|
---|
75 | function TThreadedPool.Acquire: TObject;
|
---|
76 | begin
|
---|
77 | try
|
---|
78 | Lock.Acquire;
|
---|
79 | if Items.Count = 0 then
|
---|
80 | raise Exception.Create(SObjectPoolEmpty);
|
---|
81 | while FreeItems.Count = 0 do begin
|
---|
82 | try
|
---|
83 | Lock.Release;
|
---|
84 | //FReleaseEvent.WaitFor(1);
|
---|
85 | Sleep(1);
|
---|
86 | finally
|
---|
87 | Lock.Acquire;
|
---|
88 | end;
|
---|
89 | end;
|
---|
90 | Result := inherited Acquire;
|
---|
91 | finally
|
---|
92 | Lock.Release;
|
---|
93 | end;
|
---|
94 | end;
|
---|
95 |
|
---|
96 | procedure TThreadedPool.Release(Item: TObject);
|
---|
97 | begin
|
---|
98 | try
|
---|
99 | Lock.Acquire;
|
---|
100 | inherited Release(Item);
|
---|
101 | finally
|
---|
102 | Lock.Release;
|
---|
103 | end;
|
---|
104 | end;
|
---|
105 |
|
---|
106 | constructor TThreadedPool.Create;
|
---|
107 | begin
|
---|
108 | inherited Create;
|
---|
109 | Lock := TCriticalSection.Create;
|
---|
110 | end;
|
---|
111 |
|
---|
112 | destructor TThreadedPool.Destroy;
|
---|
113 | begin
|
---|
114 | TotalCount := 0;
|
---|
115 | Lock.Free;
|
---|
116 | inherited Destroy;
|
---|
117 | end;
|
---|
118 |
|
---|
119 | { TPool }
|
---|
120 |
|
---|
121 | function TPool.NewItemObject: TObject;
|
---|
122 | begin
|
---|
123 | // Overload this method in descendand classes for automatic creation propert item class
|
---|
124 | Result := TObject.Create;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | function TPool.GetUsedCount: Integer;
|
---|
128 | begin
|
---|
129 | Result := Items.Count - FreeItems.Count;
|
---|
130 | end;
|
---|
131 |
|
---|
132 | function TPool.GetTotalCount: Integer;
|
---|
133 | begin
|
---|
134 | Result := Items.Count;
|
---|
135 | end;
|
---|
136 |
|
---|
137 | procedure TPool.SetTotalCount(const AValue: Integer);
|
---|
138 | var
|
---|
139 | I: Integer;
|
---|
140 | begin
|
---|
141 | if AValue > Items.Count then begin
|
---|
142 | for I := Items.Count to AValue - 1 do begin
|
---|
143 | Items.Add(NewItemObject);
|
---|
144 | FreeItems.Add(Items.Last);
|
---|
145 | end;
|
---|
146 | end else
|
---|
147 | if AValue < Items.Count then begin
|
---|
148 | for I := Items.Count - 1 downto AValue do begin
|
---|
149 | while FreeItems.IndexOf(Items[I]) = -1 do
|
---|
150 | Sleep(1);
|
---|
151 | FreeItems.Delete(FreeItems.IndexOf(Items[I]));
|
---|
152 | Items.Delete(I);
|
---|
153 | end;
|
---|
154 | end;
|
---|
155 | end;
|
---|
156 |
|
---|
157 | function TPool.Acquire: TObject;
|
---|
158 | begin
|
---|
159 | while FreeItems.Count = 0 do begin
|
---|
160 | //FReleaseEvent.WaitFor(1);
|
---|
161 | Sleep(1);
|
---|
162 | end;
|
---|
163 | Result := FreeItems.Last;
|
---|
164 | FreeItems.Count := FreeItems.Count - 1;
|
---|
165 | //if not Assigned(Item) then
|
---|
166 | // raise Exception.Create(Format(S);
|
---|
167 | end;
|
---|
168 |
|
---|
169 | procedure TPool.Release(Item: TObject);
|
---|
170 | var
|
---|
171 | Index: Integer;
|
---|
172 | begin
|
---|
173 | Index := Items.IndexOf(Item);
|
---|
174 | if Index = -1 then
|
---|
175 | raise Exception.Create(SReleaseError);
|
---|
176 |
|
---|
177 | FreeItems.Add(Item);
|
---|
178 | //if FUsedCount < TotalCount then
|
---|
179 | // FReleaseEvent.SetEvent;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | constructor TPool.Create;
|
---|
183 | begin
|
---|
184 | inherited;
|
---|
185 | Items := TObjectList<TObject>.Create;
|
---|
186 | FreeItems := TObjectList<TObject>.Create;
|
---|
187 | FreeItems.OwnsObjects := False;
|
---|
188 | FReleaseEvent := TEvent.Create(nil, False, False, '');
|
---|
189 | end;
|
---|
190 |
|
---|
191 | destructor TPool.Destroy;
|
---|
192 | begin
|
---|
193 | TotalCount := 0;
|
---|
194 | FreeAndNil(FReleaseEvent);
|
---|
195 | FreeAndNil(FreeItems);
|
---|
196 | FreeAndNil(Items);
|
---|
197 | inherited;
|
---|
198 | end;
|
---|
199 |
|
---|
200 | end.
|
---|
201 |
|
---|