Browse Source

Avoid BlockLiteral based APIs for simplicity

pull/10923/head
Max Katz 4 years ago
parent
commit
cf4154505b
  1. 23
      src/iOS/Avalonia.iOS/DispatcherImpl.cs
  2. 24
      src/iOS/Avalonia.iOS/Interop.cs

23
src/iOS/Avalonia.iOS/DispatcherImpl.cs

@ -34,21 +34,16 @@ internal class DispatcherImpl : IDispatcherImplWithExplicitBackgroundProcessing
// This is needed to wakeup the loop if we are called from inside of BeforeWait hook // This is needed to wakeup the loop if we are called from inside of BeforeWait hook
}; };
var observerBlock = new BlockLiteral(); var observer = Interop.CFRunLoopObserverCreate(IntPtr.Zero,
observerBlock.SetupBlock((Interop.CFRunLoopObserverCallback)ObserverCallback, null);
var observer = Interop.CFRunLoopObserverCreateWithHandler(IntPtr.Zero,
Interop.CFOptionFlags.kCFRunLoopAfterWaiting | Interop.CFOptionFlags.kCFRunLoopBeforeSources | Interop.CFOptionFlags.kCFRunLoopAfterWaiting | Interop.CFOptionFlags.kCFRunLoopBeforeSources |
Interop.CFOptionFlags.kCFRunLoopBeforeWaiting, Interop.CFOptionFlags.kCFRunLoopBeforeWaiting,
true, true, 0, ObserverCallback, IntPtr.Zero);
0, Interop.CFRunLoopAddObserver(Interop.CFRunLoopGetMain(), observer, Interop.kCFRunLoopCommonModes);
ref observerBlock);
Interop.CFRunLoopAddObserver(CFRunLoop.Main.Handle, observer, Interop.kCFRunLoopCommonModes); _timer = Interop.CFRunLoopTimerCreate(IntPtr.Zero,
var timerBlock = new BlockLiteral();
timerBlock.SetupBlock((Interop.CFRunLoopTimerCallback)TimerCallback, null);
_timer = Interop.CFRunLoopTimerCreateWithHandler(IntPtr.Zero,
Interop.CFAbsoluteTimeGetCurrent() + DistantFutureInterval, Interop.CFAbsoluteTimeGetCurrent() + DistantFutureInterval,
DistantFutureInterval, 0, 0, ref timerBlock); DistantFutureInterval, 0, 0, TimerCallback, IntPtr.Zero);
Interop.CFRunLoopAddTimer(Interop.CFRunLoopGetMain(), _timer, Interop.kCFRunLoopCommonModes);
} }
public event Action? Signaled; public event Action? Signaled;
@ -115,7 +110,7 @@ internal class DispatcherImpl : IDispatcherImplWithExplicitBackgroundProcessing
} }
[MonoPInvokeCallback(typeof(Interop.CFRunLoopObserverCallback))] [MonoPInvokeCallback(typeof(Interop.CFRunLoopObserverCallback))]
private static void ObserverCallback(IntPtr observer, Interop.CFOptionFlags activity) private static void ObserverCallback(IntPtr observer, Interop.CFOptionFlags activity, IntPtr info)
{ {
if (activity == Interop.CFOptionFlags.kCFRunLoopBeforeWaiting) if (activity == Interop.CFOptionFlags.kCFRunLoopBeforeWaiting)
{ {
@ -133,7 +128,7 @@ internal class DispatcherImpl : IDispatcherImplWithExplicitBackgroundProcessing
} }
[MonoPInvokeCallback(typeof(Interop.CFRunLoopTimerCallback))] [MonoPInvokeCallback(typeof(Interop.CFRunLoopTimerCallback))]
private static void TimerCallback(IntPtr timer) private static void TimerCallback(IntPtr timer, IntPtr info)
{ {
Instance.Timer?.Invoke(); Instance.Timer?.Invoke();
} }

24
src/iOS/Avalonia.iOS/Interop.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using CoreFoundation; using CoreFoundation;
using Foundation; using Foundation;
@ -13,7 +14,7 @@ internal class Interop
internal static NativeHandle kCFRunLoopCommonModes = CFString.CreateNative("kCFRunLoopCommonModes"); internal static NativeHandle kCFRunLoopCommonModes = CFString.CreateNative("kCFRunLoopCommonModes");
[Flags] [Flags]
public enum CFOptionFlags : ulong internal enum CFOptionFlags : ulong
{ {
kCFRunLoopBeforeSources = (1UL << 2), kCFRunLoopBeforeSources = (1UL << 2),
kCFRunLoopAfterWaiting = (1UL << 6), kCFRunLoopAfterWaiting = (1UL << 6),
@ -21,27 +22,34 @@ internal class Interop
} }
[UnmanagedFunctionPointer(CallingConvention.StdCall)] [UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void CFRunLoopObserverCallback(IntPtr observer, CFOptionFlags activity); internal delegate void CFRunLoopObserverCallback(IntPtr observer, CFOptionFlags activity, IntPtr info);
[UnmanagedFunctionPointer(CallingConvention.StdCall)] [UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void CFRunLoopTimerCallback(IntPtr timer); internal delegate void CFRunLoopTimerCallback(IntPtr timer, IntPtr info);
[DllImport(CoreFoundationLibrary)]
internal static extern IntPtr CFRunLoopGetMain();
[DllImport(CoreFoundationLibrary)] [DllImport(CoreFoundationLibrary)]
internal static extern IntPtr CFRunLoopObserverCreateWithHandler(IntPtr allocator, CFOptionFlags activities, bool repeats, int index, ref BlockLiteral block); internal static extern IntPtr CFRunLoopObserverCreate(IntPtr allocator, CFOptionFlags activities,
bool repeats, int index, CFRunLoopObserverCallback callout, IntPtr context);
[DllImport(CoreFoundationLibrary)] [DllImport(CoreFoundationLibrary)]
internal static extern IntPtr CFRunLoopAddObserver(IntPtr loop, IntPtr observer, IntPtr mode); internal static extern IntPtr CFRunLoopAddObserver(IntPtr loop, IntPtr observer, IntPtr mode);
[DllImport(CoreFoundationLibrary)] [DllImport(CoreFoundationLibrary)]
internal static extern IntPtr CFRunLoopTimerCreateWithHandler(IntPtr allocator, double firstDate, double interval, internal static extern IntPtr CFRunLoopTimerCreate(IntPtr allocator, double firstDate, double interval,
CFOptionFlags flags, int order, ref BlockLiteral block); CFOptionFlags flags, int order, CFRunLoopTimerCallback callout, IntPtr context);
[DllImport(CoreFoundationLibrary)] [DllImport(CoreFoundationLibrary)]
internal static extern void CFRunLoopTimerSetTolerance(IntPtr timer, double tolerance); internal static extern void CFRunLoopTimerSetTolerance(IntPtr timer, double tolerance);
[DllImport(CoreFoundationLibrary)] [DllImport(CoreFoundationLibrary)]
internal static extern void CFRunLoopTimerSetNextFireDate(IntPtr timer, double fireDate); internal static extern void CFRunLoopTimerSetNextFireDate(IntPtr timer, double fireDate);
[DllImport(CoreFoundationLibrary)]
internal static extern void CFRunLoopAddTimer(IntPtr loop, IntPtr timer, IntPtr mode);
[DllImport(CoreFoundationLibrary)] [DllImport(CoreFoundationLibrary)]
internal static extern double CFAbsoluteTimeGetCurrent(); internal static extern double CFAbsoluteTimeGetCurrent();
} }

Loading…
Cancel
Save