Skip to content

Commit

Permalink
Remove deprecated way of scheduling work on the main thread (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban authored and glennawatson committed Jan 19, 2020
1 parent 3797577 commit a226dc0
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/System.Reactive.Wasm/Internal/WasmScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System.Collections.Generic;
using System.Reactive.Disposables;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace System.Reactive.Concurrency
Expand Down Expand Up @@ -126,30 +127,30 @@ public override IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Fun
// Import from https://github.com/mono/mono/blob/0a8126c2094d2d0800a462d4d0c790d4db421477/mcs/class/corlib/System.Threading/Timer.cs#L39
internal static class WasmRuntime
{
private static Dictionary<int, Action> _callbacks;
private static int _nextId;
private static readonly ScheduleTimeoutDelegate _scheduleTimeout;

internal static void ScheduleTimeout(int timeout, Action action)
static WasmRuntime()
{
if (_callbacks == null)
// Note that the assembly name must be provided here for mono-wasm AOT to work properly, as
// there is no stack walking available to determine the resolution context.
if (Type.GetType("System.Threading.WasmRuntime, mscorlib") is Type wasmRuntime
&& wasmRuntime.GetMethod(nameof(ScheduleTimeout), Reflection.BindingFlags.NonPublic | Reflection.BindingFlags.Static) is MethodInfo scheduleTimeout)
{
_callbacks = new Dictionary<int, Action>();
_scheduleTimeout = (ScheduleTimeoutDelegate)scheduleTimeout.CreateDelegate(typeof(ScheduleTimeoutDelegate));
}
else
{
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations. Removed for performance reasons.
throw new NotSupportedException("The currently running version of the runtime does not support this version of the WebAssembly scheduler.");
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations
}

int id = ++_nextId;
_callbacks[id] = action;
SetTimeout(timeout, id);
}

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void SetTimeout(int timeout, int id);
private delegate void ScheduleTimeoutDelegate(int timeout, Action action);

// XXX Keep this in sync with mini-wasm.c:mono_set_timeout_exec
private static void TimeoutCallback(int id)
internal static void ScheduleTimeout(int timeout, Action action)
{
Action cb = _callbacks[id];
_callbacks.Remove(id);
cb();
_scheduleTimeout.Invoke(timeout, action);
}
}
}
Expand Down

0 comments on commit a226dc0

Please sign in to comment.