1 .\" Copyright (c) 2008-2012 Apple Inc. All rights reserved.
7 .Nd General manipulation of dispatch objects
9 .Fd #include <dispatch/dispatch.h>
12 .Fa "dispatch_object_t object"
16 .Fa "dispatch_object_t object"
20 .Fa "dispatch_object_t object"
24 .Fa "dispatch_object_t object"
27 .Fo dispatch_get_context
28 .Fa "dispatch_object_t object"
31 .Fo dispatch_set_context
32 .Fa "dispatch_object_t object"
36 .Fo dispatch_set_finalizer_f
37 .Fa "dispatch_object_t object"
38 .Fa "dispatch_function_t finalizer"
41 Dispatch objects share functions for coordinating memory management, suspension,
42 cancellation and context pointers.
44 Objects returned by creation functions in the dispatch framework may be
45 uniformly retained and released with the functions
51 The dispatch framework does not guarantee that any given client has the last or
52 only reference to a given object. Objects may be retained internally by the
54 .Ss INTEGRATION WITH OBJECTIVE-C
55 .Bd -filled -offset indent
56 When building with an Objective-C or Objective-C++ compiler, dispatch objects
57 are declared as Objective-C types. This results in the following differences
58 compared to building as plain C/C++:
61 if Objective-C Automated Reference Counting is enabled, dispatch objects are
62 memory managed by the Objective-C runtime and explicit calls to the
66 functions will produce build errors.
69 when ARC is enabled, care needs to be taken with dispatch API returning an
70 interior pointer that is only valid as long as an associated object has not
71 been released. If that object is held in a variable with automatic storage, it
72 may need to be annotated with the
73 .Li objc_precise_lifetime
74 attribute, or stored in a
76 instance variable instead, to ensure that the object is not prematurely
77 released. The functions returning interior pointers are
78 .Xr dispatch_data_create_map 3
80 .Xr dispatch_data_apply 3 .
82 the Blocks runtime automatically retains and releases dispatch objects captured
87 e.g.\& as performed during asynchronous execution of a block via
88 .Xr dispatch_async 3 .
91 retain cycles may be encountered if dispatch source objects are captured by
92 their handler blocks; these cycles can be broken by declaring the captured
96 .Xr dispatch_source_cancel 3
97 to cause its handler blocks to be released explicitly.
99 dispatch objects can be added directly to Cocoa collections, and their
100 lifetime is tracked by the Objective-C static analyzer.
103 Integration of dispatch objects with Objective-C requires targeting Mac\ OS\ X
104 10.8 or later, and is disabled when building with Objective-C Garbage
105 Collection or for the legacy Objective-C runtime. It can also be disabled
106 manually by using compiler options to define the
107 .Dv OS_OBJECT_USE_OBJC
108 preprocessor macro to
113 When building with a plain C/C++ compiler or when integration with Objective-C
114 is disabled, dispatch objects are
116 automatically retained and released when captured by a block. Therefore, when a
117 dispatch object is captured by a block that will be executed asynchronously,
118 the object must be manually retained and released:
120 .Bd -literal -offset indent
121 dispatch_retain(object);
122 dispatch_async(queue, ^{
123 do_something_with_object(object);
124 dispatch_release(object);
128 The invocation of blocks on dispatch queues or dispatch sources may be suspended
129 or resumed with the functions
133 respectively. Other dispatch objects do not support suspension.
135 The dispatch framework always checks the suspension status before executing a
136 block, but such changes never affect a block during execution (non-preemptive).
137 Therefore the suspension of an object is asynchronous, unless it is performed
138 from the context of the target queue for the given object.
139 The result of suspending or resuming an object that is not a dispatch queue or
140 a dispatch source is undefined.
143 suspension applies to all aspects of the dispatch object life cycle, including
144 the finalizer function and cancellation handler. Suspending an object causes it
145 to be retained and resuming an object causes it to be released. Therefore it is
146 important to balance calls to
150 such that the dispatch object is fully resumed when the last reference is
151 released. The result of releasing all references to a dispatch object while in
152 a suspended state is undefined.
154 Dispatch objects support supplemental context pointers. The value of the
155 context pointer may be retrieved and updated with
156 .Fn dispatch_get_context
158 .Fn dispatch_set_context
161 .Fn dispatch_set_finalizer_f
162 specifies an optional per-object finalizer function that is invoked
163 asynchronously if the context pointer is not NULL when the last
164 reference to the object is released.
166 application an opportunity to free the context data associated with the object.
167 The finalizer will be run on the object's target queue.
170 .Xr dispatch_async 3 ,
171 .Xr dispatch_group_create 3 ,
172 .Xr dispatch_queue_create 3 ,
173 .Xr dispatch_semaphore_create 3 ,
174 .Xr dispatch_set_target_queue 3 ,
175 .Xr dispatch_source_cancel 3 ,
176 .Xr dispatch_source_create 3