2 * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
21 #ifndef __DISPATCH_QUEUE__
22 #define __DISPATCH_QUEUE__
24 #ifndef __DISPATCH_INDIRECT__
25 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
26 #include <dispatch/base.h> // for HeaderDoc
32 * Dispatch is an abstract model for expressing concurrency via simple but
35 * At the core, dispatch provides serial FIFO queues to which blocks may be
36 * submitted. Blocks submitted to these dispatch queues are invoked on a pool
37 * of threads fully managed by the system. No guarantee is made regarding
38 * which thread a block will be invoked on; however, it is guaranteed that only
39 * one block submitted to the FIFO dispatch queue will be invoked at a time.
41 * When multiple queues have blocks to be processed, the system is free to
42 * allocate additional threads to invoke the blocks concurrently. When the
43 * queues become empty, these threads are automatically released.
47 * @typedef dispatch_queue_t
50 * Dispatch queues invoke blocks submitted to them serially in FIFO order. A
51 * queue will only invoke one block at a time, but independent queues may each
52 * invoke their blocks concurrently with respect to each other.
55 * Dispatch queues are lightweight objects to which blocks may be submitted.
56 * The system manages a pool of threads which process dispatch queues and
57 * invoke blocks submitted to them.
59 * Conceptually a dispatch queue may have its own thread of execution, and
60 * interaction between queues is highly asynchronous.
62 * Dispatch queues are reference counted via calls to dispatch_retain() and
63 * dispatch_release(). Pending blocks submitted to a queue also hold a
64 * reference to the queue until they have finished. Once all references to a
65 * queue have been released, the queue will be deallocated by the system.
67 DISPATCH_DECL(dispatch_queue
);
70 * @typedef dispatch_queue_attr_t
73 * Attribute and policy extensions for dispatch queues.
75 DISPATCH_DECL(dispatch_queue_attr
);
78 * @typedef dispatch_block_t
81 * The prototype of blocks submitted to dispatch queues, which take no
82 * arguments and have no return value.
85 * The declaration of a block allocates storage on the stack. Therefore, this
86 * is an invalid construct:
88 * dispatch_block_t block;
91 * block = ^{ printf("true\n"); };
93 * block = ^{ printf("false\n"); };
95 * block(); // unsafe!!!
97 * What is happening behind the scenes:
100 * struct Block __tmp_1 = ...; // setup details
103 * struct Block __tmp_2 = ...; // setup details
107 * As the example demonstrates, the address of a stack variable is escaping the
108 * scope in which it is allocated. That is a classic C bug.
111 typedef void (^dispatch_block_t
)(void);
117 * @function dispatch_async
120 * Submits a block for asynchronous execution on a dispatch queue.
123 * The dispatch_async() function is the fundamental mechanism for submitting
124 * blocks to a dispatch queue.
126 * Calls to dispatch_async() always return immediately after the block has
127 * been submitted, and never wait for the block to be invoked.
129 * The target queue determines whether the block will be invoked serially or
130 * concurrently with respect to other blocks submitted to that same queue.
131 * Serial queues are processed concurrently with with respect to each other.
134 * The target dispatch queue to which the block is submitted.
135 * The system will hold a reference on the target queue until the block
137 * The result of passing NULL in this parameter is undefined.
140 * The block to submit to the target dispatch queue. This function performs
141 * Block_copy() and Block_release() on behalf of callers.
142 * The result of passing NULL in this parameter is undefined.
145 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
146 DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
148 dispatch_async(dispatch_queue_t queue
, dispatch_block_t block
);
152 * @function dispatch_async_f
155 * Submits a function for asynchronous execution on a dispatch queue.
158 * See dispatch_async() for details.
161 * The target dispatch queue to which the function is submitted.
162 * The system will hold a reference on the target queue until the function
164 * The result of passing NULL in this parameter is undefined.
167 * The application-defined context parameter to pass to the function.
170 * The application-defined function to invoke on the target queue. The first
171 * parameter passed to this function is the context provided to
172 * dispatch_async_f().
173 * The result of passing NULL in this parameter is undefined.
175 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
176 DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
178 dispatch_async_f(dispatch_queue_t queue
,
180 dispatch_function_t work
);
183 * @function dispatch_sync
186 * Submits a block for synchronous execution on a dispatch queue.
189 * Submits a block to a dispatch queue like dispatch_async(), however
190 * dispatch_sync() will not return until the block has finished.
192 * Calls to dispatch_sync() targeting the current queue will result
193 * in dead-lock. Use of dispatch_sync() is also subject to the same
194 * multi-party dead-lock problems that may result from the use of a mutex.
195 * Use of dispatch_async() is preferred.
197 * Unlike dispatch_async(), no retain is performed on the target queue. Because
198 * calls to this function are synchronous, the dispatch_sync() "borrows" the
199 * reference of the caller.
201 * As an optimization, dispatch_sync() invokes the block on the current
202 * thread when possible.
205 * The target dispatch queue to which the block is submitted.
206 * The result of passing NULL in this parameter is undefined.
209 * The block to be invoked on the target dispatch queue.
210 * The result of passing NULL in this parameter is undefined.
213 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
214 DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
216 dispatch_sync(dispatch_queue_t queue
, dispatch_block_t block
);
220 * @function dispatch_sync_f
223 * Submits a function for synchronous execution on a dispatch queue.
226 * See dispatch_sync() for details.
229 * The target dispatch queue to which the function is submitted.
230 * The result of passing NULL in this parameter is undefined.
233 * The application-defined context parameter to pass to the function.
236 * The application-defined function to invoke on the target queue. The first
237 * parameter passed to this function is the context provided to
239 * The result of passing NULL in this parameter is undefined.
241 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
242 DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
244 dispatch_sync_f(dispatch_queue_t queue
,
246 dispatch_function_t work
);
249 * @function dispatch_apply
252 * Submits a block to a dispatch queue for multiple invocations.
255 * Submits a block to a dispatch queue for multiple invocations. This function
256 * waits for the task block to complete before returning. If the target queue
257 * is a concurrent queue returned by dispatch_get_concurrent_queue(), the block
258 * may be invoked concurrently, and it must therefore be reentrant safe.
260 * Each invocation of the block will be passed the current index of iteration.
263 * The number of iterations to perform.
266 * The target dispatch queue to which the block is submitted.
267 * The result of passing NULL in this parameter is undefined.
270 * The block to be invoked the specified number of iterations.
271 * The result of passing NULL in this parameter is undefined.
274 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
275 DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
277 dispatch_apply(size_t iterations
, dispatch_queue_t queue
, void (^block
)(size_t));
281 * @function dispatch_apply_f
284 * Submits a function to a dispatch queue for multiple invocations.
287 * See dispatch_apply() for details.
290 * The number of iterations to perform.
293 * The target dispatch queue to which the function is submitted.
294 * The result of passing NULL in this parameter is undefined.
297 * The application-defined context parameter to pass to the function.
300 * The application-defined function to invoke on the target queue. The first
301 * parameter passed to this function is the context provided to
302 * dispatch_apply_f(). The second parameter passed to this function is the
303 * current index of iteration.
304 * The result of passing NULL in this parameter is undefined.
306 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
307 DISPATCH_NONNULL2 DISPATCH_NONNULL4 DISPATCH_NOTHROW
309 dispatch_apply_f(size_t iterations
, dispatch_queue_t queue
,
311 void (*work
)(void *, size_t));
314 * @function dispatch_get_current_queue
317 * Returns the queue on which the currently executing block is running.
320 * Returns the queue on which the currently executing block is running.
322 * When dispatch_get_current_queue() is called outside of the context of a
323 * submitted block, it will return the default concurrent queue.
326 * Returns the current queue.
328 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
329 DISPATCH_PURE DISPATCH_WARN_RESULT DISPATCH_NOTHROW
331 dispatch_get_current_queue(void);
334 * @function dispatch_get_main_queue
337 * Returns the default queue that is bound to the main thread.
340 * In order to invoke blocks submitted to the main queue, the application must
341 * call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main
345 * Returns the main queue. This queue is created automatically on behalf of
346 * the main thread before main() is called.
348 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
349 extern struct dispatch_queue_s _dispatch_main_q
;
350 #define dispatch_get_main_queue() (&_dispatch_main_q)
353 * @enum dispatch_queue_priority_t
355 * @constant DISPATCH_QUEUE_PRIORITY_HIGH
356 * Items dispatched to the queue will run at high priority,
357 * i.e. the queue will be scheduled for execution before
358 * any default priority or low priority queue.
360 * @constant DISPATCH_QUEUE_PRIORITY_DEFAULT
361 * Items dispatched to the queue will run at the default
362 * priority, i.e. the queue will be scheduled for execution
363 * after all high priority queues have been scheduled, but
364 * before any low priority queues have been scheduled.
366 * @constant DISPATCH_QUEUE_PRIORITY_LOW
367 * Items dispatched to the queue will run at low priority,
368 * i.e. the queue will be scheduled for execution after all
369 * default priority and high priority queues have been
373 DISPATCH_QUEUE_PRIORITY_HIGH
= 2,
374 DISPATCH_QUEUE_PRIORITY_DEFAULT
= 0,
375 DISPATCH_QUEUE_PRIORITY_LOW
= -2,
379 * @function dispatch_get_global_queue
382 * Returns a well-known global concurrent queue of a given priority level.
385 * The well-known global concurrent queues may not be modified. Calls to
386 * dispatch_suspend(), dispatch_resume(), dispatch_set_context(), etc., will
387 * have no effect when used with queues returned by this function.
390 * A priority defined in dispatch_queue_priority_t
393 * Reserved for future use. Passing any value other than zero may result in
394 * a NULL return value.
397 * Returns the requested global queue.
399 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
400 DISPATCH_PURE DISPATCH_WARN_RESULT DISPATCH_NOTHROW
402 dispatch_get_global_queue(long priority
, unsigned long flags
);
405 * @function dispatch_queue_create
408 * Creates a new dispatch queue to which blocks may be submitted.
411 * Dispatch queues invoke blocks serially in FIFO order.
413 * When the dispatch queue is no longer needed, it should be released
414 * with dispatch_release(). Note that any pending blocks submitted
415 * to a queue will hold a reference to that queue. Therefore a queue
416 * will not be deallocated until all pending blocks have finished.
419 * A string label to attach to the queue.
420 * This parameter is optional and may be NULL.
423 * Unused. Pass NULL for now.
426 * The newly created dispatch queue.
428 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
429 DISPATCH_MALLOC DISPATCH_WARN_RESULT DISPATCH_NOTHROW
431 dispatch_queue_create(const char *label
, dispatch_queue_attr_t attr
);
434 * @function dispatch_queue_get_label
437 * Returns the label of the queue that was specified when the
441 * The result of passing NULL in this parameter is undefined.
444 * The label of the queue. The result may be NULL.
446 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
447 DISPATCH_NONNULL_ALL DISPATCH_PURE DISPATCH_WARN_RESULT DISPATCH_NOTHROW
449 dispatch_queue_get_label(dispatch_queue_t queue
);
452 * @function dispatch_set_target_queue
455 * Sets the target queue for the given object.
458 * An object's target queue is responsible for processing the object.
460 * A dispatch queue's priority is inherited by its target queue. Use the
461 * dispatch_get_global_queue() function to obtain suitable target queue
462 * of the desired priority.
464 * A dispatch source's target queue specifies where its event handler and
465 * cancellation handler blocks will be submitted.
467 * The result of calling dispatch_set_target_queue() on any other type of
468 * dispatch object is undefined.
471 * The object to modify.
472 * The result of passing NULL in this parameter is undefined.
475 * The new target queue for the object. The queue is retained, and the
476 * previous one, if any, is released.
477 * The result of passing NULL in this parameter is undefined.
479 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
480 DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
482 dispatch_set_target_queue(dispatch_object_t object
, dispatch_queue_t queue
);
485 * @function dispatch_main
488 * Execute blocks submitted to the main queue.
491 * This function "parks" the main thread and waits for blocks to be submitted
492 * to the main queue. This function never returns.
494 * Applications that call NSApplicationMain() or CFRunLoopRun() on the
495 * main thread do not need to call dispatch_main().
497 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
498 DISPATCH_NOTHROW DISPATCH_NORETURN
503 * @function dispatch_after
506 * Schedule a block for execution on a given queue at a specified time.
509 * Passing DISPATCH_TIME_NOW as the "when" parameter is supported, but not as
510 * optimal as calling dispatch_async() instead. Passing DISPATCH_TIME_FOREVER
514 * A temporal milestone returned by dispatch_time() or dispatch_walltime().
517 * A queue to which the given block will be submitted at the specified time.
518 * The result of passing NULL in this parameter is undefined.
521 * The block of code to execute.
522 * The result of passing NULL in this parameter is undefined.
525 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
526 DISPATCH_NONNULL2 DISPATCH_NONNULL3 DISPATCH_NOTHROW
528 dispatch_after(dispatch_time_t when
,
529 dispatch_queue_t queue
,
530 dispatch_block_t block
);
534 * @function dispatch_after_f
537 * Schedule a function for execution on a given queue at a specified time.
540 * See dispatch_after() for details.
543 * A temporal milestone returned by dispatch_time() or dispatch_walltime().
546 * A queue to which the given function will be submitted at the specified time.
547 * The result of passing NULL in this parameter is undefined.
550 * The application-defined context parameter to pass to the function.
553 * The application-defined function to invoke on the target queue. The first
554 * parameter passed to this function is the context provided to
555 * dispatch_after_f().
556 * The result of passing NULL in this parameter is undefined.
558 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
559 DISPATCH_NONNULL2 DISPATCH_NONNULL4 DISPATCH_NOTHROW
561 dispatch_after_f(dispatch_time_t when
,
562 dispatch_queue_t queue
,
564 dispatch_function_t work
);