]> git.saurik.com Git - apple/libdispatch.git/blob - man/dispatch_queue_create.3
libdispatch-703.1.4.tar.gz
[apple/libdispatch.git] / man / dispatch_queue_create.3
1 .\" Copyright (c) 2008-2012 Apple Inc. All rights reserved.
2 .Dd May 1, 2008
3 .Dt dispatch_queue_create 3
4 .Os Darwin
5 .Sh NAME
6 .Nm dispatch_queue_create ,
7 .Nm dispatch_queue_get_label ,
8 .Nm dispatch_get_current_queue ,
9 .Nm dispatch_get_global_queue ,
10 .Nm dispatch_get_main_queue ,
11 .Nm dispatch_main ,
12 .Nm dispatch_set_target_queue
13 .Nd where blocks are scheduled for execution
14 .Sh SYNOPSIS
15 .Fd #include <dispatch/dispatch.h>
16 .Ft dispatch_queue_t
17 .Fo dispatch_queue_create
18 .Fa "const char *label" "dispatch_queue_attr_t attr"
19 .Fc
20 .Ft "const char *"
21 .Fo dispatch_queue_get_label
22 .Fa "dispatch_queue_t queue"
23 .Fc
24 .Ft dispatch_queue_t
25 .Fo dispatch_get_global_queue
26 .Fa "long priority"
27 .Fa "unsigned long flags"
28 .Fc
29 .Ft dispatch_queue_t
30 .Fo dispatch_get_main_queue
31 .Fa void
32 .Fc
33 .Ft void
34 .Fo dispatch_main
35 .Fa void
36 .Fc
37 .Ft void
38 .Fo dispatch_set_target_queue
39 .Fa "dispatch_object_t object"
40 .Fa "dispatch_queue_t target"
41 .Fc
42 .Sh DESCRIPTION
43 Queues are the fundamental mechanism for scheduling blocks for execution within
44 the
45 .Xr dispatch 3
46 framework.
47 .Pp
48 All blocks submitted to dispatch queues are dequeued in FIFO order.
49 Queues created with the
50 .Dv DISPATCH_QUEUE_SERIAL
51 attribute wait for the previously dequeued block to complete before dequeuing
52 the next block. A queue with this FIFO completion behavior is usually simply
53 described as a "serial queue." All memory writes performed by a block dispatched
54 to a serial queue are guaranteed to be visible to subsequent blocks dispatched
55 to the same queue. Queues are not bound to any specific thread of execution and
56 blocks submitted to independent queues may execute concurrently.
57 .Pp
58 Queues created with the
59 .Dv DISPATCH_QUEUE_CONCURRENT
60 attribute may execute dequeued blocks concurrently and support barrier blocks
61 submitted with the dispatch barrier API.
62 .Sh CREATION
63 Queues are created with the
64 .Fn dispatch_queue_create
65 function. Queues, like all dispatch objects, are reference counted and newly
66 created queues have a reference count of one.
67 .Pp
68 The optional
69 .Fa label
70 argument is used to describe the purpose of the queue and is useful during
71 debugging and performance analysis. If a label is provided, it is copied.
72 By convention, clients should pass a reverse DNS style label. For example:
73 .Pp
74 .Bd -literal -offset indent
75 my_queue = dispatch_queue_create("com.example.subsystem.taskXYZ", NULL);
76 .Ed
77 .Pp
78 The
79 .Fa attr
80 argument specifies the type of queue to create and must be either
81 .Dv DISPATCH_QUEUE_SERIAL
82 or
83 .Dv DISPATCH_QUEUE_CONCURRENT .
84 .Pp
85 The
86 .Fn dispatch_queue_get_label
87 function returns the label provided when the given
88 .Fa queue
89 was created (or an empty C string if no label was provided at creation).
90 Passing the constant
91 .Dv DISPATCH_CURRENT_QUEUE_LABEL
92 to
93 .Fn dispatch_queue_get_label
94 returns the label of the current queue.
95 .Sh SUSPENSION
96 Queues may be temporarily suspended and resumed with the functions
97 .Fn dispatch_suspend
98 and
99 .Fn dispatch_resume
100 respectively. Suspension is checked prior to block execution and is
101 .Em not
102 preemptive.
103 .Sh MAIN QUEUE
104 The dispatch framework provides a default serial queue for the application to
105 use. This queue is accessed via the
106 .Fn dispatch_get_main_queue
107 function.
108 .Pp
109 Programs must call
110 .Fn dispatch_main
111 at the end of
112 .Fn main
113 in order to process blocks submitted to the main queue. (See the
114 .Sx COMPATIBILITY
115 section for exceptions.) The
116 .Fn dispatch_main
117 function never returns.
118 .Sh GLOBAL CONCURRENT QUEUES
119 Unlike the main queue or queues allocated with
120 .Fn dispatch_queue_create ,
121 the global concurrent queues schedule blocks as soon as threads become
122 available (non-FIFO completion order). Four global concurrent queues are
123 provided, representing the following priority bands:
124 .Bl -bullet -compact -offset indent
125 .It
126 DISPATCH_QUEUE_PRIORITY_HIGH
127 .It
128 DISPATCH_QUEUE_PRIORITY_DEFAULT
129 .It
130 DISPATCH_QUEUE_PRIORITY_LOW
131 .It
132 DISPATCH_QUEUE_PRIORITY_BACKGROUND
133 .El
134 .Pp
135 The priority of a global concurrent queue controls the scheduling priority of
136 the threads created by the system to invoke the blocks submitted to that queue.
137 Global queues with lower priority will be scheduled for execution after all
138 global queues with higher priority have been scheduled. Additionally, items on
139 the background priority global queue will execute on threads with background
140 state as described in
141 .Xr setpriority 2
142 (i.e.\& disk I/O is throttled and the thread's scheduling priority is set to
143 lowest value).
144 .Pp
145 Use the
146 .Fn dispatch_get_global_queue
147 function to obtain the global queue of given priority. The
148 .Fa flags
149 argument is reserved for future use and must be zero. Passing any value other
150 than zero may result in a NULL return value.
151 .Sh TARGET QUEUE
152 The
153 .Fn dispatch_set_target_queue
154 function updates the target queue of the given dispatch object. The target
155 queue of an object is responsible for processing the object.
156 .Pp
157 The new target queue is retained by the given object before the previous target
158 queue is released. The new target queue setting will take effect between block
159 executions on the object, but not in the middle of any existing block executions
160 (non-preemptive).
161 .Pp
162 The default target queue of all dispatch objects created by the application is
163 the default priority global concurrent queue. To reset an object's target queue
164 to the default, pass the
165 .Dv DISPATCH_TARGET_QUEUE_DEFAULT
166 constant to
167 .Fn dispatch_set_target_queue .
168 .Pp
169 The priority of a dispatch queue is inherited from its target queue.
170 In order to change the priority of a queue created with
171 .Fn dispatch_queue_create ,
172 use the
173 .Fn dispatch_get_global_queue
174 function to obtain a target queue of the desired priority.
175 .Pp
176 Blocks submitted to a serial queue whose target queue is another serial queue
177 will not be invoked concurrently with blocks submitted to the target queue or
178 to any other queue with that same target queue.
179 .Pp
180 The target queue of a dispatch source specifies where its event handler and
181 cancellation handler blocks will be submitted. See
182 .Xr dispatch_source_create 3
183 for more information about dispatch sources.
184 .Pp
185 The target queue of a dispatch I/O channel specifies the priority of the global
186 queue where its I/O operations are executed. See
187 .Xr dispatch_io_create 3
188 for more information about dispatch I/O channels.
189 .Pp
190 For all other dispatch object types, the only function of the target queue is
191 to determine where an object's finalizer function is invoked.
192 .Pp
193 The result of passing the main queue or a global concurrent queue as the first
194 argument of
195 .Fn dispatch_set_target_queue
196 is undefined.
197 .Pp
198 Directly or indirectly setting the target queue of a dispatch queue to itself is
199 undefined.
200 .Sh DEPRECATED FUNCTIONS
201 The following functions are deprecated and will be removed in a future release:
202 .Bl -item
203 .It
204 .Ft dispatch_queue_t
205 .Fn dispatch_get_current_queue void ;
206 .El
207 .Pp
208 .Fn dispatch_get_current_queue
209 always returns a valid queue. When called from within a block
210 submitted to a dispatch queue, that queue will be returned. If this function is
211 called from the main thread before
212 .Fn dispatch_main
213 is called, then the result of
214 .Fn dispatch_get_main_queue
215 is returned. In all other cases, the default target queue will be returned.
216 .Pp
217 The use of
218 .Fn dispatch_get_current_queue
219 is strongly discouraged except for debugging and logging purposes. Code must not
220 make any assumptions about the queue returned, unless it is one of the global
221 queues or a queue the code has itself created. The returned queue may have
222 arbitrary policies that may surprise code that tries to schedule work with the
223 queue. The list of policies includes, but is not limited to, queue width (i.e.
224 serial vs. concurrent), scheduling priority, security credential or filesystem
225 configuration. This function is deprecated and will be removed in a future
226 release.
227 .Pp
228 It is equally unsafe for code to assume that synchronous execution onto a queue
229 is safe from deadlock if that queue is not the one returned by
230 .Fn dispatch_get_current_queue .
231 .Pp
232 The result of
233 .Fn dispatch_get_main_queue
234 may or may not equal the result of
235 .Fn dispatch_get_current_queue
236 when called on the main thread. Comparing the two is not a valid way to test
237 whether code is executing on the main thread. Foundation/AppKit programs should
238 use [NSThread isMainThread]. POSIX programs may use
239 .Xr pthread_main_np 3 .
240 .Pp
241 .Fn dispatch_get_current_queue
242 may return a queue owned by a different subsystem which has already had all
243 external references to it released. While such a queue will continue to exist
244 until all blocks submitted to it have completed, attempting to retain it is
245 forbidden and will trigger an assertion. If Objective-C Automatic Reference
246 Counting is enabled, any use of the object returned by
247 .Fn dispatch_get_current_queue
248 will cause retain calls to be automatically generated, so the use of
249 .Fn dispatch_get_current_queue
250 for any reason in code built with ARC is particularly strongly discouraged.
251 .Sh COMPATIBILITY
252 Cocoa applications need not call
253 .Fn dispatch_main .
254 Blocks submitted to the main queue will be executed as part of the "common
255 modes" of the application's main NSRunLoop or CFRunLoop.
256 However, blocks submitted to the main queue in applications using
257 .Fn dispatch_main
258 are not guaranteed to execute on the main thread.
259 .Pp
260 The dispatch framework is a pure C level API. As a result, it does not catch
261 exceptions generated by higher level languages such as Objective-C or C++.
262 Applications
263 .Em MUST
264 catch all exceptions before returning from a block submitted to a dispatch
265 queue; otherwise the process will be terminated with an uncaught exception.
266 .Pp
267 The dispatch framework manages the relationship between dispatch queues and
268 threads of execution. As a result, applications
269 .Em MUST NOT
270 delete or mutate objects that they did not create. The following interfaces
271 .Em MUST NOT
272 be called by blocks submitted to a dispatch queue:
273 .Bl -bullet -offset indent
274 .It
275 .Fn pthread_cancel
276 .It
277 .Fn pthread_detach
278 .It
279 .Fn pthread_join
280 .It
281 .Fn pthread_kill
282 .It
283 .Fn pthread_exit
284 .El
285 .Pp
286 Applications
287 .Em MAY
288 call the following interfaces from a block submitted to a dispatch queue if
289 and only if they restore the thread to its original state before returning:
290 .Bl -bullet -offset indent
291 .It
292 .Fn pthread_setcancelstate
293 .It
294 .Fn pthread_setcanceltype
295 .It
296 .Fn pthread_setschedparam
297 .It
298 .Fn pthread_sigmask
299 .It
300 .Fn pthread_setugid_np
301 .El
302 .Pp
303 Applications
304 .Em MUST NOT
305 rely on the following interfaces returning predictable results between
306 invocations of blocks submitted to a dispatch queue:
307 .Bl -bullet -offset indent
308 .It
309 .Fn pthread_self
310 .It
311 .Fn pthread_getschedparam
312 .It
313 .Fn pthread_get_stacksize_np
314 .It
315 .Fn pthread_get_stackaddr_np
316 .It
317 .Fn pthread_mach_thread_np
318 .It
319 .Fn pthread_from_mach_thread_np
320 .El
321 .Pp
322 While the result of
323 .Fn pthread_self
324 may change between invocations of blocks, the value will not change during the
325 execution of any single block. Because the underlying thread may change beteween
326 block invocations on a single queue, using per-thread data as an out-of-band
327 return value is error prone. In other words, the result of calling
328 .Fn pthread_setspecific
329 and
330 .Fn pthread_getspecific
331 is well defined within a signle block, but not across multiple blocks. Also,
332 one cannot make any assumptions about when the destructor passed to
333 .Fn pthread_key_create
334 is called. The destructor may be called between the invocation of blocks on
335 the same queue, or during the idle state of a process.
336 .Pp
337 The following example code correctly handles per-thread return values:
338 .Bd -literal -offset indent
339 __block int r;
340 __block int e;
341 dispatch_sync(queue, ^{
342 r = kill(1, 0);
343 // Copy the per-thread return value to the callee thread
344 e = errno;
345 });
346 printf("kill(1,0) returned %d and errno %d\n", r, e);
347 .Ed
348 .Pp
349 Note that in the above example
350 .Va errno
351 is a per-thread variable and must be copied out explicitly as the block may be
352 invoked on different thread of execution than the caller. Another example of
353 per-thread data that would need to be copied is the use of
354 .Fn getpwnam
355 instead of
356 .Fn getpwnam_r .
357 .Pp
358 As an optimization,
359 .Fn dispatch_sync
360 invokes the block on the current thread when possible. In this case, the thread
361 specific data such as
362 .Va errno
363 may persist from the block until back to the caller. Great care should be taken
364 not to accidentally rely on this side-effect.
365 .Pp
366 .Sh SEE ALSO
367 .Xr dispatch 3 ,
368 .Xr dispatch_async 3 ,
369 .Xr dispatch_object 3 ,
370 .Xr dispatch_source_create 3