Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ insert_final_newline = true
indent_size = 4
charset = utf-8-bom

# Don't show a message for analyzer suppressions that don't apply to a specific TFM, they probably apply to other TFMs
dotnet_diagnostic.IDE0079.severity = none

# XML project files
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}]
indent_size = 2
Expand Down
32 changes: 18 additions & 14 deletions src/CenterEdge.Async/AsyncHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ public static void RunSync(Func<ValueTask> task)
SynchronizationContext.SetSynchronizationContext(synch);
try
{
#pragma warning disable CA2012
var awaiter = task().GetAwaiter();
#pragma warning restore CA2012

if (!awaiter.IsCompleted)
{
Expand Down Expand Up @@ -138,7 +140,9 @@ public static void RunSync<TState>(Func<TState, ValueTask> task, TState state)
SynchronizationContext.SetSynchronizationContext(synch);
try
{
#pragma warning disable CA2012
var awaiter = task(state).GetAwaiter();
#pragma warning restore CA2012

if (!awaiter.IsCompleted)
{
Expand Down Expand Up @@ -247,7 +251,9 @@ public static T RunSync<T>(Func<ValueTask<T>> task)
SynchronizationContext.SetSynchronizationContext(synch);
try
{
#pragma warning disable CA2012
var awaiter = task().GetAwaiter();
#pragma warning restore CA2012

if (!awaiter.IsCompleted)
{
Expand Down Expand Up @@ -284,7 +290,9 @@ public static T RunSync<T, TState>(Func<TState, ValueTask<T>> task, TState state
SynchronizationContext.SetSynchronizationContext(synch);
try
{
#pragma warning disable CA2012
var awaiter = task(state).GetAwaiter();
#pragma warning restore CA2012

if (!awaiter.IsCompleted)
{
Expand All @@ -305,16 +313,12 @@ public static T RunSync<T, TState>(Func<TState, ValueTask<T>> task, TState state
}

// Note: Sealing this class can help JIT make non-virtual method calls and inlined method calls for virtual methods
private sealed class ExclusiveSynchronizationContext<TAwaiter> : SynchronizationContext, IDisposable
private sealed class ExclusiveSynchronizationContext<TAwaiter>(
SynchronizationContext? parentSynchronizationContext)
: SynchronizationContext, IDisposable
where TAwaiter : struct, ICriticalNotifyCompletion
{
private readonly SynchronizationContext? _parentSynchronizationContext;
private readonly BlockingCollection<(SendOrPostCallback Callback, object? State)> _items = new();

public ExclusiveSynchronizationContext(SynchronizationContext? parentSynchronizationContext)
{
_parentSynchronizationContext = parentSynchronizationContext;
}
private readonly BlockingCollection<(SendOrPostCallback Callback, object? State)> _items = [];

public override void Send(SendOrPostCallback d, object? state)
{
Expand Down Expand Up @@ -366,9 +370,9 @@ public void Run(TAwaiter awaiter)

while (!_items.IsCompleted)
{
var task = _items.Take();
var (callback, state) = _items.Take();

task.Callback(task.State);
callback(state);
}
}

Expand All @@ -379,18 +383,18 @@ public void RunAlreadyComplete()

while (!_items.IsCompleted)
{
var task = _items.Take();
var (callback, state) = _items.Take();

ExecuteOnParent(task.Callback, task.State);
ExecuteOnParent(callback, state);
}
}

// Executes a work item on the parent SynchronizationContext or on the thread pool if there is not one
private void ExecuteOnParent(SendOrPostCallback callback, object? state)
{
if (_parentSynchronizationContext != null)
if (parentSynchronizationContext != null)
{
_parentSynchronizationContext.Post(callback, state);
parentSynchronizationContext.Post(callback, state);
}
else
{
Expand Down