-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5002 from peppy/sync-context-run-inline
Refactor `SynchronizationContext` to provide order and execution guarantees
- Loading branch information
Showing
6 changed files
with
143 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// 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. | ||
|
||
namespace osu.Framework.Threading | ||
{ | ||
internal class GameThreadScheduler : Scheduler | ||
{ | ||
public GameThreadScheduler(GameThread thread) | ||
: base(() => thread.IsCurrent, thread.Clock) | ||
{ | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
osu.Framework/Threading/GameThreadSynchronizationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// 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 | ||
|
||
namespace osu.Framework.Threading | ||
{ | ||
/// <summary> | ||
/// A synchronisation context which posts all continuations to an isolated scheduler instance. | ||
/// </summary> | ||
/// <remarks> | ||
/// This implementation roughly follows the expectations set out for winforms/WPF as per | ||
/// https://docs.microsoft.com/en-us/archive/msdn-magazine/2011/february/msdn-magazine-parallel-computing-it-s-all-about-the-synchronizationcontext. | ||
/// - Calls to <see cref="Post"/> are guaranteed to run asynchronously. | ||
/// - Calls to <see cref="Send"/> will run inline when they can. | ||
/// - Order of execution is guaranteed (in our case, it is guaranteed over <see cref="Send"/> and <see cref="Post"/> calls alike). | ||
/// - To enforce the above, calling <see cref="Send"/> will flush any pending work until the newly queued item has been completed. | ||
/// </remarks> | ||
internal class GameThreadSynchronizationContext : SynchronizationContext | ||
{ | ||
/// <summary> | ||
/// The total tasks this synchronization context has run. | ||
/// </summary> | ||
public int TotalTasksRun => scheduler.TotalTasksRun; | ||
|
||
private readonly Scheduler scheduler; | ||
|
||
public GameThreadSynchronizationContext(GameThread gameThread) | ||
{ | ||
scheduler = new GameThreadScheduler(gameThread); | ||
} | ||
|
||
public override void Send(SendOrPostCallback callback, object? state) | ||
{ | ||
var scheduledDelegate = scheduler.Add(() => callback(state)); | ||
|
||
Debug.Assert(scheduledDelegate != null); | ||
|
||
while (scheduledDelegate.State < ScheduledDelegate.RunState.Complete) | ||
{ | ||
if (scheduler.IsMainThread) | ||
scheduler.Update(); | ||
else | ||
Thread.Sleep(1); | ||
} | ||
} | ||
|
||
public override void Post(SendOrPostCallback callback, object? state) => scheduler.Add(() => callback(state)); | ||
|
||
/// <summary> | ||
/// Run any pending work queued against this synchronization context. | ||
/// </summary> | ||
public void RunWork() => scheduler.Update(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
osu.Framework/Threading/SchedulerSynchronizationContext.cs
This file was deleted.
Oops, something went wrong.