@@ -96,22 +96,17 @@ internal interface IMessageSource
96
96
internal abstract class HostEnvironmentBase < TEnv > : ChannelProviderBase , IHostEnvironment , IChannelProvider
97
97
where TEnv : HostEnvironmentBase < TEnv >
98
98
{
99
- public bool IsCanceled { get ; protected set ; }
100
-
101
- private readonly List < IHost > _hosts ;
102
-
103
- private readonly object _cancelEnvLock ;
104
-
105
99
[ BestFriend ]
106
100
internal void CancelExecutionHosts ( )
107
101
{
108
- lock ( _cancelEnvLock )
102
+ lock ( _cancelLock )
109
103
{
110
- foreach ( var host in _hosts )
111
- if ( host is ICancelableHost )
112
- ( ( ICancelableHost ) host ) . CancelExecution ( ) ;
104
+ foreach ( var child in _children )
105
+ if ( child . TryGetTarget ( out IHost host ) )
106
+ if ( host is ICancelableHost cancelableHost )
107
+ cancelableHost . CancelExecution ( ) ;
113
108
114
- _hosts . Clear ( ) ;
109
+ _children . Clear ( ) ;
115
110
}
116
111
}
117
112
@@ -126,14 +121,10 @@ public abstract class HostBase : HostEnvironmentBase<TEnv>, ICancelableHost
126
121
127
122
public Random Rand => _rand ;
128
123
129
- // We don't have dispose mechanism for hosts, so to let GC collect children hosts we make them WeakReference.
130
- private readonly List < WeakReference < IHost > > _children ;
131
-
132
124
public HostBase ( HostEnvironmentBase < TEnv > source , string shortName , string parentFullName , Random rand , bool verbose )
133
125
: base ( source , rand , verbose , shortName , parentFullName )
134
126
{
135
127
Depth = source . Depth + 1 ;
136
- _children = new List < WeakReference < IHost > > ( ) ;
137
128
}
138
129
139
130
public void CancelExecution ( )
@@ -195,7 +186,7 @@ protected PipeBase(ChannelProviderBase parent, string shortName,
195
186
196
187
public void Dispose ( )
197
188
{
198
- if ( ! _disposed )
189
+ if ( ! _disposed )
199
190
{
200
191
Dispose ( true ) ;
201
192
_disposed = true ;
@@ -363,6 +354,11 @@ public void RemoveListener(Action<IMessageSource, TMessage> listenerFunc)
363
354
364
355
public override int Depth => 0 ;
365
356
357
+ public bool IsCanceled { get ; protected set ; }
358
+
359
+ // We don't have dispose mechanism for hosts, so to let GC collect children hosts we make them WeakReference.
360
+ private readonly List < WeakReference < IHost > > _children ;
361
+
366
362
/// <summary>
367
363
/// The main constructor.
368
364
/// </summary>
@@ -375,10 +371,9 @@ protected HostEnvironmentBase(Random rand, bool verbose,
375
371
ListenerDict = new ConcurrentDictionary < Type , Dispatcher > ( ) ;
376
372
ProgressTracker = new ProgressReporting . ProgressTracker ( this ) ;
377
373
_cancelLock = new object ( ) ;
378
- _cancelEnvLock = new object ( ) ;
379
374
Root = this as TEnv ;
380
375
ComponentCatalog = new ComponentCatalog ( ) ;
381
- _hosts = new List < IHost > ( ) ;
376
+ _children = new List < WeakReference < IHost > > ( ) ;
382
377
}
383
378
384
379
/// <summary>
@@ -392,26 +387,25 @@ protected HostEnvironmentBase(HostEnvironmentBase<TEnv> source, Random rand, boo
392
387
Contracts . CheckValueOrNull ( rand ) ;
393
388
_rand = rand ?? RandomUtils . Create ( ) ;
394
389
_cancelLock = new object ( ) ;
395
- _cancelEnvLock = new object ( ) ;
396
- _hosts = new List < IHost > ( ) ;
397
390
398
391
// This fork shares some stuff with the master.
399
392
Master = source ;
400
393
Root = source . Root ;
401
394
ListenerDict = source . ListenerDict ;
402
395
ProgressTracker = source . ProgressTracker ;
403
396
ComponentCatalog = source . ComponentCatalog ;
397
+ _children = new List < WeakReference < IHost > > ( ) ;
404
398
}
405
399
406
400
public IHost Register ( string name , int ? seed = null , bool ? verbose = null )
407
401
{
408
402
Contracts . CheckNonEmpty ( name , nameof ( name ) ) ;
409
403
IHost host ;
410
- lock ( _cancelEnvLock )
404
+ lock ( _cancelLock )
411
405
{
412
406
Random rand = ( seed . HasValue ) ? RandomUtils . Create ( seed . Value ) : RandomUtils . Create ( _rand ) ;
413
407
host = RegisterCore ( this , name , Master ? . FullName , rand , verbose ?? Verbose ) ;
414
- _hosts . Add ( host ) ;
408
+ _children . Add ( new WeakReference < IHost > ( host ) ) ;
415
409
}
416
410
return host ;
417
411
}
0 commit comments