Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SynchronizationContext to provide order and execution guarantees #5002

Merged
merged 11 commits into from
Jan 26, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Development;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Platform;
Expand Down Expand Up @@ -62,8 +63,12 @@ public void TestAsyncThrows()
// ReSharper disable once AsyncVoidLambda
host.UpdateThread.Scheduler.Add(async () =>
{
Assert.That(ThreadSafety.IsUpdateThread);

await Task.Delay(100).ConfigureAwait(true);

Assert.That(ThreadSafety.IsUpdateThread);

throw new InvalidOperationException();
});
});
Expand Down
11 changes: 11 additions & 0 deletions osu.Framework/Threading/SchedulerSynchronizationContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Diagnostics;
using System.Threading;

#nullable enable
Expand All @@ -19,6 +20,16 @@ public SchedulerSynchronizationContext(Scheduler scheduler)
this.scheduler = scheduler;
}

public override void Send(SendOrPostCallback d, object? state)
{
var del = scheduler.Add(() => d(state));

Debug.Assert(del != null);

while (del.State == ScheduledDelegate.RunState.Waiting)
scheduler.Update();
frenzibyte marked this conversation as resolved.
Show resolved Hide resolved
}

public override void Post(SendOrPostCallback d, object? state) => scheduler.Add(() => d(state));
}
}