|
12 | 12 | ===========================================================*/
|
13 | 13 |
|
14 | 14 | using System.Diagnostics;
|
| 15 | +using System.Runtime.CompilerServices; |
15 | 16 | using System.Runtime.ExceptionServices;
|
16 | 17 | using System.Runtime.Serialization;
|
17 | 18 |
|
@@ -136,6 +137,11 @@ internal static void RunInternal(ExecutionContext executionContext, ContextCallb
|
136 | 137 | Thread currentThread0 = Thread.CurrentThread;
|
137 | 138 | Thread currentThread = currentThread0;
|
138 | 139 | ExecutionContext previousExecutionCtx0 = currentThread0.ExecutionContext;
|
| 140 | + if (previousExecutionCtx0 != null && previousExecutionCtx0.m_isDefault) |
| 141 | + { |
| 142 | + // Default is a null ExecutionContext internally |
| 143 | + previousExecutionCtx0 = null; |
| 144 | + } |
139 | 145 |
|
140 | 146 | // Store current ExecutionContext and SynchronizationContext as "previousXxx".
|
141 | 147 | // This allows us to restore them and undo any Context changes made in callback.Invoke
|
@@ -215,6 +221,11 @@ internal static void RunInternal<TState>(ExecutionContext executionContext, Cont
|
215 | 221 | Thread currentThread0 = Thread.CurrentThread;
|
216 | 222 | Thread currentThread = currentThread0;
|
217 | 223 | ExecutionContext previousExecutionCtx0 = currentThread0.ExecutionContext;
|
| 224 | + if (previousExecutionCtx0 != null && previousExecutionCtx0.m_isDefault) |
| 225 | + { |
| 226 | + // Default is a null ExecutionContext internally |
| 227 | + previousExecutionCtx0 = null; |
| 228 | + } |
218 | 229 |
|
219 | 230 | // Store current ExecutionContext and SynchronizationContext as "previousXxx".
|
220 | 231 | // This allows us to restore them and undo any Context changes made in callback.Invoke
|
@@ -282,6 +293,115 @@ internal static void RunInternal<TState>(ExecutionContext executionContext, Cont
|
282 | 293 | edi?.Throw();
|
283 | 294 | }
|
284 | 295 |
|
| 296 | + internal static void RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, object state) |
| 297 | + { |
| 298 | + Debug.Assert(threadPoolThread == Thread.CurrentThread); |
| 299 | + CheckThreadPoolAndContextsAreDefault(); |
| 300 | + // ThreadPool starts on Default Context so we don't need to save the "previous" state as we know it is Default (null) |
| 301 | + |
| 302 | + if (executionContext != null && executionContext.m_isDefault) |
| 303 | + { |
| 304 | + // Default is a null ExecutionContext internally |
| 305 | + executionContext = null; |
| 306 | + } |
| 307 | + else if (executionContext != null) |
| 308 | + { |
| 309 | + // Non-Default context to restore |
| 310 | + threadPoolThread.ExecutionContext = executionContext; |
| 311 | + if (executionContext.HasChangeNotifications) |
| 312 | + { |
| 313 | + // There are change notifications; trigger any affected |
| 314 | + OnValuesChanged(previousExecutionCtx: null, executionContext); |
| 315 | + } |
| 316 | + } |
| 317 | + |
| 318 | + ExceptionDispatchInfo edi = null; |
| 319 | + try |
| 320 | + { |
| 321 | + callback.Invoke(state); |
| 322 | + } |
| 323 | + catch (Exception ex) |
| 324 | + { |
| 325 | + // Note: we have a "catch" rather than a "finally" because we want |
| 326 | + // to stop the first pass of EH here. That way we can restore the previous |
| 327 | + // context before any of our callers' EH filters run. |
| 328 | + edi = ExceptionDispatchInfo.Capture(ex); |
| 329 | + } |
| 330 | + |
| 331 | + // Enregister threadPoolThread as it crossed EH, and use enregistered variable |
| 332 | + Thread currentThread = threadPoolThread; |
| 333 | + |
| 334 | + ExecutionContext currentExecutionCtx = currentThread.ExecutionContext; |
| 335 | + |
| 336 | + // Restore changed SynchronizationContext back to Default |
| 337 | + currentThread.SynchronizationContext = null; |
| 338 | + if (currentExecutionCtx != null) |
| 339 | + { |
| 340 | + // The EC always needs to be reset for this overload, as it will flow back to the caller if it performs |
| 341 | + // extra work prior to returning to the Dispatch loop. For example for Task-likes it will flow out of await points |
| 342 | + |
| 343 | + // Restore to Default before Notifications, as the change can be observed in the handler. |
| 344 | + currentThread.ExecutionContext = null; |
| 345 | + if (currentExecutionCtx.HasChangeNotifications) |
| 346 | + { |
| 347 | + // There are change notifications; trigger any affected |
| 348 | + OnValuesChanged(currentExecutionCtx, nextExecutionCtx: null); |
| 349 | + } |
| 350 | + } |
| 351 | + |
| 352 | + // If exception was thrown by callback, rethrow it now original contexts are restored |
| 353 | + edi?.Throw(); |
| 354 | + } |
| 355 | + |
| 356 | + internal static void RunForThreadPoolUnsafe<TState>(ExecutionContext executionContext, Action<TState> callback, in TState state) |
| 357 | + { |
| 358 | + // We aren't running in try/catch as if an exception is directly thrown on the ThreadPool either process |
| 359 | + // will crash or its a ThreadAbortException. |
| 360 | + |
| 361 | + CheckThreadPoolAndContextsAreDefault(); |
| 362 | + Debug.Assert(executionContext != null && !executionContext.m_isDefault, "ExecutionContext argument is Default."); |
| 363 | + |
| 364 | + Thread currentThread = Thread.CurrentThread; |
| 365 | + // Restore Non-Default context |
| 366 | + currentThread.ExecutionContext = executionContext; |
| 367 | + if (executionContext.HasChangeNotifications) |
| 368 | + { |
| 369 | + OnValuesChanged(previousExecutionCtx: null, executionContext); |
| 370 | + } |
| 371 | + |
| 372 | + callback.Invoke(state); |
| 373 | + |
| 374 | + // ThreadPoolWorkQueue.Dispatch will handle notifications and reset EC and SyncCtx back to default |
| 375 | + } |
| 376 | + |
| 377 | + // Inline as only called in one place and always called |
| 378 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 379 | + internal static void ResetThreadPoolThread(Thread currentThread) |
| 380 | + { |
| 381 | + ExecutionContext currentExecutionCtx = currentThread.ExecutionContext; |
| 382 | + |
| 383 | + // Reset to defaults |
| 384 | + currentThread.SynchronizationContext = null; |
| 385 | + currentThread.ExecutionContext = null; |
| 386 | + |
| 387 | + if (currentExecutionCtx != null && currentExecutionCtx.HasChangeNotifications) |
| 388 | + { |
| 389 | + OnValuesChanged(currentExecutionCtx, nextExecutionCtx: null); |
| 390 | + |
| 391 | + // Reset to defaults again without change notifications in case the Change handler changed the contexts |
| 392 | + currentThread.SynchronizationContext = null; |
| 393 | + currentThread.ExecutionContext = null; |
| 394 | + } |
| 395 | + } |
| 396 | + |
| 397 | + [System.Diagnostics.Conditional("DEBUG")] |
| 398 | + internal static void CheckThreadPoolAndContextsAreDefault() |
| 399 | + { |
| 400 | + Debug.Assert(Thread.CurrentThread.IsThreadPoolThread); |
| 401 | + Debug.Assert(Thread.CurrentThread.ExecutionContext == null, "ThreadPool thread not on Default ExecutionContext."); |
| 402 | + Debug.Assert(Thread.CurrentThread.SynchronizationContext == null, "ThreadPool thread not on Default SynchronizationContext."); |
| 403 | + } |
| 404 | + |
285 | 405 | internal static void OnValuesChanged(ExecutionContext previousExecutionCtx, ExecutionContext nextExecutionCtx)
|
286 | 406 | {
|
287 | 407 | Debug.Assert(previousExecutionCtx != nextExecutionCtx);
|
|
0 commit comments