3131// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3232//
3333using System ;
34+ using System . ComponentModel ;
3435using System . Runtime . InteropServices ;
3536using System . Threading ;
3637using ObjCRuntime ;
@@ -47,26 +48,18 @@ public enum DispatchQueuePriority : int {
4748 Background = Int16 . MinValue
4849 }
4950
50- public abstract class DispatchObject : INativeObject
51- #if ! COREBUILD
52- , IDisposable
53- #endif
51+ public abstract class DispatchObject : NativeObject
5452 {
5553#if ! COREBUILD
56- internal IntPtr handle ;
57-
5854 //
5955 // Constructors and lifecycle
6056 //
6157 [ Preserve ( Conditional = true ) ]
6258 internal DispatchObject ( IntPtr handle , bool owns )
59+ : base ( handle , owns )
6360 {
6461 if ( handle == IntPtr . Zero )
6562 throw new ArgumentNullException ( "handle" ) ;
66-
67- this . handle = handle ;
68- if ( ! owns )
69- dispatch_retain ( handle ) ;
7063 }
7164
7265 internal DispatchObject ( )
@@ -79,27 +72,14 @@ internal DispatchObject ()
7972 [ DllImport ( Constants . libcLibrary ) ]
8073 extern static IntPtr dispatch_retain ( IntPtr o ) ;
8174
82- ~ DispatchObject ( )
83- {
84- Dispose ( false ) ;
85- }
86-
87- public void Dispose ( )
75+ protected override void Retain ( )
8876 {
89- Dispose ( true ) ;
90- GC . SuppressFinalize ( this ) ;
77+ dispatch_retain ( Handle ) ;
9178 }
9279
93- public IntPtr Handle {
94- get { return handle ; }
95- }
96-
97- protected virtual void Dispose ( bool disposing )
80+ protected override void Release ( )
9881 {
99- if ( handle != IntPtr . Zero ) {
100- dispatch_release ( handle ) ;
101- handle = IntPtr . Zero ;
102- }
82+ dispatch_release ( Handle ) ;
10383 }
10484
10585 public static bool operator == ( DispatchObject a , DispatchObject b )
@@ -114,7 +94,7 @@ protected virtual void Dispose (bool disposing)
11494 } else {
11595 if ( ob == null )
11696 return false ;
117- return a . handle == b . handle ;
97+ return a . Handle == b . Handle ;
11898 }
11999 }
120100
@@ -128,19 +108,22 @@ public override bool Equals (object other)
128108 var od = other as DispatchQueue ;
129109 if ( od == null )
130110 return false ;
131- return od . handle == handle ;
111+ return od . Handle == Handle ;
132112 }
133113
134114 public override int GetHashCode ( )
135115 {
136- return ( int ) handle ;
116+ return ( int ) Handle ;
137117 }
138118
119+ #if ! XAMCORE_4_0
120+ [ EditorBrowsable ( EditorBrowsableState . Never ) ]
121+ [ Obsolete ( "Use 'GetCheckedHandle' instead." ) ]
139122 protected void Check ( )
140123 {
141- if ( handle == IntPtr . Zero )
142- throw new ObjectDisposedException ( GetType ( ) . ToString ( ) ) ;
143- }
124+ GetCheckedHandle ( ) ;
125+ }
126+ #endif
144127
145128 [ DllImport ( Constants . libcLibrary ) ]
146129 extern static void dispatch_set_target_queue ( /* dispatch_object_t */ IntPtr queue , /* dispatch_queue_t */ IntPtr target ) ;
@@ -149,7 +132,7 @@ public void SetTargetQueue (DispatchQueue queue)
149132 {
150133 // note: null is allowed because DISPATCH_TARGET_QUEUE_DEFAULT is defined as NULL (dispatch/queue.h)
151134 IntPtr q = queue == null ? IntPtr . Zero : queue . Handle ;
152- dispatch_set_target_queue ( handle , q ) ;
135+ dispatch_set_target_queue ( Handle , q ) ;
153136 }
154137
155138 [ DllImport ( Constants . libcLibrary ) ]
@@ -171,11 +154,10 @@ public DispatchQueue (IntPtr handle) : base (handle, false)
171154 {
172155 }
173156
174- public DispatchQueue ( string label ) : base ( )
157+ public DispatchQueue ( string label )
158+ : base ( dispatch_queue_create ( label , IntPtr . Zero ) , true )
175159 {
176- // Initialized in owned state for the queue.
177- handle = dispatch_queue_create ( label , IntPtr . Zero ) ;
178- if ( handle == IntPtr . Zero )
160+ if ( Handle == IntPtr . Zero )
179161 throw new Exception ( "Error creating dispatch queue" ) ;
180162 }
181163
@@ -189,9 +171,9 @@ static IntPtr ConcurrentQueue {
189171 }
190172
191173 public DispatchQueue ( string label , bool concurrent )
174+ : base ( dispatch_queue_create ( label , concurrent ? ConcurrentQueue : IntPtr . Zero ) , true )
192175 {
193- handle = dispatch_queue_create ( label , concurrent ? ConcurrentQueue : IntPtr . Zero ) ;
194- if ( handle == IntPtr . Zero )
176+ if ( Handle == IntPtr . Zero )
195177 throw new Exception ( "Error creating dispatch queue" ) ;
196178 }
197179
@@ -201,10 +183,7 @@ public DispatchQueue (string label, bool concurrent)
201183
202184 public string Label {
203185 get {
204- if ( handle == IntPtr . Zero )
205- throw new ObjectDisposedException ( "DispatchQueue" ) ;
206-
207- return Marshal . PtrToStringAnsi ( dispatch_queue_get_label ( handle ) ) ;
186+ return Marshal . PtrToStringAnsi ( dispatch_queue_get_label ( GetCheckedHandle ( ) ) ) ;
208187 }
209188 }
210189
@@ -217,14 +196,12 @@ public static string CurrentQueueLabel {
217196
218197 public void Suspend ( )
219198 {
220- Check ( ) ;
221- dispatch_suspend ( handle ) ;
199+ dispatch_suspend ( GetCheckedHandle ( ) ) ;
222200 }
223201
224202 public void Resume ( )
225203 {
226- Check ( ) ;
227- dispatch_resume ( handle ) ;
204+ dispatch_resume ( GetCheckedHandle ( ) ) ;
228205 }
229206
230207 [ DllImport ( Constants . libcLibrary ) ]
@@ -238,12 +215,10 @@ public void Resume ()
238215
239216 public IntPtr Context {
240217 get {
241- Check ( ) ;
242- return dispatch_get_context ( handle ) ;
218+ return dispatch_get_context ( GetCheckedHandle ( ) ) ;
243219 }
244220 set {
245- Check ( ) ;
246- dispatch_set_context ( handle , value ) ;
221+ dispatch_set_context ( GetCheckedHandle ( ) , value ) ;
247222 }
248223 }
249224
@@ -350,38 +325,38 @@ public void DispatchAsync (Action action)
350325 if ( action == null )
351326 throw new ArgumentNullException ( "action" ) ;
352327
353- dispatch_async_f ( handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , this ) ) , static_dispatch ) ;
328+ dispatch_async_f ( Handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , this ) ) , static_dispatch ) ;
354329 }
355330
356331 public void DispatchSync ( Action action )
357332 {
358333 if ( action == null )
359334 throw new ArgumentNullException ( "action" ) ;
360335
361- dispatch_sync_f ( handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , this ) ) , static_dispatch ) ;
336+ dispatch_sync_f ( Handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , this ) ) , static_dispatch ) ;
362337 }
363338
364339 public void DispatchBarrierAsync ( Action action )
365340 {
366341 if ( action == null )
367342 throw new ArgumentNullException ( "action" ) ;
368343
369- dispatch_barrier_async_f ( handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , this ) ) , static_dispatch ) ;
344+ dispatch_barrier_async_f ( Handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , this ) ) , static_dispatch ) ;
370345 }
371346
372347 public void DispatchAfter ( DispatchTime when , Action action )
373348 {
374349 if ( action == null )
375350 throw new ArgumentNullException ( "action" ) ;
376351
377- dispatch_after_f ( when . Nanoseconds , handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , this ) ) , static_dispatch ) ;
352+ dispatch_after_f ( when . Nanoseconds , Handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , this ) ) , static_dispatch ) ;
378353 }
379354
380355 public void Submit ( Action < int > action , long times )
381356 {
382357 if ( action == null )
383358 throw new ArgumentNullException ( "action" ) ;
384- dispatch_apply_f ( ( IntPtr ) times , handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , this ) ) , static_dispatch_iterations ) ;
359+ dispatch_apply_f ( ( IntPtr ) times , Handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , this ) ) , static_dispatch_iterations ) ;
385360 }
386361
387362 //
@@ -418,7 +393,7 @@ public override bool Equals (object other)
418393 DispatchQueue o = other as DispatchQueue ;
419394 if ( o == null )
420395 return false ;
421- return ( o . Handle == handle ) ;
396+ return ( o . Handle == Handle ) ;
422397 }
423398
424399 public static bool operator == ( DispatchQueue left , DispatchQueue right )
@@ -437,7 +412,7 @@ public override bool Equals (object other)
437412
438413 public override int GetHashCode ( )
439414 {
440- return ( int ) handle ;
415+ return ( int ) Handle ;
441416 }
442417
443418#if MONOMAC
@@ -524,8 +499,7 @@ public void DispatchAsync (DispatchQueue queue, Action action)
524499 if ( action == null )
525500 throw new ArgumentNullException ( "action" ) ;
526501
527- Check ( ) ;
528- dispatch_group_async_f ( handle , queue . handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , queue ) ) , DispatchQueue . static_dispatch ) ;
502+ dispatch_group_async_f ( GetCheckedHandle ( ) , queue . Handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , queue ) ) , DispatchQueue . static_dispatch ) ;
529503 }
530504
531505 public void Notify ( DispatchQueue queue , Action action )
@@ -535,26 +509,22 @@ public void Notify (DispatchQueue queue, Action action)
535509 if ( action == null )
536510 throw new ArgumentNullException ( "action" ) ;
537511
538- Check ( ) ;
539- dispatch_group_notify_f ( handle , queue . handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , queue ) ) , DispatchQueue . static_dispatch ) ;
512+ dispatch_group_notify_f ( GetCheckedHandle ( ) , queue . Handle , ( IntPtr ) GCHandle . Alloc ( Tuple . Create ( action , queue ) ) , DispatchQueue . static_dispatch ) ;
540513 }
541514
542515 public void Enter ( )
543516 {
544- Check ( ) ;
545- dispatch_group_enter ( handle ) ;
517+ dispatch_group_enter ( GetCheckedHandle ( ) ) ;
546518 }
547519
548520 public void Leave ( )
549521 {
550- Check ( ) ;
551- dispatch_group_leave ( handle ) ;
522+ dispatch_group_leave ( GetCheckedHandle ( ) ) ;
552523 }
553524
554525 public bool Wait ( DispatchTime timeout )
555526 {
556- Check ( ) ;
557- return dispatch_group_wait ( handle , timeout . Nanoseconds ) == 0 ;
527+ return dispatch_group_wait ( GetCheckedHandle ( ) , timeout . Nanoseconds ) == 0 ;
558528 }
559529
560530 [ DllImport ( Constants . libcLibrary ) ]
0 commit comments