diff --git a/src/CoreFoundation/CFRunLoop.cs b/src/CoreFoundation/CFRunLoop.cs index d3b7e661cc83..d38411a83745 100644 --- a/src/CoreFoundation/CFRunLoop.cs +++ b/src/CoreFoundation/CFRunLoop.cs @@ -59,7 +59,11 @@ public enum CFRunLoopExitReason : int { // CFRunLoop.h [StructLayout (LayoutKind.Sequential)] +#if NET + internal unsafe struct CFRunLoopSourceContext { +#else internal struct CFRunLoopSourceContext { +#endif public CFIndex Version; public IntPtr Info; public IntPtr Retain; @@ -67,9 +71,15 @@ internal struct CFRunLoopSourceContext { public IntPtr CopyDescription; public IntPtr Equal; public IntPtr Hash; +#if NET + public delegate* unmanaged Schedule; + public delegate* unmanaged Cancel; + public delegate* unmanaged Perform; +#else public IntPtr Schedule; public IntPtr Cancel; public IntPtr Perform; +#endif } #if NET @@ -145,9 +155,11 @@ public abstract class CFRunLoopSourceCustom : CFRunLoopSource { [DllImport (Constants.CoreFoundationLibrary)] extern static /* CFRunLoopSourceRef */ IntPtr CFRunLoopSourceCreate (/* CFAllocatorRef */ IntPtr allocator, /* CFIndex */ nint order, /* CFRunLoopSourceContext* */ ref CFRunLoopSourceContext context); +#if !NET static ScheduleCallback ScheduleDelegate = (ScheduleCallback) Schedule; static CancelCallback CancelDelegate = (CancelCallback) Cancel; static PerformCallback PerformDelegate = (PerformCallback) Perform; +#endif protected CFRunLoopSourceCustom () : base (IntPtr.Zero, true) @@ -155,9 +167,17 @@ protected CFRunLoopSourceCustom () gch = GCHandle.Alloc (this); var ctx = new CFRunLoopSourceContext (); ctx.Info = GCHandle.ToIntPtr (gch); +#if NET + unsafe { + ctx.Schedule = &Schedule; + ctx.Cancel = &Cancel; + ctx.Perform = &Perform; + } +#else ctx.Schedule = Marshal.GetFunctionPointerForDelegate (ScheduleDelegate); ctx.Cancel = Marshal.GetFunctionPointerForDelegate (CancelDelegate); ctx.Perform = Marshal.GetFunctionPointerForDelegate (PerformDelegate); +#endif var handle = CFRunLoopSourceCreate (IntPtr.Zero, 0, ref ctx); InitializeHandle (handle); @@ -165,7 +185,11 @@ protected CFRunLoopSourceCustom () delegate void ScheduleCallback (IntPtr info, IntPtr runLoop, IntPtr mode); +#if NET + [UnmanagedCallersOnly] +#else [MonoPInvokeCallback (typeof(ScheduleCallback))] +#endif static void Schedule (IntPtr info, IntPtr runLoop, IntPtr mode) { var source = GCHandle.FromIntPtr (info).Target as CFRunLoopSourceCustom; @@ -182,7 +206,11 @@ static void Schedule (IntPtr info, IntPtr runLoop, IntPtr mode) delegate void CancelCallback (IntPtr info, IntPtr runLoop, IntPtr mode); +#if NET + [UnmanagedCallersOnly] +#else [MonoPInvokeCallback (typeof(CancelCallback))] +#endif static void Cancel (IntPtr info, IntPtr runLoop, IntPtr mode) { var source = GCHandle.FromIntPtr (info).Target as CFRunLoopSourceCustom; @@ -199,7 +227,11 @@ static void Cancel (IntPtr info, IntPtr runLoop, IntPtr mode) delegate void PerformCallback (IntPtr info); +#if NET + [UnmanagedCallersOnly] +#else [MonoPInvokeCallback (typeof(PerformCallback))] +#endif static void Perform (IntPtr info) { var source = GCHandle.FromIntPtr (info).Target as CFRunLoopSourceCustom;