]> git.saurik.com Git - apple/libdispatch.git/blob - dispatch/queue.h
libdispatch-228.18.tar.gz
[apple/libdispatch.git] / dispatch / queue.h
1 /*
2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21 #ifndef __DISPATCH_QUEUE__
22 #define __DISPATCH_QUEUE__
23
24 #ifndef __DISPATCH_INDIRECT__
25 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
26 #include <dispatch/base.h> // for HeaderDoc
27 #endif
28
29 /*!
30 * @header
31 *
32 * Dispatch is an abstract model for expressing concurrency via simple but
33 * powerful API.
34 *
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.
40 *
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.
44 */
45
46 /*!
47 * @typedef dispatch_queue_t
48 *
49 * @abstract
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.
53 *
54 * @discussion
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.
58 *
59 * Conceptually a dispatch queue may have its own thread of execution, and
60 * interaction between queues is highly asynchronous.
61 *
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.
66 */
67 DISPATCH_DECL(dispatch_queue);
68
69 /*!
70 * @typedef dispatch_queue_attr_t
71 *
72 * @abstract
73 * Attribute for dispatch queues.
74 */
75 DISPATCH_DECL(dispatch_queue_attr);
76
77 /*!
78 * @typedef dispatch_block_t
79 *
80 * @abstract
81 * The prototype of blocks submitted to dispatch queues, which take no
82 * arguments and have no return value.
83 *
84 * @discussion
85 * The declaration of a block allocates storage on the stack. Therefore, this
86 * is an invalid construct:
87 *
88 * dispatch_block_t block;
89 *
90 * if (x) {
91 * block = ^{ printf("true\n"); };
92 * } else {
93 * block = ^{ printf("false\n"); };
94 * }
95 * block(); // unsafe!!!
96 *
97 * What is happening behind the scenes:
98 *
99 * if (x) {
100 * struct Block __tmp_1 = ...; // setup details
101 * block = &__tmp_1;
102 * } else {
103 * struct Block __tmp_2 = ...; // setup details
104 * block = &__tmp_2;
105 * }
106 *
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.
109 */
110 #ifdef __BLOCKS__
111 typedef void (^dispatch_block_t)(void);
112 #endif
113
114 __BEGIN_DECLS
115
116 /*!
117 * @function dispatch_async
118 *
119 * @abstract
120 * Submits a block for asynchronous execution on a dispatch queue.
121 *
122 * @discussion
123 * The dispatch_async() function is the fundamental mechanism for submitting
124 * blocks to a dispatch queue.
125 *
126 * Calls to dispatch_async() always return immediately after the block has
127 * been submitted, and never wait for the block to be invoked.
128 *
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 respect to each other.
132 *
133 * @param queue
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
136 * has finished.
137 * The result of passing NULL in this parameter is undefined.
138 *
139 * @param block
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.
143 */
144 #ifdef __BLOCKS__
145 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
146 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
147 void
148 dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
149 #endif
150
151 /*!
152 * @function dispatch_async_f
153 *
154 * @abstract
155 * Submits a function for asynchronous execution on a dispatch queue.
156 *
157 * @discussion
158 * See dispatch_async() for details.
159 *
160 * @param queue
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
163 * has returned.
164 * The result of passing NULL in this parameter is undefined.
165 *
166 * @param context
167 * The application-defined context parameter to pass to the function.
168 *
169 * @param work
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.
174 */
175 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
176 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
177 void
178 dispatch_async_f(dispatch_queue_t queue,
179 void *context,
180 dispatch_function_t work);
181
182 /*!
183 * @function dispatch_sync
184 *
185 * @abstract
186 * Submits a block for synchronous execution on a dispatch queue.
187 *
188 * @discussion
189 * Submits a block to a dispatch queue like dispatch_async(), however
190 * dispatch_sync() will not return until the block has finished.
191 *
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.
196 *
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.
200 *
201 * As an optimization, dispatch_sync() invokes the block on the current
202 * thread when possible.
203 *
204 * @param queue
205 * The target dispatch queue to which the block is submitted.
206 * The result of passing NULL in this parameter is undefined.
207 *
208 * @param block
209 * The block to be invoked on the target dispatch queue.
210 * The result of passing NULL in this parameter is undefined.
211 */
212 #ifdef __BLOCKS__
213 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
214 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
215 void
216 dispatch_sync(dispatch_queue_t queue, dispatch_block_t block);
217 #endif
218
219 /*!
220 * @function dispatch_sync_f
221 *
222 * @abstract
223 * Submits a function for synchronous execution on a dispatch queue.
224 *
225 * @discussion
226 * See dispatch_sync() for details.
227 *
228 * @param queue
229 * The target dispatch queue to which the function is submitted.
230 * The result of passing NULL in this parameter is undefined.
231 *
232 * @param context
233 * The application-defined context parameter to pass to the function.
234 *
235 * @param work
236 * The application-defined function to invoke on the target queue. The first
237 * parameter passed to this function is the context provided to
238 * dispatch_sync_f().
239 * The result of passing NULL in this parameter is undefined.
240 */
241 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
242 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
243 void
244 dispatch_sync_f(dispatch_queue_t queue,
245 void *context,
246 dispatch_function_t work);
247
248 /*!
249 * @function dispatch_apply
250 *
251 * @abstract
252 * Submits a block to a dispatch queue for multiple invocations.
253 *
254 * @discussion
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 concurrent, the block may be invoked concurrently, and it must therefore
258 * be reentrant safe.
259 *
260 * Each invocation of the block will be passed the current index of iteration.
261 *
262 * @param iterations
263 * The number of iterations to perform.
264 *
265 * @param queue
266 * The target dispatch queue to which the block is submitted.
267 * The result of passing NULL in this parameter is undefined.
268 *
269 * @param block
270 * The block to be invoked the specified number of iterations.
271 * The result of passing NULL in this parameter is undefined.
272 */
273 #ifdef __BLOCKS__
274 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
275 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
276 void
277 dispatch_apply(size_t iterations, dispatch_queue_t queue,
278 void (^block)(size_t));
279 #endif
280
281 /*!
282 * @function dispatch_apply_f
283 *
284 * @abstract
285 * Submits a function to a dispatch queue for multiple invocations.
286 *
287 * @discussion
288 * See dispatch_apply() for details.
289 *
290 * @param iterations
291 * The number of iterations to perform.
292 *
293 * @param queue
294 * The target dispatch queue to which the function is submitted.
295 * The result of passing NULL in this parameter is undefined.
296 *
297 * @param context
298 * The application-defined context parameter to pass to the function.
299 *
300 * @param work
301 * The application-defined function to invoke on the target queue. The first
302 * parameter passed to this function is the context provided to
303 * dispatch_apply_f(). The second parameter passed to this function is the
304 * current index of iteration.
305 * The result of passing NULL in this parameter is undefined.
306 */
307 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
308 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NONNULL4 DISPATCH_NOTHROW
309 void
310 dispatch_apply_f(size_t iterations, dispatch_queue_t queue,
311 void *context,
312 void (*work)(void *, size_t));
313
314 /*!
315 * @function dispatch_get_current_queue
316 *
317 * @abstract
318 * Returns the queue on which the currently executing block is running.
319 *
320 * @discussion
321 * Returns the queue on which the currently executing block is running.
322 *
323 * When dispatch_get_current_queue() is called outside of the context of a
324 * submitted block, it will return the default concurrent queue.
325 *
326 * Recommended for debugging and logging purposes only:
327 * The code must not make any assumptions about the queue returned, unless it
328 * is one of the global queues or a queue the code has itself created.
329 * The code must not assume that synchronous execution onto a queue is safe
330 * from deadlock if that queue is not the one returned by
331 * dispatch_get_current_queue().
332 *
333 * When dispatch_get_current_queue() is called on the main thread, it may
334 * or may not return the same value as dispatch_get_main_queue(). Comparing
335 * the two is not a valid way to test whether code is executing on the
336 * main thread.
337 *
338 * @result
339 * Returns the current queue.
340 */
341 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
342 DISPATCH_EXPORT DISPATCH_PURE DISPATCH_WARN_RESULT DISPATCH_NOTHROW
343 dispatch_queue_t
344 dispatch_get_current_queue(void);
345
346 /*!
347 * @function dispatch_get_main_queue
348 *
349 * @abstract
350 * Returns the default queue that is bound to the main thread.
351 *
352 * @discussion
353 * In order to invoke blocks submitted to the main queue, the application must
354 * call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main
355 * thread.
356 *
357 * @result
358 * Returns the main queue. This queue is created automatically on behalf of
359 * the main thread before main() is called.
360 */
361 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
362 DISPATCH_EXPORT struct dispatch_queue_s _dispatch_main_q;
363 #define dispatch_get_main_queue() \
364 DISPATCH_GLOBAL_OBJECT(dispatch_queue_t, _dispatch_main_q)
365
366 /*!
367 * @typedef dispatch_queue_priority_t
368 * Type of dispatch_queue_priority
369 *
370 * @constant DISPATCH_QUEUE_PRIORITY_HIGH
371 * Items dispatched to the queue will run at high priority,
372 * i.e. the queue will be scheduled for execution before
373 * any default priority or low priority queue.
374 *
375 * @constant DISPATCH_QUEUE_PRIORITY_DEFAULT
376 * Items dispatched to the queue will run at the default
377 * priority, i.e. the queue will be scheduled for execution
378 * after all high priority queues have been scheduled, but
379 * before any low priority queues have been scheduled.
380 *
381 * @constant DISPATCH_QUEUE_PRIORITY_LOW
382 * Items dispatched to the queue will run at low priority,
383 * i.e. the queue will be scheduled for execution after all
384 * default priority and high priority queues have been
385 * scheduled.
386 *
387 * @constant DISPATCH_QUEUE_PRIORITY_BACKGROUND
388 * Items dispatched to the queue will run at background priority, i.e. the queue
389 * will be scheduled for execution after all higher priority queues have been
390 * scheduled and the system will run items on this queue on a thread with
391 * background status as per setpriority(2) (i.e. disk I/O is throttled and the
392 * thread's scheduling priority is set to lowest value).
393 */
394 #define DISPATCH_QUEUE_PRIORITY_HIGH 2
395 #define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
396 #define DISPATCH_QUEUE_PRIORITY_LOW (-2)
397 #define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
398
399 typedef long dispatch_queue_priority_t;
400
401 /*!
402 * @function dispatch_get_global_queue
403 *
404 * @abstract
405 * Returns a well-known global concurrent queue of a given priority level.
406 *
407 * @discussion
408 * The well-known global concurrent queues may not be modified. Calls to
409 * dispatch_suspend(), dispatch_resume(), dispatch_set_context(), etc., will
410 * have no effect when used with queues returned by this function.
411 *
412 * @param priority
413 * A priority defined in dispatch_queue_priority_t
414 *
415 * @param flags
416 * Reserved for future use. Passing any value other than zero may result in
417 * a NULL return value.
418 *
419 * @result
420 * Returns the requested global queue.
421 */
422 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
423 DISPATCH_EXPORT DISPATCH_CONST DISPATCH_WARN_RESULT DISPATCH_NOTHROW
424 dispatch_queue_t
425 dispatch_get_global_queue(dispatch_queue_priority_t priority,
426 unsigned long flags);
427
428 /*!
429 * @const DISPATCH_QUEUE_SERIAL
430 * @discussion A dispatch queue that invokes blocks serially in FIFO order.
431 */
432 #define DISPATCH_QUEUE_SERIAL NULL
433
434 /*!
435 * @const DISPATCH_QUEUE_CONCURRENT
436 * @discussion A dispatch queue that may invoke blocks concurrently and supports
437 * barrier blocks submitted with the dispatch barrier API.
438 */
439 #define DISPATCH_QUEUE_CONCURRENT \
440 DISPATCH_GLOBAL_OBJECT(dispatch_queue_attr_t, \
441 _dispatch_queue_attr_concurrent)
442 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
443 DISPATCH_EXPORT
444 struct dispatch_queue_attr_s _dispatch_queue_attr_concurrent;
445
446 /*!
447 * @function dispatch_queue_create
448 *
449 * @abstract
450 * Creates a new dispatch queue to which blocks may be submitted.
451 *
452 * @discussion
453 * Dispatch queues created with the DISPATCH_QUEUE_SERIAL or a NULL attribute
454 * invoke blocks serially in FIFO order.
455 *
456 * Dispatch queues created with the DISPATCH_QUEUE_CONCURRENT attribute may
457 * invoke blocks concurrently (similarly to the global concurrent queues, but
458 * potentially with more overhead), and support barrier blocks submitted with
459 * the dispatch barrier API, which e.g. enables the implementation of efficient
460 * reader-writer schemes.
461 *
462 * When a dispatch queue is no longer needed, it should be released with
463 * dispatch_release(). Note that any pending blocks submitted to a queue will
464 * hold a reference to that queue. Therefore a queue will not be deallocated
465 * until all pending blocks have finished.
466 *
467 * The target queue of a newly created dispatch queue is the default priority
468 * global concurrent queue.
469 *
470 * @param label
471 * A string label to attach to the queue.
472 * This parameter is optional and may be NULL.
473 *
474 * @param attr
475 * DISPATCH_QUEUE_SERIAL or DISPATCH_QUEUE_CONCURRENT.
476 *
477 * @result
478 * The newly created dispatch queue.
479 */
480 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
481 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
482 DISPATCH_NOTHROW
483 dispatch_queue_t
484 dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
485
486 /*!
487 * @function dispatch_queue_get_label
488 *
489 * @abstract
490 * Returns the label of the queue that was specified when the
491 * queue was created.
492 *
493 * @param queue
494 * The result of passing NULL in this parameter is undefined.
495 *
496 * @result
497 * The label of the queue. The result may be NULL.
498 */
499 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
500 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_PURE DISPATCH_WARN_RESULT
501 DISPATCH_NOTHROW
502 const char *
503 dispatch_queue_get_label(dispatch_queue_t queue);
504
505 /*!
506 * @const DISPATCH_TARGET_QUEUE_DEFAULT
507 * @discussion Constant to pass to the dispatch_set_target_queue() and
508 * dispatch_source_create() functions to indicate that the default target queue
509 * for the given object type should be used.
510 */
511 #define DISPATCH_TARGET_QUEUE_DEFAULT NULL
512
513 /*!
514 * @function dispatch_set_target_queue
515 *
516 * @abstract
517 * Sets the target queue for the given object.
518 *
519 * @discussion
520 * An object's target queue is responsible for processing the object.
521 *
522 * A dispatch queue's priority is inherited from its target queue. Use the
523 * dispatch_get_global_queue() function to obtain suitable target queue
524 * of the desired priority.
525 *
526 * Blocks submitted to a serial queue whose target queue is another serial
527 * queue will not be invoked concurrently with blocks submitted to the target
528 * queue or to any other queue with that same target queue.
529 *
530 * The result of introducing a cycle into the hierarchy of target queues is
531 * undefined.
532 *
533 * A dispatch source's target queue specifies where its event handler and
534 * cancellation handler blocks will be submitted.
535 *
536 * A dispatch I/O channel's target queue specifies where where its I/O
537 * operations are executed. If the channel's target queue's priority is set to
538 * DISPATCH_QUEUE_PRIORITY_BACKGROUND, then the I/O operations performed by
539 * dispatch_io_read() or dispatch_io_write() on that queue will be
540 * throttled when there is I/O contention.
541 *
542 * For all other dispatch object types, the only function of the target queue
543 * is to determine where an object's finalizer function is invoked.
544 *
545 * @param object
546 * The object to modify.
547 * The result of passing NULL in this parameter is undefined.
548 *
549 * @param queue
550 * The new target queue for the object. The queue is retained, and the
551 * previous target queue, if any, is released.
552 * If queue is DISPATCH_TARGET_QUEUE_DEFAULT, set the object's target queue
553 * to the default target queue for the given object type.
554 */
555 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
556 DISPATCH_EXPORT DISPATCH_NOTHROW // DISPATCH_NONNULL1
557 void
558 dispatch_set_target_queue(dispatch_object_t object, dispatch_queue_t queue);
559
560 /*!
561 * @function dispatch_main
562 *
563 * @abstract
564 * Execute blocks submitted to the main queue.
565 *
566 * @discussion
567 * This function "parks" the main thread and waits for blocks to be submitted
568 * to the main queue. This function never returns.
569 *
570 * Applications that call NSApplicationMain() or CFRunLoopRun() on the
571 * main thread do not need to call dispatch_main().
572 */
573 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
574 DISPATCH_EXPORT DISPATCH_NOTHROW DISPATCH_NORETURN
575 void
576 dispatch_main(void);
577
578 /*!
579 * @function dispatch_after
580 *
581 * @abstract
582 * Schedule a block for execution on a given queue at a specified time.
583 *
584 * @discussion
585 * Passing DISPATCH_TIME_NOW as the "when" parameter is supported, but not as
586 * optimal as calling dispatch_async() instead. Passing DISPATCH_TIME_FOREVER
587 * is undefined.
588 *
589 * @param when
590 * A temporal milestone returned by dispatch_time() or dispatch_walltime().
591 *
592 * @param queue
593 * A queue to which the given block will be submitted at the specified time.
594 * The result of passing NULL in this parameter is undefined.
595 *
596 * @param block
597 * The block of code to execute.
598 * The result of passing NULL in this parameter is undefined.
599 */
600 #ifdef __BLOCKS__
601 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
602 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NONNULL3 DISPATCH_NOTHROW
603 void
604 dispatch_after(dispatch_time_t when,
605 dispatch_queue_t queue,
606 dispatch_block_t block);
607 #endif
608
609 /*!
610 * @function dispatch_after_f
611 *
612 * @abstract
613 * Schedule a function for execution on a given queue at a specified time.
614 *
615 * @discussion
616 * See dispatch_after() for details.
617 *
618 * @param when
619 * A temporal milestone returned by dispatch_time() or dispatch_walltime().
620 *
621 * @param queue
622 * A queue to which the given function will be submitted at the specified time.
623 * The result of passing NULL in this parameter is undefined.
624 *
625 * @param context
626 * The application-defined context parameter to pass to the function.
627 *
628 * @param work
629 * The application-defined function to invoke on the target queue. The first
630 * parameter passed to this function is the context provided to
631 * dispatch_after_f().
632 * The result of passing NULL in this parameter is undefined.
633 */
634 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
635 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NONNULL4 DISPATCH_NOTHROW
636 void
637 dispatch_after_f(dispatch_time_t when,
638 dispatch_queue_t queue,
639 void *context,
640 dispatch_function_t work);
641
642 /*!
643 * @functiongroup Dispatch Barrier API
644 * The dispatch barrier API is a mechanism for submitting barrier blocks to a
645 * dispatch queue, analogous to the dispatch_async()/dispatch_sync() API.
646 * It enables the implementation of efficient reader/writer schemes.
647 * Barrier blocks only behave specially when submitted to queues created with
648 * the DISPATCH_QUEUE_CONCURRENT attribute; on such a queue, a barrier block
649 * will not run until all blocks submitted to the queue earlier have completed,
650 * and any blocks submitted to the queue after a barrier block will not run
651 * until the barrier block has completed.
652 * When submitted to a a global queue or to a queue not created with the
653 * DISPATCH_QUEUE_CONCURRENT attribute, barrier blocks behave identically to
654 * blocks submitted with the dispatch_async()/dispatch_sync() API.
655 */
656
657 /*!
658 * @function dispatch_barrier_async
659 *
660 * @abstract
661 * Submits a barrier block for asynchronous execution on a dispatch queue.
662 *
663 * @discussion
664 * Submits a block to a dispatch queue like dispatch_async(), but marks that
665 * block as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
666 *
667 * See dispatch_async() for details.
668 *
669 * @param queue
670 * The target dispatch queue to which the block is submitted.
671 * The system will hold a reference on the target queue until the block
672 * has finished.
673 * The result of passing NULL in this parameter is undefined.
674 *
675 * @param block
676 * The block to submit to the target dispatch queue. This function performs
677 * Block_copy() and Block_release() on behalf of callers.
678 * The result of passing NULL in this parameter is undefined.
679 */
680 #ifdef __BLOCKS__
681 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
682 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
683 void
684 dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);
685 #endif
686
687 /*!
688 * @function dispatch_barrier_async_f
689 *
690 * @abstract
691 * Submits a barrier function for asynchronous execution on a dispatch queue.
692 *
693 * @discussion
694 * Submits a function to a dispatch queue like dispatch_async_f(), but marks
695 * that function as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT
696 * queues).
697 *
698 * See dispatch_async_f() for details.
699 *
700 * @param queue
701 * The target dispatch queue to which the function is submitted.
702 * The system will hold a reference on the target queue until the function
703 * has returned.
704 * The result of passing NULL in this parameter is undefined.
705 *
706 * @param context
707 * The application-defined context parameter to pass to the function.
708 *
709 * @param work
710 * The application-defined function to invoke on the target queue. The first
711 * parameter passed to this function is the context provided to
712 * dispatch_barrier_async_f().
713 * The result of passing NULL in this parameter is undefined.
714 */
715 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
716 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
717 void
718 dispatch_barrier_async_f(dispatch_queue_t queue,
719 void *context,
720 dispatch_function_t work);
721
722 /*!
723 * @function dispatch_barrier_sync
724 *
725 * @abstract
726 * Submits a barrier block for synchronous execution on a dispatch queue.
727 *
728 * @discussion
729 * Submits a block to a dispatch queue like dispatch_sync(), but marks that
730 * block as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
731 *
732 * See dispatch_sync() for details.
733 *
734 * @param queue
735 * The target dispatch queue to which the block is submitted.
736 * The result of passing NULL in this parameter is undefined.
737 *
738 * @param block
739 * The block to be invoked on the target dispatch queue.
740 * The result of passing NULL in this parameter is undefined.
741 */
742 #ifdef __BLOCKS__
743 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
744 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
745 void
746 dispatch_barrier_sync(dispatch_queue_t queue, dispatch_block_t block);
747 #endif
748
749 /*!
750 * @function dispatch_barrier_sync_f
751 *
752 * @abstract
753 * Submits a barrier function for synchronous execution on a dispatch queue.
754 *
755 * @discussion
756 * Submits a function to a dispatch queue like dispatch_sync_f(), but marks that
757 * fuction as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
758 *
759 * See dispatch_sync_f() for details.
760 *
761 * @param queue
762 * The target dispatch queue to which the function is submitted.
763 * The result of passing NULL in this parameter is undefined.
764 *
765 * @param context
766 * The application-defined context parameter to pass to the function.
767 *
768 * @param work
769 * The application-defined function to invoke on the target queue. The first
770 * parameter passed to this function is the context provided to
771 * dispatch_barrier_sync_f().
772 * The result of passing NULL in this parameter is undefined.
773 */
774 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
775 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
776 void
777 dispatch_barrier_sync_f(dispatch_queue_t queue,
778 void *context,
779 dispatch_function_t work);
780
781 /*!
782 * @functiongroup Dispatch queue-specific contexts
783 * This API allows different subsystems to associate context to a shared queue
784 * without risk of collision and to retrieve that context from blocks executing
785 * on that queue or any of its child queues in the target queue hierarchy.
786 */
787
788 /*!
789 * @function dispatch_queue_set_specific
790 *
791 * @abstract
792 * Associates a subsystem-specific context with a dispatch queue, for a key
793 * unique to the subsystem.
794 *
795 * @discussion
796 * The specified destructor will be invoked with the context on the default
797 * priority global concurrent queue when a new context is set for the same key,
798 * or after all references to the queue have been released.
799 *
800 * @param queue
801 * The dispatch queue to modify.
802 * The result of passing NULL in this parameter is undefined.
803 *
804 * @param key
805 * The key to set the context for, typically a pointer to a static variable
806 * specific to the subsystem. Keys are only compared as pointers and never
807 * dereferenced. Passing a string constant directly is not recommended.
808 * The NULL key is reserved and attemps to set a context for it are ignored.
809 *
810 * @param context
811 * The new subsystem-specific context for the object. This may be NULL.
812 *
813 * @param destructor
814 * The destructor function pointer. This may be NULL and is ignored if context
815 * is NULL.
816 */
817 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
818 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NOTHROW
819 void
820 dispatch_queue_set_specific(dispatch_queue_t queue, const void *key,
821 void *context, dispatch_function_t destructor);
822
823 /*!
824 * @function dispatch_queue_get_specific
825 *
826 * @abstract
827 * Returns the subsystem-specific context associated with a dispatch queue, for
828 * a key unique to the subsystem.
829 *
830 * @discussion
831 * Returns the context for the specified key if it has been set on the specified
832 * queue.
833 *
834 * @param queue
835 * The dispatch queue to query.
836 * The result of passing NULL in this parameter is undefined.
837 *
838 * @param key
839 * The key to get the context for, typically a pointer to a static variable
840 * specific to the subsystem. Keys are only compared as pointers and never
841 * dereferenced. Passing a string constant directly is not recommended.
842 *
843 * @result
844 * The context for the specified key or NULL if no context was found.
845 */
846 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
847 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_PURE DISPATCH_WARN_RESULT
848 DISPATCH_NOTHROW
849 void *
850 dispatch_queue_get_specific(dispatch_queue_t queue, const void *key);
851
852 /*!
853 * @function dispatch_get_specific
854 *
855 * @abstract
856 * Returns the current subsystem-specific context for a key unique to the
857 * subsystem.
858 *
859 * @discussion
860 * When called from a block executing on a queue, returns the context for the
861 * specified key if it has been set on the queue, otherwise returns the result
862 * of dispatch_get_specific() executed on the queue's target queue or NULL
863 * if the current queue is a global concurrent queue.
864 *
865 * @param key
866 * The key to get the context for, typically a pointer to a static variable
867 * specific to the subsystem. Keys are only compared as pointers and never
868 * dereferenced. Passing a string constant directly is not recommended.
869 *
870 * @result
871 * The context for the specified key or NULL if no context was found.
872 */
873 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
874 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_PURE DISPATCH_WARN_RESULT
875 DISPATCH_NOTHROW
876 void *
877 dispatch_get_specific(const void *key);
878
879 __END_DECLS
880
881 #endif