My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
///<summary>Locking/blocking collections. Part of the OmniThreadLibrary project.</summary>
///<author>Primoz Gabrijelcic</author>
///<license>
///This software is distributed under the BSD license.
///
///Copyright (c) 2009 Primoz Gabrijelcic
///All rights reserved.
///
///Redistribution and use in source and binary forms, with or without modification,
///are permitted provided that the following conditions are met:
///- Redistributions of source code must retain the above copyright notice, this
/// list of conditions and the following disclaimer.
///- Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
///- The name of the Primoz Gabrijelcic may not be used to endorse or promote
/// products derived from this software without specific prior written permission.
///
///THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
///ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
///WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
///DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
///ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
///(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
///LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
///ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
///(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
///SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
///</license>
///<remarks><para>
/// Author : Primoz Gabrijelcic
/// Creation date : 2009-12-27
/// Last modification : 2010-01-14
/// Version : 1.02
///</para><para>
/// History:
/// release-1.05a/1.02a: 2010-07-21
/// - TOmniBlockingCollection.TryTake was broken. When two threads were waiting in
/// TryTake at the same time, first thread to complete the wait would remove
/// observer from the queue and that would cause next Add *not* to wake the other
/// waiting thread.
/// 1.02: 2010-01-14
/// - Small changes required for the interoperability with the Parallel.ForEach.
/// 1.01a: 2010-01-05
/// - Better behaviour when running on a single core.
/// 1.01: 2009-12-30
/// - Number of producer/consumers can be passed to TOmniBlockingCollection
/// constructor. TryTake will then detect the deadlock state when all producer/
/// consumers are inside the TryTake and the collection is empty and will
/// automatically complete the collection.
/// 1.0: 2009-12-29
/// - Released.
///</para></remarks>

unit OtlCollections;

interface

uses
Windows,
SysUtils,
DSiWin32,
GpStuff,
OtlCommon,
OtlContainers,
OtlContainerObserver,
OtlSync;

type
ECollectionCompleted = class(Exception);

///<summary>Blocking collection
///- http://blogs.msdn.com/pfxteam/archive/2009/11/06/9918363.aspx
///- http://msdn.microsoft.com/en-us/library/dd267312(VS.100).aspx
///</summary>
IOmniBlockingCollection = interface(IGpTraceable) ['{208EFA15-1F8F-4885-A509-B00191145D38}']
procedure Add(const value: TOmniValue);
procedure CompleteAdding;
function GetEnumerator: IOmniValueEnumerator;
function IsCompleted: boolean;
function Take(var value: TOmniValue): boolean;
function TryAdd(const value: TOmniValue): boolean;
function TryTake(var value: TOmniValue; timeout_ms: cardinal = 0): boolean;
end; { IOmniBlockingCollection }

TOmniBlockingCollection = class(TGpTraceable,
IOmniBlockingCollection,
IOmniValueEnumerable)
strict private
obcCollection : TOmniQueue;
obcCompleted : boolean;
obcCompletedSignal: TDSiEventHandle;
obcObserver : TOmniContainerWindowsEventObserver;
obcResourceCount : TOmniResourceCount;
public
constructor Create(numProducersConsumers: integer = 0);
destructor Destroy; override;
procedure Add(const value: TOmniValue); inline;
procedure CompleteAdding; inline;
function GetEnumerator: IOmniValueEnumerator; inline;
function IsCompleted: boolean; inline;
function Take(var value: TOmniValue): boolean; inline;
function TryAdd(const value: TOmniValue): boolean; inline;
function TryTake(var value: TOmniValue; timeout_ms: cardinal = 0): boolean;
property CompletedSignal: THandle read obcCompletedSignal;
end; { TOmniBlockingCollection }

TOmniBlockingCollectionEnumerator = class(TInterfacedObject,
IOmniValueEnumerator)
strict private
obceCollection_ref: TOmniBlockingCollection;
obceValue : TOmniValue;
public
constructor Create(collection: TOmniBlockingCollection);
function GetCurrent: TOmniValue; inline;
function MoveNext: boolean; inline;
function Take(var value: TOmniValue): boolean;
property Current: TOmniValue read GetCurrent;
end; { TOmniBlockingCollectionEnumerator }

implementation

uses
Classes;

{ TOmniBlockingCollectionEnumerator }

constructor TOmniBlockingCollectionEnumerator.Create(collection: TOmniBlockingCollection);
begin
obceCollection_ref := collection;
end; { TOmniBlockingCollectionEnumerator.Create }

function TOmniBlockingCollectionEnumerator.GetCurrent: TOmniValue;
begin
Result := obceValue;
end; { TOmniBlockingCollectionEnumerator.GetCurrent }

function TOmniBlockingCollectionEnumerator.MoveNext: boolean;
begin
Result := obceCollection_ref.Take(obceValue);
end; { TOmniBlockingCollectionEnumerator.MoveNext }

function TOmniBlockingCollectionEnumerator.Take(var value: TOmniValue): boolean;
begin
Result := MoveNext;
if Result then
value := obceValue;
end; { TOmniBlockingCollectionEnumerator.Take }

{ TOmniBlockingCollection }

///If numProducersConsumers > 0, collection will automatically enter 'completed' state
///when this number of .Take calls is simultaneously blocked because the collection is
///empty.
constructor TOmniBlockingCollection.Create(numProducersConsumers: integer);
begin
inherited Create;
if numProducersConsumers > 0 then
obcResourceCount := TOmniResourceCount.Create(numProducersConsumers);
obcCollection := TOmniQueue.Create;
obcCompletedSignal := CreateEvent(nil, true, false, nil);
obcObserver := CreateContainerWindowsEventObserver;
obcCollection.ContainerSubject.Attach(obcObserver, coiNotifyOnAllInserts);
end; { TOmniBlockingCollection.Create }

destructor TOmniBlockingCollection.Destroy;
begin
if assigned(obcCollection) and assigned(obcObserver) then
obcCollection.ContainerSubject.Detach(obcObserver, coiNotifyOnAllInserts);
FreeAndNil(obcObserver);
DSiCloseHandleAndNull(obcCompletedSignal);
FreeAndNil(obcCollection);
FreeAndNil(obcResourceCount);
inherited Destroy;
end; { TOmniBlockingCollection.Destroy }

procedure TOmniBlockingCollection.Add(const value: TOmniValue);
begin
if not TryAdd(value) then
raise ECollectionCompleted.Create('Adding to completed collection');
end; { TOmniBlockingCollection.Add }

procedure TOmniBlockingCollection.CompleteAdding;
begin
if not obcCompleted then begin
obcCompleted := true;
Win32Check(SetEvent(obcCompletedSignal));
end;
end; { TOmniBlockingCollection.CompleteAdding }

function TOmniBlockingCollection.GetEnumerator: IOmniValueEnumerator;
begin
Result := TOmniBlockingCollectionEnumerator.Create(Self);
end; { TOmniBlockingCollection.GetEnumerator }

function TOmniBlockingCollection.IsCompleted: boolean;
begin
Result := obcCompleted;
end; { TOmniBlockingCollection.IsCompleted }

function TOmniBlockingCollection.Take(var value: TOmniValue): boolean;
begin
Result := TryTake(value, INFINITE);
end; { TOmniBlockingCollection.Take }

function TOmniBlockingCollection.TryAdd(const value: TOmniValue): boolean;
begin
// CompleteAdding and TryAdd are not synchronised
Result := not obcCompleted;
if Result then
obcCollection.Enqueue(value);
end; { TOmniBlockingCollection.TryAdd }

function TOmniBlockingCollection.TryTake(var value: TOmniValue;
timeout_ms: cardinal): boolean;
var
awaited : DWORD;
startTime : int64;
waitHandles: array [0..2] of THandle;

function Elapsed: boolean;
begin
if timeout_ms = INFINITE then
Result := false
else
Result := (startTime + timeout_ms) < DSiTimeGetTime64;
end; { Elapsed }

function TimeLeft_ms: DWORD;
var
intTime: integer;
begin
if timeout_ms = INFINITE then
Result := INFINITE
else begin
intTime := startTime + timeout_ms - DSiTimeGetTime64;
if intTime < 0 then
Result := 0
else
Result := intTime;
end;
end; { TimeLeft }

begin { TOmniBlockingCollection.TryTake }
if obcCollection.TryDequeue(value) then
Result := true
else if IsCompleted or (timeout_ms = 0) then
Result := false
else begin
if assigned(obcResourceCount) then
obcResourceCount.Allocate;
try
startTime := DSiTimeGetTime64;
waitHandles[0] := obcCompletedSignal;
waitHandles[1] := obcObserver.GetEvent;
if assigned(obcResourceCount) then
waitHandles[2] := obcResourceCount.Handle;
Result := false;
while not (IsCompleted or Elapsed) do begin
if obcCollection.TryDequeue(value) then begin
Result := true;
break; //while
end;
awaited := WaitForMultipleObjects(IFF(assigned(obcResourceCount), 3, 2),
@waitHandles, false, TimeLeft_ms);
if awaited <> WAIT_OBJECT_1 then begin
if awaited = WAIT_OBJECT_2 then
CompleteAdding;
Result := false;
break; //while
end;
end;
finally
if assigned(obcResourceCount) then
obcResourceCount.Release;
end;
end;
end; { TOmniBlockingCollection.TryTake }

end.

Change log

r790 by gabr42 on Jul 21, 2010   Diff
! TOmniBlockingCollection.TryTake was
broken. When two threads were waiting in
TryTake at the same time, first thread to
complete the wait would remove observer
from the queue and that would cause next
Add *not* to wake the other waiting
thread.
Go to: 
Project members, sign in to write a code review

Older revisions

r664 by pri...@gabrijelcic.org on Mar 8, 2010   Diff
* bugfix release
r649 by pri...@gabrijelcic.org on Feb 22, 2010   Diff
+ smallish code reformat
r584 by pri...@gabrijelcic.org on Jan 14, 2010   Diff
+ First working implementation of the
Parallel.ForEach.
+ blocking collection: Small changes
required for the interoperability with
the Parallel.ForEach.
...
All revisions of this file

File info

Size: 10388 bytes, 281 lines
Powered by Google Project Hosting