From b4b3e8d280ba4ebb452711c72e83b75e7931e5fb Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 13 Sep 2018 13:07:30 +0200 Subject: [PATCH 1/8] [CoreFoundation] Make DispatchObject inherit from NativeObject to share more code. --- src/CoreFoundation/Dispatch.cs | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/src/CoreFoundation/Dispatch.cs b/src/CoreFoundation/Dispatch.cs index e53a3aa32022..5ad4a486c4e1 100644 --- a/src/CoreFoundation/Dispatch.cs +++ b/src/CoreFoundation/Dispatch.cs @@ -47,13 +47,12 @@ public enum DispatchQueuePriority : int { Background = Int16.MinValue } - public abstract class DispatchObject : INativeObject -#if !COREBUILD - , IDisposable -#endif + public abstract class DispatchObject : NativeObject { #if !COREBUILD - internal IntPtr handle; + // Temporary field to easier split up changes into different commits. + // It will be removed in a later commit in this PR. + internal IntPtr handle { get { return Handle; } set { InitializeHandle (value); } } // // Constructors and lifecycle @@ -79,27 +78,14 @@ internal DispatchObject () [DllImport (Constants.libcLibrary)] extern static IntPtr dispatch_retain (IntPtr o); - ~DispatchObject () - { - Dispose (false); - } - - public void Dispose () + protected override void Retain () { - Dispose (true); - GC.SuppressFinalize (this); - } - - public IntPtr Handle { - get { return handle; } + dispatch_retain (Handle); } - protected virtual void Dispose (bool disposing) + protected override void Release () { - if (handle != IntPtr.Zero){ - dispatch_release (handle); - handle = IntPtr.Zero; - } + dispatch_release (Handle); } public static bool operator == (DispatchObject a, DispatchObject b) From c4a98142fbad203bdc8f8383042309f8cd17e900 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 13 Sep 2018 13:11:21 +0200 Subject: [PATCH 2/8] [CoreFoundation] Replace calls to Check () with calls to GetCheckedHandle () to reuse more code. --- src/CoreFoundation/Dispatch.cs | 38 ++++------------ src/CoreFoundation/DispatchSource.cs | 68 ++++++++++------------------ 2 files changed, 33 insertions(+), 73 deletions(-) diff --git a/src/CoreFoundation/Dispatch.cs b/src/CoreFoundation/Dispatch.cs index 5ad4a486c4e1..cc63fec3599f 100644 --- a/src/CoreFoundation/Dispatch.cs +++ b/src/CoreFoundation/Dispatch.cs @@ -122,12 +122,6 @@ public override int GetHashCode () return (int) handle; } - protected void Check () - { - if (handle == IntPtr.Zero) - throw new ObjectDisposedException (GetType ().ToString ()); - } - [DllImport (Constants.libcLibrary)] extern static void dispatch_set_target_queue (/* dispatch_object_t */ IntPtr queue, /* dispatch_queue_t */ IntPtr target); @@ -187,10 +181,7 @@ public DispatchQueue (string label, bool concurrent) public string Label { get { - if (handle == IntPtr.Zero) - throw new ObjectDisposedException ("DispatchQueue"); - - return Marshal.PtrToStringAnsi (dispatch_queue_get_label (handle)); + return Marshal.PtrToStringAnsi (dispatch_queue_get_label (GetCheckedHandle ())); } } @@ -203,14 +194,12 @@ public static string CurrentQueueLabel { public void Suspend () { - Check (); - dispatch_suspend (handle); + dispatch_suspend (GetCheckedHandle ()); } public void Resume () { - Check (); - dispatch_resume (handle); + dispatch_resume (GetCheckedHandle ()); } [DllImport (Constants.libcLibrary)] @@ -224,12 +213,10 @@ public void Resume () public IntPtr Context { get { - Check (); - return dispatch_get_context (handle); + return dispatch_get_context (GetCheckedHandle ()); } set { - Check (); - dispatch_set_context (handle, value); + dispatch_set_context (GetCheckedHandle (), value); } } @@ -534,8 +521,7 @@ public void DispatchAsync (DispatchQueue queue, Action action) if (action == null) throw new ArgumentNullException ("action"); - Check (); - dispatch_group_async_f (handle, queue.handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); + dispatch_group_async_f (GetCheckedHandle (), queue.handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); } public void Notify (DispatchQueue queue, Action action) @@ -545,26 +531,22 @@ public void Notify (DispatchQueue queue, Action action) if (action == null) throw new ArgumentNullException ("action"); - Check (); - dispatch_group_notify_f (handle, queue.handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); + dispatch_group_notify_f (GetCheckedHandle (), queue.handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); } public void Enter () { - Check (); - dispatch_group_enter (handle); + dispatch_group_enter (GetCheckedHandle ()); } public void Leave () { - Check (); - dispatch_group_leave (handle); + dispatch_group_leave (GetCheckedHandle ()); } public bool Wait (DispatchTime timeout) { - Check (); - return dispatch_group_wait (handle, timeout.Nanoseconds) == 0; + return dispatch_group_wait (GetCheckedHandle (), timeout.Nanoseconds) == 0; } [DllImport (Constants.libcLibrary)] diff --git a/src/CoreFoundation/DispatchSource.cs b/src/CoreFoundation/DispatchSource.cs index cca3552b99bc..2a8ca9dfda80 100644 --- a/src/CoreFoundation/DispatchSource.cs +++ b/src/CoreFoundation/DispatchSource.cs @@ -97,9 +97,8 @@ internal DispatchSource () {} public void SetEventHandler (Action handler) { - Check (); if (handler == null){ - dispatch_source_set_event_handler_f (handle, IntPtr.Zero); + dispatch_source_set_event_handler_f (GetCheckedHandle (), IntPtr.Zero); return; } @@ -115,27 +114,24 @@ public void SetEventHandler (Action handler) if (sc == null) SynchronizationContext.SetSynchronizationContext (null); } - }, block=> dispatch_source_set_event_handler (handle, block)); + }, block=> dispatch_source_set_event_handler (GetCheckedHandle (), block)); } } public void Suspend () { - Check (); - dispatch_suspend (handle); + dispatch_suspend (GetCheckedHandle ()); } public void Resume () { - Check (); - dispatch_resume (handle); + dispatch_resume (GetCheckedHandle ()); } public void SetRegistrationHandler (Action handler) { if (handler == null) throw new ArgumentNullException ("handler"); - Check (); unsafe { DispatchBlock.Invoke ( @@ -149,7 +145,7 @@ public void SetRegistrationHandler (Action handler) if (sc == null) SynchronizationContext.SetSynchronizationContext (null); } - }, block => dispatch_source_set_registration_handler (handle, block)); + }, block => dispatch_source_set_registration_handler (GetCheckedHandle (), block)); } } @@ -158,7 +154,6 @@ public void SetCancelHandler (Action handler) if (handler == null) throw new ArgumentNullException ("handler"); - Check (); unsafe { DispatchBlock.Invoke ( delegate { @@ -171,14 +166,13 @@ public void SetCancelHandler (Action handler) if (sc == null) SynchronizationContext.SetSynchronizationContext (null); } - }, block => dispatch_source_set_cancel_handler (handle, block)); + }, block => dispatch_source_set_cancel_handler (GetCheckedHandle (), block)); } } public void Cancel () { - Check (); - dispatch_source_cancel (handle); + dispatch_source_cancel (GetCheckedHandle ()); } protected override void Dispose (bool disposing) @@ -191,8 +185,7 @@ protected override void Dispose (bool disposing) public bool IsCanceled { get { - Check (); - return dispatch_source_testcancel (handle) != IntPtr.Zero; + return dispatch_source_testcancel (GetCheckedHandle ()) != IntPtr.Zero; } } @@ -257,8 +250,7 @@ internal Mach () public int MachPort { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } } @@ -282,8 +274,7 @@ public MachSend (int machPort, bool sendDead = false, DispatchQueue queue = null public bool SendRightsDestroyed { get { - Check (); - return dispatch_source_get_data (handle) != IntPtr.Zero; + return dispatch_source_get_data (GetCheckedHandle ()) != IntPtr.Zero; } } } @@ -324,8 +315,7 @@ public MemoryPressure (MemoryPressureFlags monitorFlags = MemoryPressureFlags.No public MemoryPressureFlags PressureFlags { get { - Check (); - return (MemoryPressureFlags) dispatch_source_get_data (handle); + return (MemoryPressureFlags) dispatch_source_get_data (GetCheckedHandle ()); } } } @@ -349,15 +339,13 @@ public ProcessMonitor (int processId, ProcessMonitorFlags monitorKind = ProcessM public int ProcessId { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } public ProcessMonitorFlags MonitorFlags { get { - Check (); - return (ProcessMonitorFlags) dispatch_source_get_data (handle); + return (ProcessMonitorFlags) dispatch_source_get_data (GetCheckedHandle ()); } } } @@ -380,15 +368,13 @@ public ReadMonitor (int fileDescriptor, DispatchQueue queue = null) public int FileDescriptor { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } public int BytesAvailable { get { - Check (); - return (int) dispatch_source_get_data (handle); + return (int) dispatch_source_get_data (GetCheckedHandle ()); } } } @@ -410,15 +396,13 @@ public SignalMonitor (int signalNumber, DispatchQueue queue = null) public int SignalNumber { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } public int SignalsDelivered { get { - Check (); - return (int) dispatch_source_get_data (handle); + return (int) dispatch_source_get_data (GetCheckedHandle ()); } } } @@ -442,8 +426,7 @@ public Timer (bool strict = false, DispatchQueue queue = null) public int TimerFiredCount { get { - Check (); - return (int) dispatch_source_get_data (handle); + return (int) dispatch_source_get_data (GetCheckedHandle ()); } } [DllImport (Constants.libcLibrary)] @@ -451,8 +434,7 @@ public int TimerFiredCount { public void SetTimer (DispatchTime time, long nanosecondInterval, long nanosecondLeeway) { - Check (); - dispatch_source_set_timer (handle, time.Nanoseconds, nanosecondInterval, nanosecondLeeway); + dispatch_source_set_timer (GetCheckedHandle (), time.Nanoseconds, nanosecondInterval, nanosecondLeeway); } } @@ -513,15 +495,13 @@ protected override void Dispose (bool disposing) public int FileDescriptor { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } public VnodeMonitorKind ObservedEvents { get { - Check (); - return (VnodeMonitorKind) (int) dispatch_source_get_data (handle); + return (VnodeMonitorKind) (int) dispatch_source_get_data (GetCheckedHandle ()); } } @@ -544,15 +524,13 @@ public WriteMonitor (int fileDescriptor, DispatchQueue queue = null) } public int FileDescriptor { get { - Check (); - return (int) dispatch_source_get_handle (handle); + return (int) dispatch_source_get_handle (GetCheckedHandle ()); } } public int BufferSpaceAvailable { get { - Check (); - return (int) dispatch_source_get_data (handle); + return (int) dispatch_source_get_data (GetCheckedHandle ()); } } } From ddf5549d6667eb1d32fedc04d42f4a14a2f90735 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 13 Sep 2018 13:32:16 +0200 Subject: [PATCH 3/8] [CoreFoundation] Simplify a bit by reusing code in base constructors. --- src/CoreFoundation/Dispatch.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/CoreFoundation/Dispatch.cs b/src/CoreFoundation/Dispatch.cs index cc63fec3599f..a3e4bf043f81 100644 --- a/src/CoreFoundation/Dispatch.cs +++ b/src/CoreFoundation/Dispatch.cs @@ -59,13 +59,10 @@ public abstract class DispatchObject : NativeObject // [Preserve (Conditional = true)] internal DispatchObject (IntPtr handle, bool owns) + : base (handle, owns) { if (handle == IntPtr.Zero) throw new ArgumentNullException ("handle"); - - this.handle = handle; - if (!owns) - dispatch_retain (handle); } internal DispatchObject () @@ -151,10 +148,9 @@ public DispatchQueue (IntPtr handle) : base (handle, false) { } - public DispatchQueue (string label) : base () + public DispatchQueue (string label) + : base (dispatch_queue_create (label, IntPtr.Zero), true) { - // Initialized in owned state for the queue. - handle = dispatch_queue_create (label, IntPtr.Zero); if (handle == IntPtr.Zero) throw new Exception ("Error creating dispatch queue"); } @@ -169,8 +165,8 @@ static IntPtr ConcurrentQueue { } public DispatchQueue (string label, bool concurrent) + : base (dispatch_queue_create (label, concurrent ? ConcurrentQueue : IntPtr.Zero), true) { - handle = dispatch_queue_create (label, concurrent ? ConcurrentQueue : IntPtr.Zero); if (handle == IntPtr.Zero) throw new Exception ("Error creating dispatch queue"); } From 279c1d5b79fe6a961002fb98b6246ee2c368d598 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 13 Sep 2018 13:25:55 +0200 Subject: [PATCH 4/8] [CoreFoundation] Use Handle instead of handle. --- src/CoreFoundation/Dispatch.cs | 30 ++++++++++++++-------------- src/CoreFoundation/DispatchData.cs | 8 ++++---- src/CoreFoundation/DispatchSource.cs | 4 ++-- src/Network/NWConnection.cs | 2 +- src/Network/NWListener.cs | 2 +- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/CoreFoundation/Dispatch.cs b/src/CoreFoundation/Dispatch.cs index a3e4bf043f81..01f808f8946d 100644 --- a/src/CoreFoundation/Dispatch.cs +++ b/src/CoreFoundation/Dispatch.cs @@ -97,7 +97,7 @@ protected override void Release () } else { if (ob == null) return false; - return a.handle == b.handle; + return a.Handle == b.Handle; } } @@ -111,12 +111,12 @@ public override bool Equals (object other) var od = other as DispatchQueue; if (od == null) return false; - return od.handle == handle; + return od.Handle == Handle; } public override int GetHashCode () { - return (int) handle; + return (int) Handle; } [DllImport (Constants.libcLibrary)] @@ -126,7 +126,7 @@ public void SetTargetQueue (DispatchQueue queue) { // note: null is allowed because DISPATCH_TARGET_QUEUE_DEFAULT is defined as NULL (dispatch/queue.h) IntPtr q = queue == null ? IntPtr.Zero : queue.Handle; - dispatch_set_target_queue (handle, q); + dispatch_set_target_queue (Handle, q); } [DllImport (Constants.libcLibrary)] @@ -151,7 +151,7 @@ public DispatchQueue (IntPtr handle) : base (handle, false) public DispatchQueue (string label) : base (dispatch_queue_create (label, IntPtr.Zero), true) { - if (handle == IntPtr.Zero) + if (Handle == IntPtr.Zero) throw new Exception ("Error creating dispatch queue"); } @@ -167,7 +167,7 @@ static IntPtr ConcurrentQueue { public DispatchQueue (string label, bool concurrent) : base (dispatch_queue_create (label, concurrent ? ConcurrentQueue : IntPtr.Zero), true) { - if (handle == IntPtr.Zero) + if (Handle == IntPtr.Zero) throw new Exception ("Error creating dispatch queue"); } @@ -337,7 +337,7 @@ public void DispatchAsync (Action action) if (action == null) throw new ArgumentNullException ("action"); - dispatch_async_f (handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); + dispatch_async_f (Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); } public void DispatchSync (Action action) @@ -345,7 +345,7 @@ public void DispatchSync (Action action) if (action == null) throw new ArgumentNullException ("action"); - dispatch_sync_f (handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); + dispatch_sync_f (Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); } public void DispatchBarrierAsync (Action action) @@ -353,7 +353,7 @@ public void DispatchBarrierAsync (Action action) if (action == null) throw new ArgumentNullException ("action"); - dispatch_barrier_async_f (handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); + dispatch_barrier_async_f (Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); } public void DispatchAfter (DispatchTime when, Action action) @@ -361,14 +361,14 @@ public void DispatchAfter (DispatchTime when, Action action) if (action == null) throw new ArgumentNullException ("action"); - dispatch_after_f (when.Nanoseconds, handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); + dispatch_after_f (when.Nanoseconds, Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch); } public void Submit (Action action, long times) { if (action == null) throw new ArgumentNullException ("action"); - dispatch_apply_f ((IntPtr) times, handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch_iterations); + dispatch_apply_f ((IntPtr) times, Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, this)), static_dispatch_iterations); } // @@ -411,7 +411,7 @@ public override bool Equals (object other) DispatchQueue o = other as DispatchQueue; if (o == null) return false; - return (o.Handle == handle); + return (o.Handle == Handle); } public static bool operator == (DispatchQueue left, DispatchQueue right) @@ -430,7 +430,7 @@ public override bool Equals (object other) public override int GetHashCode () { - return (int)handle; + return (int) Handle; } #if MONOMAC @@ -517,7 +517,7 @@ public void DispatchAsync (DispatchQueue queue, Action action) if (action == null) throw new ArgumentNullException ("action"); - dispatch_group_async_f (GetCheckedHandle (), queue.handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); + dispatch_group_async_f (GetCheckedHandle (), queue.Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); } public void Notify (DispatchQueue queue, Action action) @@ -527,7 +527,7 @@ public void Notify (DispatchQueue queue, Action action) if (action == null) throw new ArgumentNullException ("action"); - dispatch_group_notify_f (GetCheckedHandle (), queue.handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); + dispatch_group_notify_f (GetCheckedHandle (), queue.Handle, (IntPtr) GCHandle.Alloc (Tuple.Create (action, queue)), DispatchQueue.static_dispatch); } public void Enter () diff --git a/src/CoreFoundation/DispatchData.cs b/src/CoreFoundation/DispatchData.cs index 8826b5624ecb..663c4d66b3fb 100644 --- a/src/CoreFoundation/DispatchData.cs +++ b/src/CoreFoundation/DispatchData.cs @@ -97,14 +97,14 @@ public static DispatchData FromBuffer (IntPtr buffer, nuint size) [DllImport (Constants.libcLibrary)] extern static nuint dispatch_data_get_size (IntPtr handle); - public nuint Size => dispatch_data_get_size (handle); + public nuint Size => dispatch_data_get_size (Handle); [DllImport (Constants.libcLibrary)] extern static IntPtr dispatch_data_create_map (IntPtr handle, out IntPtr bufferPtr, out nuint size); public DispatchData CreateMap (out IntPtr bufferPtr, out nuint size) { - var nh = dispatch_data_create_map (handle, out bufferPtr, out size); + var nh = dispatch_data_create_map (Handle, out bufferPtr, out size); return new DispatchData (nh, owns: true); } @@ -118,7 +118,7 @@ public static DispatchData Concat (DispatchData data1, DispatchData data2) if (data2 == null) throw new ArgumentNullException (nameof (data2)); - return new DispatchData (dispatch_data_create_concat (data1.handle, data2.handle), owns: true); + return new DispatchData (dispatch_data_create_concat (data1.Handle, data2.Handle), owns: true); } [DllImport (Constants.libcLibrary)] @@ -126,7 +126,7 @@ public static DispatchData Concat (DispatchData data1, DispatchData data2) public DispatchData CreateSubrange (nuint offset, nuint size) { - return new DispatchData (dispatch_data_create_subrange (handle, offset, size), owns: true); + return new DispatchData (dispatch_data_create_subrange (Handle, offset, size), owns: true); } #endif } diff --git a/src/CoreFoundation/DispatchSource.cs b/src/CoreFoundation/DispatchSource.cs index 2a8ca9dfda80..0df5650cda30 100644 --- a/src/CoreFoundation/DispatchSource.cs +++ b/src/CoreFoundation/DispatchSource.cs @@ -195,12 +195,12 @@ internal Data (IntPtr handle, bool owns) : base (handle, owns) {} public void MergeData (IntPtr value) { - dispatch_source_merge_data (handle, value); + dispatch_source_merge_data (Handle, value); } public IntPtr PendingData { get { - return dispatch_source_get_data (handle); + return dispatch_source_get_data (Handle); } } } diff --git a/src/Network/NWConnection.cs b/src/Network/NWConnection.cs index c655e73949ba..978050ffdd70 100644 --- a/src/Network/NWConnection.cs +++ b/src/Network/NWConnection.cs @@ -212,7 +212,7 @@ public void SetQueue (DispatchQueue queue) { if (queue == null) throw new ArgumentNullException (nameof (queue)); - nw_connection_set_queue (GetCheckedHandle (), queue.handle); + nw_connection_set_queue (GetCheckedHandle (), queue.Handle); } [DllImport (Constants.NetworkLibrary)] diff --git a/src/Network/NWListener.cs b/src/Network/NWListener.cs index 16ebee54f3a9..9533da6a5aa8 100644 --- a/src/Network/NWListener.cs +++ b/src/Network/NWListener.cs @@ -85,7 +85,7 @@ public void SetQueue (DispatchQueue queue) if (queue == null) throw new ArgumentNullException (nameof (queue)); - nw_listener_set_queue (GetCheckedHandle (), queue.handle); + nw_listener_set_queue (GetCheckedHandle (), queue.Handle); } [DllImport (Constants.NetworkLibrary)] From cbe48e2b995719daebe12f8be92ee5f5d6eb30b2 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 13 Sep 2018 17:54:31 +0200 Subject: [PATCH 5/8] [CoreFoundation] Use InitializeHandle instead of setting the 'handle' field. --- src/CoreFoundation/DispatchSource.cs | 48 +++++++++++++++++++++------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/CoreFoundation/DispatchSource.cs b/src/CoreFoundation/DispatchSource.cs index 0df5650cda30..89da6f3cca42 100644 --- a/src/CoreFoundation/DispatchSource.cs +++ b/src/CoreFoundation/DispatchSource.cs @@ -217,10 +217,12 @@ public DataAdd (DispatchQueue queue = null) type_data_add = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_data_add"); this.queue = queue; - handle = dispatch_source_create (type_data_add, + var handle = dispatch_source_create (type_data_add, handle: IntPtr.Zero, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } } @@ -235,10 +237,12 @@ public DataOr (DispatchQueue queue = null) if (type_data_or == IntPtr.Zero) type_data_or = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_data_or"); this.queue = queue; - handle = dispatch_source_create (type_data_or, + var handle = dispatch_source_create (type_data_or, handle: IntPtr.Zero, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } } @@ -266,10 +270,12 @@ public MachSend (int machPort, bool sendDead = false, DispatchQueue queue = null if (type_mach_send == IntPtr.Zero) type_mach_send = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_mach_send"); this.queue = queue; - handle = dispatch_source_create (type_mach_send, + var handle = dispatch_source_create (type_mach_send, handle: (IntPtr) machPort, mask: (IntPtr) (sendDead ? 1 : 0), queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public bool SendRightsDestroyed { @@ -289,10 +295,12 @@ public MachReceive (int machPort, DispatchQueue queue = null) if (type_mach_recv == IntPtr.Zero) type_mach_recv = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_mach_recv"); this.queue = queue; - handle = dispatch_source_create (type_mach_recv, + var handle = dispatch_source_create (type_mach_recv, handle: (IntPtr) machPort, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } } @@ -307,10 +315,12 @@ public MemoryPressure (MemoryPressureFlags monitorFlags = MemoryPressureFlags.No if (type_memorypressure == IntPtr.Zero) type_memorypressure = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_memorypressure"); this.queue = queue; - handle = dispatch_source_create (type_memorypressure, + var handle = dispatch_source_create (type_memorypressure, handle: IntPtr.Zero, mask: (IntPtr) monitorFlags, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public MemoryPressureFlags PressureFlags { @@ -331,10 +341,12 @@ public ProcessMonitor (int processId, ProcessMonitorFlags monitorKind = ProcessM if (type_proc == IntPtr.Zero) type_proc = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_proc"); this.queue = queue; - handle = dispatch_source_create (type_proc, + var handle = dispatch_source_create (type_proc, handle: (IntPtr) processId, mask: (IntPtr) monitorKind, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public int ProcessId { @@ -360,10 +372,12 @@ public ReadMonitor (int fileDescriptor, DispatchQueue queue = null) if (type_read == IntPtr.Zero) type_read = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_read"); this.queue = queue; - handle = dispatch_source_create (type_read, + var handle = dispatch_source_create (type_read, handle: (IntPtr) fileDescriptor, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public int FileDescriptor { @@ -388,10 +402,12 @@ public SignalMonitor (int signalNumber, DispatchQueue queue = null) if (type_signal == IntPtr.Zero) type_signal = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_signal"); this.queue = queue; - handle = dispatch_source_create (type_signal, + var handle = dispatch_source_create (type_signal, handle: (IntPtr) signalNumber, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public int SignalNumber { @@ -418,10 +434,12 @@ public Timer (bool strict = false, DispatchQueue queue = null) if (type_timer == IntPtr.Zero) type_timer = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_timer"); this.queue = queue; - handle = dispatch_source_create (type_timer, + var handle = dispatch_source_create (type_timer, handle: IntPtr.Zero, mask: strict ? (IntPtr) 1 : IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public int TimerFiredCount { @@ -453,10 +471,12 @@ public VnodeMonitor (int fileDescriptor, VnodeMonitorKind vnodeKind, DispatchQue type_vnode = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_vnode"); this.queue = queue; fd = -1; - handle = dispatch_source_create (type_vnode, + var handle = dispatch_source_create (type_vnode, handle: (IntPtr) fileDescriptor, mask: (IntPtr) vnodeKind, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } const int O_EVTONLY = 0x8000; @@ -478,10 +498,12 @@ public VnodeMonitor (string path, VnodeMonitorKind vnodeKind, DispatchQueue queu type_vnode = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_vnode"); this.queue = queue; - handle = dispatch_source_create (type_vnode, + var handle = dispatch_source_create (type_vnode, handle: (IntPtr) fd, mask: (IntPtr) vnodeKind, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } protected override void Dispose (bool disposing) @@ -517,10 +539,12 @@ public WriteMonitor (int fileDescriptor, DispatchQueue queue = null) if (type_write == IntPtr.Zero) type_write = Dlfcn.dlsym (Libraries.System.Handle, "_dispatch_source_type_write"); this.queue = queue; - handle = dispatch_source_create (type_write, + var handle = dispatch_source_create (type_write, handle: (IntPtr) fileDescriptor, mask: IntPtr.Zero, queue: queue == null ? IntPtr.Zero : queue.Handle); + if (handle != IntPtr.Zero) + InitializeHandle (handle); } public int FileDescriptor { get { From 2add8d1e3ee380601b5c6a00651c09b827e5b9fd Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 13 Sep 2018 13:33:10 +0200 Subject: [PATCH 6/8] [CoreFoundation] Remove temporary 'handle' field. --- src/CoreFoundation/Dispatch.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/CoreFoundation/Dispatch.cs b/src/CoreFoundation/Dispatch.cs index 01f808f8946d..1fd88b4da8da 100644 --- a/src/CoreFoundation/Dispatch.cs +++ b/src/CoreFoundation/Dispatch.cs @@ -50,10 +50,6 @@ public enum DispatchQueuePriority : int { public abstract class DispatchObject : NativeObject { #if !COREBUILD - // Temporary field to easier split up changes into different commits. - // It will be removed in a later commit in this PR. - internal IntPtr handle { get { return Handle; } set { InitializeHandle (value); } } - // // Constructors and lifecycle // From f715def2e9b0becfe11cb52c0615d9862ee861a3 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 13 Sep 2018 13:36:53 +0200 Subject: [PATCH 7/8] [CoreFoundation] Remove needless 'unsafe' blocks. --- src/CoreFoundation/DispatchSource.cs | 72 +++++++++++++--------------- 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/src/CoreFoundation/DispatchSource.cs b/src/CoreFoundation/DispatchSource.cs index 89da6f3cca42..08f2ee1168df 100644 --- a/src/CoreFoundation/DispatchSource.cs +++ b/src/CoreFoundation/DispatchSource.cs @@ -102,20 +102,18 @@ public void SetEventHandler (Action handler) return; } - unsafe { - DispatchBlock.Invoke ( - delegate { - var sc = SynchronizationContext.Current; + DispatchBlock.Invoke ( + delegate { + var sc = SynchronizationContext.Current; + if (sc == null) + SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); + try { + handler (); + } finally { if (sc == null) - SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); - try { - handler (); - } finally { - if (sc == null) - SynchronizationContext.SetSynchronizationContext (null); - } - }, block=> dispatch_source_set_event_handler (GetCheckedHandle (), block)); - } + SynchronizationContext.SetSynchronizationContext (null); + } + }, block=> dispatch_source_set_event_handler (GetCheckedHandle (), block)); } public void Suspend () @@ -133,20 +131,18 @@ public void SetRegistrationHandler (Action handler) if (handler == null) throw new ArgumentNullException ("handler"); - unsafe { - DispatchBlock.Invoke ( - delegate { - var sc = SynchronizationContext.Current; + DispatchBlock.Invoke ( + delegate { + var sc = SynchronizationContext.Current; + if (sc == null) + SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); + try { + handler (); + } finally { if (sc == null) - SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); - try { - handler (); - } finally { - if (sc == null) - SynchronizationContext.SetSynchronizationContext (null); - } - }, block => dispatch_source_set_registration_handler (GetCheckedHandle (), block)); - } + SynchronizationContext.SetSynchronizationContext (null); + } + }, block => dispatch_source_set_registration_handler (GetCheckedHandle (), block)); } public void SetCancelHandler (Action handler) @@ -154,20 +150,18 @@ public void SetCancelHandler (Action handler) if (handler == null) throw new ArgumentNullException ("handler"); - unsafe { - DispatchBlock.Invoke ( - delegate { - var sc = SynchronizationContext.Current; + DispatchBlock.Invoke ( + delegate { + var sc = SynchronizationContext.Current; + if (sc == null) + SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); + try { + handler (); + } finally { if (sc == null) - SynchronizationContext.SetSynchronizationContext (new DispatchQueueSynchronizationContext (queue)); - try { - handler (); - } finally { - if (sc == null) - SynchronizationContext.SetSynchronizationContext (null); - } - }, block => dispatch_source_set_cancel_handler (GetCheckedHandle (), block)); - } + SynchronizationContext.SetSynchronizationContext (null); + } + }, block => dispatch_source_set_cancel_handler (GetCheckedHandle (), block)); } public void Cancel () From 328dcacb796fd70526bbc2d99270e154cdc0c110 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Mon, 8 Oct 2018 07:44:04 +0200 Subject: [PATCH 8/8] Reintroduce DispatchObject.Check, since it's public API. --- src/CoreFoundation/Dispatch.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/CoreFoundation/Dispatch.cs b/src/CoreFoundation/Dispatch.cs index 1fd88b4da8da..d7e4bc64563f 100644 --- a/src/CoreFoundation/Dispatch.cs +++ b/src/CoreFoundation/Dispatch.cs @@ -31,6 +31,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; +using System.ComponentModel; using System.Runtime.InteropServices; using System.Threading; using ObjCRuntime; @@ -115,6 +116,15 @@ public override int GetHashCode () return (int) Handle; } +#if !XAMCORE_4_0 + [EditorBrowsable (EditorBrowsableState.Never)] + [Obsolete ("Use 'GetCheckedHandle' instead.")] + protected void Check () + { + GetCheckedHandle (); + } +#endif + [DllImport (Constants.libcLibrary)] extern static void dispatch_set_target_queue (/* dispatch_object_t */ IntPtr queue, /* dispatch_queue_t */ IntPtr target);