]> git.saurik.com Git - apple/libdispatch.git/blame_incremental - man/dispatch_object.3
libdispatch-913.30.4.tar.gz
[apple/libdispatch.git] / man / dispatch_object.3
... / ...
CommitLineData
1.\" Copyright (c) 2008-2012 Apple Inc. All rights reserved.
2.Dd March 1, 2012
3.Dt dispatch_object 3
4.Os Darwin
5.Sh NAME
6.Nm dispatch_object
7.Nd General manipulation of dispatch objects
8.Sh SYNOPSIS
9.Fd #include <dispatch/dispatch.h>
10.Ft void
11.Fo dispatch_retain
12.Fa "dispatch_object_t object"
13.Fc
14.Ft void
15.Fo dispatch_release
16.Fa "dispatch_object_t object"
17.Fc
18.Ft void
19.Fo dispatch_suspend
20.Fa "dispatch_object_t object"
21.Fc
22.Ft void
23.Fo dispatch_resume
24.Fa "dispatch_object_t object"
25.Fc
26.Ft void
27.Fo dispatch_activate
28.Fa "dispatch_object_t object"
29.Fc
30.Ft "void *"
31.Fo dispatch_get_context
32.Fa "dispatch_object_t object"
33.Fc
34.Ft void
35.Fo dispatch_set_context
36.Fa "dispatch_object_t object"
37.Fa "void *context"
38.Fc
39.Ft void
40.Fo dispatch_set_finalizer_f
41.Fa "dispatch_object_t object"
42.Fa "dispatch_function_t finalizer"
43.Fc
44.Sh DESCRIPTION
45Dispatch objects share functions for coordinating memory management, suspension,
46cancellation and context pointers.
47.Sh MEMORY MANAGEMENT
48Objects returned by creation functions in the dispatch framework may be
49uniformly retained and released with the functions
50.Fn dispatch_retain
51and
52.Fn dispatch_release
53respectively.
54.Pp
55The dispatch framework does not guarantee that any given client has the last or
56only reference to a given object. Objects may be retained internally by the
57system.
58.Ss INTEGRATION WITH OBJECTIVE-C
59.Bd -filled -offset indent
60When building with an Objective-C or Objective-C++ compiler, dispatch objects
61are declared as Objective-C types. This results in the following differences
62compared to building as plain C/C++:
63.Bl -dash
64.It
65if Objective-C Automated Reference Counting is enabled, dispatch objects are
66memory managed by the Objective-C runtime and explicit calls to the
67.Fn dispatch_retain
68and
69.Fn dispatch_release
70functions will produce build errors.
71.Pp
72.Em Note :
73when ARC is enabled, care needs to be taken with dispatch API returning an
74interior pointer that is only valid as long as an associated object has not
75been released. If that object is held in a variable with automatic storage, it
76may need to be annotated with the
77.Li objc_precise_lifetime
78attribute, or stored in a
79.Li __strong
80instance variable instead, to ensure that the object is not prematurely
81released. The functions returning interior pointers are
82.Xr dispatch_data_create_map 3
83and
84.Xr dispatch_data_apply 3 .
85.It
86the Blocks runtime automatically retains and releases dispatch objects captured
87by blocks upon
88.Fn Block_copy
89and
90.Fn Block_release ,
91e.g.\& as performed during asynchronous execution of a block via
92.Xr dispatch_async 3 .
93.Pp
94.Em Note :
95retain cycles may be encountered if dispatch source objects are captured by
96their handler blocks; these cycles can be broken by declaring the captured
97object
98.Li __weak
99or by calling
100.Xr dispatch_source_cancel 3
101to cause its handler blocks to be released explicitly.
102.It
103dispatch objects can be added directly to Cocoa collections, and their
104lifetime is tracked by the Objective-C static analyzer.
105.El
106.Pp
107Integration of dispatch objects with Objective-C requires targeting Mac\ OS\ X
10810.8 or later, and is disabled when building for the legacy Objective-C runtime.
109It can also be disabled manually by using compiler options to define the
110.Dv OS_OBJECT_USE_OBJC
111preprocessor macro to
112.Li 0 .
113.Ed
114.Pp
115.Em Important :
116When building with a plain C/C++ compiler or when integration with Objective-C
117is disabled, dispatch objects are
118.Em not
119automatically retained and released when captured by a block. Therefore, when a
120dispatch object is captured by a block that will be executed asynchronously,
121the object must be manually retained and released:
122.Pp
123.Bd -literal -offset indent
124dispatch_retain(object);
125dispatch_async(queue, ^{
126 do_something_with_object(object);
127 dispatch_release(object);
128});
129.Ed
130.Sh ACTIVATION
131Dispatch objects such as queues and sources may be created in an inactive
132state. Objects in this state must be activated before any blocks
133associated with them will be invoked. Calling
134.Fn dispatch_activate
135on an active object has no effect.
136.Pp
137Changing attributes such as the target queue or a source handler is no longer permitted
138once the object has been activated (see
139.Xr dispatch_set_target_queue 3 ,
140.Xr dispatch_source_set_event_handler 3 ).
141.Sh SUSPENSION
142The invocation of blocks on dispatch queues or dispatch sources may be suspended
143or resumed with the functions
144.Fn dispatch_suspend
145and
146.Fn dispatch_resume
147respectively. Other dispatch objects do not support suspension.
148.Pp
149The dispatch framework always checks the suspension status before executing a
150block, but such changes never affect a block during execution (non-preemptive).
151Therefore the suspension of an object is asynchronous, unless it is performed
152from the context of the target queue for the given object.
153The result of suspending or resuming an object that is not a dispatch queue or
154a dispatch source is undefined.
155.Pp
156.Em Important :
157suspension applies to all aspects of the dispatch object life cycle, including
158the finalizer function and cancellation handler. Suspending an object causes it
159to be retained and resuming an object causes it to be released. Therefore it is
160important to balance calls to
161.Fn dispatch_suspend
162and
163.Fn dispatch_resume
164such that the dispatch object is fully resumed when the last reference is
165released. The result of releasing all references to a dispatch object while in
166an inactive or suspended state is undefined.
167.Sh CONTEXT POINTERS
168Dispatch objects support supplemental context pointers. The value of the
169context pointer may be retrieved and updated with
170.Fn dispatch_get_context
171and
172.Fn dispatch_set_context
173respectively.
174The
175.Fn dispatch_set_finalizer_f
176specifies an optional per-object finalizer function that is invoked
177asynchronously if the context pointer is not NULL when the last
178reference to the object is released.
179This gives the
180application an opportunity to free the context data associated with the object.
181The finalizer will be run on the object's target queue.
182.Sh SEE ALSO
183.Xr dispatch 3 ,
184.Xr dispatch_async 3 ,
185.Xr dispatch_group_create 3 ,
186.Xr dispatch_queue_create 3 ,
187.Xr dispatch_semaphore_create 3 ,
188.Xr dispatch_set_target_queue 3 ,
189.Xr dispatch_source_cancel 3 ,
190.Xr dispatch_source_create 3