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