private ValueTask WaitForCallbackIfNecessaryAsync() { // Same as WaitForCallbackIfNecessary, except returning a task that'll be completed when callbacks complete. CancellationTokenSource source = _node.Partition.Source; if (source.IsCancellationRequested && // Running callbacks has commenced. !source.IsCancellationCompleted && // Running callbacks hasn't finished. source.ThreadIDExecutingCallbacks != Environment.CurrentManagedThreadId) // The executing thread ID is not this thread's ID. { // Callback execution is in progress, the executing thread is different from this thread and has taken the callback for execution // so get a task that'll complete when this target callback is no longer the executing callback. return source.WaitForCallbackToCompleteAsync(_id); } // Callback is either already completed, won't execute, or the callback itself is calling this. return default; }