+
Skip to content

Implement SynchronizationContext for GLib #856

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

Merged
merged 1 commit into from
May 14, 2023
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
35 changes: 35 additions & 0 deletions src/Libs/GLib-2.0/Internal/MainLoopSynchronizationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Threading;

namespace GLib.Internal;

public sealed class MainLoopSynchronizationContext : SynchronizationContext
{
public override SynchronizationContext CreateCopy()
=> new MainLoopSynchronizationContext();

public override void Post(SendOrPostCallback d, object? state)
=> ScheduleAction(() => d(state));

public override void Send(SendOrPostCallback d, object? state)
=> throw new NotImplementedException();

private static void ScheduleAction(Action action)
{
var proxy = new SourceFuncNotifiedHandler(() =>
{
try
{
action();
}
catch (Exception ex)
{
UnhandledException.Raise(ex);
}

return false;
});

Functions.IdleAdd(Constants.PRIORITY_DEFAULT_IDLE, proxy.NativeCallback, IntPtr.Zero, proxy.DestroyNotify);
}
}
20 changes: 19 additions & 1 deletion src/Libs/GLib-2.0/Public/MainLoop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace GLib;
using System.Threading;

namespace GLib;

public sealed partial class MainLoop
{
Expand All @@ -21,5 +23,21 @@ public MainContext GetContext()

public void Run() => Internal.MainLoop.Run(Handle);

public void RunWithSynchronizationContext()
{
var original = SynchronizationContext.Current;

SynchronizationContext.SetSynchronizationContext(new Internal.MainLoopSynchronizationContext());

try
{
Run();
}
finally
{
SynchronizationContext.SetSynchronizationContext(original);
}
}

public void Quit() => Internal.MainLoop.Quit(Handle);
}
20 changes: 19 additions & 1 deletion src/Libs/Gio-2.0/Public/Application.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Gio;
using System.Threading;

namespace Gio;

public partial class Application
{
Expand All @@ -11,4 +13,20 @@ public int Run()
{
return Internal.Application.Run(Handle, 0, new string[0]);
}

public int RunWithSynchronizationContext()
{
var original = SynchronizationContext.Current;

SynchronizationContext.SetSynchronizationContext(new GLib.Internal.MainLoopSynchronizationContext());

try
{
return Run();
}
finally
{
SynchronizationContext.SetSynchronizationContext(original);
}
}
}
68 changes: 68 additions & 0 deletions src/Tests/Libs/GLib-2.0.Tests/SynchronizationContextTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GLib.Tests;

[TestClass, TestCategory("IntegrationTest")]
public class SynchronizationContextTest : Test
{
[TestMethod]
public void AsyncMethodIsExecutedOnMainLoopThread()
{
var mainLoop = new MainLoop();
var context = mainLoop.GetContext();
var source = Functions.TimeoutSourceNew(1);
source.Attach(context);

int? resumeThread = null;
source.SetCallback(() =>
{
AsyncMethod(() =>
{
resumeThread = System.Threading.Thread.CurrentThread.ManagedThreadId;
mainLoop.Quit();
});
return false;
});

mainLoop.RunWithSynchronizationContext();

resumeThread.Should().Be(System.Threading.Thread.CurrentThread.ManagedThreadId);
}

[TestMethod]
public void ExceptionInAsyncMethodCanBeHandledViaUnhandledExceptionHandler()
{
var mainLoop = new MainLoop();
var context = mainLoop.GetContext();
var source = Functions.TimeoutSourceNew(1);
source.Attach(context);
source.SetCallback(() =>
{
AsyncMethod(() =>
{
throw new Exception();
});
return false;
});

var exceptionCaught = false;
UnhandledException.SetHandler(e =>
{
exceptionCaught = true;
mainLoop.Quit();
});

mainLoop.RunWithSynchronizationContext();

exceptionCaught.Should().BeTrue();
}

private static async void AsyncMethod(Action finish)
{
await Task.Delay(1);
finish();
}
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载