@@ -5,6 +5,7 @@ import System.Diagnostics
5
5
import System.Collections.Concurrent
6
6
import System.Threading
7
7
import System.Threading.Tasks
8
+ import System.Threading.Channels
8
9
9
10
10
11
namespace Harmony.Core.Utility
@@ -14,7 +15,21 @@ namespace Harmony.Core.Utility
14
15
public virtual method Dispose, void
15
16
endparams
16
17
proc
17
- throw new System.NotImplementedException()
18
+ _channel.Writer.Complete()
19
+
20
+ ;; Clear made items
21
+ try
22
+ begin
23
+ Monitor.Enter(_madeItems)
24
+ begin
25
+ _madeItems.Clear()
26
+ end
27
+ end
28
+ finally
29
+ begin
30
+ Monitor.Exit(_madeItems)
31
+ end
32
+ endtry
18
33
endmethod
19
34
20
35
private _factory, @Func<IServiceProvider, T>
@@ -23,7 +38,7 @@ namespace Harmony.Core.Utility
23
38
private _maxCreated, int
24
39
private _pruneAbove, int
25
40
protected _madeItems, @List<ItemPlaceHolder>
26
- protected _availableItems , @BlockingCollection <T>
41
+ protected _channel , @Channel <T>
27
42
private _waitDuration, TimeSpan
28
43
protected readwrite property Init, @Action<T, IServiceProvider>
29
44
protected readwrite property Recycle, @Func<T, Task>
@@ -48,33 +63,16 @@ namespace Harmony.Core.Utility
48
63
_maxCreated = maxCreated
49
64
_pruneAbove = pruneAbove
50
65
_waitDuration = waitDuration
51
- _availableItems = new BlockingCollection <T>()
66
+ _channel = System.Threading.Channels.Channel.CreateUnbounded <T>()
52
67
_madeItems = new List<ItemPlaceHolder>()
53
68
endmethod
54
69
55
- private class AsyncTakeHelper
56
- public Token, @CancellationToken
57
- public Items, @BlockingCollection<T>
58
- public method Take, T
59
- proc
60
- mreturn Items.Take(Token)
61
- endmethod
62
- endclass
63
-
64
-
65
- protected method TakeAsync, @Task<T>
66
- token, @CancellationToken
67
- proc
68
- data helper = new AsyncTakeHelper() { Token = token, Items = _availableItems }
69
- mreturn Task.Run(helper.Take, token)
70
- endmethod
71
-
72
70
protected method CreateFast, T
73
71
serviceProvider, @IServiceProvider
74
72
proc
75
73
data result, @ItemPlaceHolder, ^null
76
74
data avilableResult, T
77
- if (_availableItems.TryTake (avilableResult))
75
+ if (_channel.Reader.TryRead (avilableResult))
78
76
begin
79
77
if(this.Init != ^null)
80
78
this.Init(avilableResult, serviceProvider)
@@ -154,7 +152,7 @@ namespace Harmony.Core.Utility
154
152
data tokenSource = new CancellationTokenSource(_waitDuration)
155
153
token = tokenSource.Token
156
154
end
157
- data resultObj = await TakeAsync (token)
155
+ data resultObj = await _channel.Reader.ReadAsync (token)
158
156
159
157
if (this.Init != ^null)
160
158
this.Init(resultObj, serviceProvider)
@@ -185,42 +183,26 @@ namespace Harmony.Core.Utility
185
183
obj, T
186
184
endparams
187
185
proc
188
- if (_availableItems.Count >= _pruneAbove) then
189
- begin
190
- try
191
- begin
192
- RemoveFromMadeItems(obj)
193
- FinalDestroyItem(obj)
194
- end
195
- catch (ex, @Exception)
196
- begin
197
- ;; nothing left to try here
198
- Trace.TraceError(ex.ToString())
199
- end
200
- endtry
201
- end
202
- else
186
+ try
203
187
begin
204
- try
188
+ if(CanRecycle == ^null || CanRecycle(obj)) then
205
189
begin
206
- if(CanRecycle == ^null || CanRecycle(obj)) then
207
- begin
208
- RecycleItem(obj)
209
- end
210
- else
211
- begin
212
- RemoveFromMadeItems(obj)
213
- FinalDestroyItem(obj)
214
- end
190
+ RecycleItem(obj)
215
191
end
216
- catch (ex, @Exception)
192
+ else
217
193
begin
218
194
RemoveFromMadeItems(obj)
219
195
FinalDestroyItem(obj)
220
- Trace.TraceError(ex.ToString())
221
196
end
222
- endtry
223
197
end
198
+ catch (ex, @Exception)
199
+ begin
200
+ RemoveFromMadeItems(obj)
201
+ FinalDestroyItem(obj)
202
+ Trace.TraceError(ex.ToString())
203
+ end
204
+ endtry
205
+
224
206
mreturn false
225
207
endmethod
226
208
@@ -234,7 +216,7 @@ namespace Harmony.Core.Utility
234
216
begin
235
217
await Recycle(obj)
236
218
end
237
- _availableItems.Add (obj)
219
+ await _channel.Writer.WriteAsync (obj)
238
220
end
239
221
;;async void method - gotta catch em all
240
222
catch(ex, @Exception)
@@ -285,6 +267,6 @@ namespace Harmony.Core.Utility
285
267
endtry
286
268
endmethod
287
269
288
- endclass
270
+ endclass
289
271
290
272
endnamespace
0 commit comments