]> git.saurik.com Git - apple/libdispatch.git/blob - dispatch/queue.h
libdispatch-187.5.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 * @result
334 * Returns the current queue.
335 */
336 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
337 DISPATCH_EXPORT DISPATCH_PURE DISPATCH_WARN_RESULT DISPATCH_NOTHROW
338 dispatch_queue_t
339 dispatch_get_current_queue(void);
340
341 /*!
342 * @function dispatch_get_main_queue
343 *
344 * @abstract
345 * Returns the default queue that is bound to the main thread.
346 *
347 * @discussion
348 * In order to invoke blocks submitted to the main queue, the application must
349 * call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main
350 * thread.
351 *
352 * @result
353 * Returns the main queue. This queue is created automatically on behalf of
354 * the main thread before main() is called.
355 */
356 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
357 DISPATCH_EXPORT struct dispatch_queue_s _dispatch_main_q;
358 #define dispatch_get_main_queue() (&_dispatch_main_q)
359
360 /*!
361 * @typedef dispatch_queue_priority_t
362 * Type of dispatch_queue_priority
363 *
364 * @constant DISPATCH_QUEUE_PRIORITY_HIGH
365 * Items dispatched to the queue will run at high priority,
366 * i.e. the queue will be scheduled for execution before
367 * any default priority or low priority queue.
368 *
369 * @constant DISPATCH_QUEUE_PRIORITY_DEFAULT
370 * Items dispatched to the queue will run at the default
371 * priority, i.e. the queue will be scheduled for execution
372 * after all high priority queues have been scheduled, but
373 * before any low priority queues have been scheduled.
374 *
375 * @constant DISPATCH_QUEUE_PRIORITY_LOW
376 * Items dispatched to the queue will run at low priority,
377 * i.e. the queue will be scheduled for execution after all
378 * default priority and high priority queues have been
379 * scheduled.
380 *
381 * @constant DISPATCH_QUEUE_PRIORITY_BACKGROUND
382 * Items dispatched to the queue will run at background priority, i.e. the queue
383 * will be scheduled for execution after all higher priority queues have been
384 * scheduled and the system will run items on this queue on a thread with
385 * background status as per setpriority(2) (i.e. disk I/O is throttled and the
386 * thread's scheduling priority is set to lowest value).
387 */
388 #define DISPATCH_QUEUE_PRIORITY_HIGH 2
389 #define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
390 #define DISPATCH_QUEUE_PRIORITY_LOW (-2)
391 #define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
392
393 typedef long dispatch_queue_priority_t;
394
395 /*!
396 * @function dispatch_get_global_queue
397 *
398 * @abstract
399 * Returns a well-known global concurrent queue of a given priority level.
400 *
401 * @discussion
402 * The well-known global concurrent queues may not be modified. Calls to
403 * dispatch_suspend(), dispatch_resume(), dispatch_set_context(), etc., will
404 * have no effect when used with queues returned by this function.
405 *
406 * @param priority
407 * A priority defined in dispatch_queue_priority_t
408 *
409 * @param flags
410 * Reserved for future use. Passing any value other than zero may result in
411 * a NULL return value.
412 *
413 * @result
414 * Returns the requested global queue.
415 */
416 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
417 DISPATCH_EXPORT DISPATCH_CONST DISPATCH_WARN_RESULT DISPATCH_NOTHROW
418 dispatch_queue_t
419 dispatch_get_global_queue(dispatch_queue_priority_t priority,
420 unsigned long flags);
421
422 /*!
423 * @const DISPATCH_QUEUE_SERIAL
424 * @discussion A dispatch queue that invokes blocks serially in FIFO order.
425 */
426 #define DISPATCH_QUEUE_SERIAL NULL
427
428 /*!
429 * @const DISPATCH_QUEUE_CONCURRENT
430 * @discussion A dispatch queue that may invoke blocks concurrently and supports
431 * barrier blocks submitted with the dispatch barrier API.
432 */
433 #define DISPATCH_QUEUE_CONCURRENT (&_dispatch_queue_attr_concurrent)
434 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
435 DISPATCH_EXPORT
436 struct dispatch_queue_attr_s _dispatch_queue_attr_concurrent;
437
438 /*!
439 * @function dispatch_queue_create
440 *
441 * @abstract
442 * Creates a new dispatch queue to which blocks may be submitted.
443 *
444 * @discussion
445 * Dispatch queues created with the DISPATCH_QUEUE_SERIAL or a NULL attribute
446 * invoke blocks serially in FIFO order.
447 *
448 * Dispatch queues created with the DISPATCH_QUEUE_CONCURRENT attribute may
449 * invoke blocks concurrently (similarly to the global concurrent queues, but
450 * potentially with more overhead), and support barrier blocks submitted with
451 * the dispatch barrier API, which e.g. enables the implementation of efficient
452 * reader-writer schemes.
453 *
454 * When a dispatch queue is no longer needed, it should be released with
455 * dispatch_release(). Note that any pending blocks submitted to a queue will
456 * hold a reference to that queue. Therefore a queue will not be deallocated
457 * until all pending blocks have finished.
458 *
459 * The target queue of a newly created dispatch queue is the default priority
460 * global concurrent queue.
461 *
462 * @param label
463 * A string label to attach to the queue.
464 * This parameter is optional and may be NULL.
465 *
466 * @param attr
467 * DISPATCH_QUEUE_SERIAL or DISPATCH_QUEUE_CONCURRENT.
468 *
469 * @result
470 * The newly created dispatch queue.
471 */
472 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
473 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_WARN_RESULT DISPATCH_NOTHROW
474 dispatch_queue_t
475 dispatch_queue_create(const char *label, dispatch_queue_attr_t attr);
476
477 /*!
478 * @function dispatch_queue_get_label
479 *
480 * @abstract
481 * Returns the label of the queue that was specified when the
482 * queue was created.
483 *
484 * @param queue
485 * The result of passing NULL in this parameter is undefined.
486 *
487 * @result
488 * The label of the queue. The result may be NULL.
489 */
490 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
491 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_PURE DISPATCH_WARN_RESULT
492 DISPATCH_NOTHROW
493 const char *
494 dispatch_queue_get_label(dispatch_queue_t queue);
495
496 /*!
497 * @const DISPATCH_TARGET_QUEUE_DEFAULT
498 * @discussion Constant to pass to the dispatch_set_target_queue() and
499 * dispatch_source_create() functions to indicate that the default target queue
500 * for the given object type should be used.
501 */
502 #define DISPATCH_TARGET_QUEUE_DEFAULT NULL
503
504 /*!
505 * @function dispatch_set_target_queue
506 *
507 * @abstract
508 * Sets the target queue for the given object.
509 *
510 * @discussion
511 * An object's target queue is responsible for processing the object.
512 *
513 * A dispatch queue's priority is inherited from its target queue. Use the
514 * dispatch_get_global_queue() function to obtain suitable target queue
515 * of the desired priority.
516 *
517 * Blocks submitted to a serial queue whose target queue is another serial
518 * queue will not be invoked concurrently with blocks submitted to the target
519 * queue or to any other queue with that same target queue.
520 *
521 * The result of introducing a cycle into the hierarchy of target queues is
522 * undefined.
523 *
524 * A dispatch source's target queue specifies where its event handler and
525 * cancellation handler blocks will be submitted.
526 *
527 * A dispatch I/O channel's target queue specifies where where its I/O
528 * operations are executed.
529 *
530 * For all other dispatch object types, the only function of the target queue
531 * is to determine where an object's finalizer function is invoked.
532 *
533 * @param object
534 * The object to modify.
535 * The result of passing NULL in this parameter is undefined.
536 *
537 * @param queue
538 * The new target queue for the object. The queue is retained, and the
539 * previous target queue, if any, is released.
540 * If queue is DISPATCH_TARGET_QUEUE_DEFAULT, set the object's target queue
541 * to the default target queue for the given object type.
542 */
543 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
544 DISPATCH_EXPORT DISPATCH_NOTHROW // DISPATCH_NONNULL1
545 void
546 dispatch_set_target_queue(dispatch_object_t object, dispatch_queue_t queue);
547
548 /*!
549 * @function dispatch_main
550 *
551 * @abstract
552 * Execute blocks submitted to the main queue.
553 *
554 * @discussion
555 * This function "parks" the main thread and waits for blocks to be submitted
556 * to the main queue. This function never returns.
557 *
558 * Applications that call NSApplicationMain() or CFRunLoopRun() on the
559 * main thread do not need to call dispatch_main().
560 */
561 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
562 DISPATCH_EXPORT DISPATCH_NOTHROW DISPATCH_NORETURN
563 void
564 dispatch_main(void);
565
566 /*!
567 * @function dispatch_after
568 *
569 * @abstract
570 * Schedule a block for execution on a given queue at a specified time.
571 *
572 * @discussion
573 * Passing DISPATCH_TIME_NOW as the "when" parameter is supported, but not as
574 * optimal as calling dispatch_async() instead. Passing DISPATCH_TIME_FOREVER
575 * is undefined.
576 *
577 * @param when
578 * A temporal milestone returned by dispatch_time() or dispatch_walltime().
579 *
580 * @param queue
581 * A queue to which the given block will be submitted at the specified time.
582 * The result of passing NULL in this parameter is undefined.
583 *
584 * @param block
585 * The block of code to execute.
586 * The result of passing NULL in this parameter is undefined.
587 */
588 #ifdef __BLOCKS__
589 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
590 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NONNULL3 DISPATCH_NOTHROW
591 void
592 dispatch_after(dispatch_time_t when,
593 dispatch_queue_t queue,
594 dispatch_block_t block);
595 #endif
596
597 /*!
598 * @function dispatch_after_f
599 *
600 * @abstract
601 * Schedule a function for execution on a given queue at a specified time.
602 *
603 * @discussion
604 * See dispatch_after() for details.
605 *
606 * @param when
607 * A temporal milestone returned by dispatch_time() or dispatch_walltime().
608 *
609 * @param queue
610 * A queue to which the given function will be submitted at the specified time.
611 * The result of passing NULL in this parameter is undefined.
612 *
613 * @param context
614 * The application-defined context parameter to pass to the function.
615 *
616 * @param work
617 * The application-defined function to invoke on the target queue. The first
618 * parameter passed to this function is the context provided to
619 * dispatch_after_f().
620 * The result of passing NULL in this parameter is undefined.
621 */
622 __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0)
623 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NONNULL4 DISPATCH_NOTHROW
624 void
625 dispatch_after_f(dispatch_time_t when,
626 dispatch_queue_t queue,
627 void *context,
628 dispatch_function_t work);
629
630 /*!
631 * @functiongroup Dispatch Barrier API
632 * The dispatch barrier API is a mechanism for submitting barrier blocks to a
633 * dispatch queue, analogous to the dispatch_async()/dispatch_sync() API.
634 * It enables the implementation of efficient reader/writer schemes.
635 * Barrier blocks only behave specially when submitted to queues created with
636 * the DISPATCH_QUEUE_CONCURRENT attribute; on such a queue, a barrier block
637 * will not run until all blocks submitted to the queue earlier have completed,
638 * and any blocks submitted to the queue after a barrier block will not run
639 * until the barrier block has completed.
640 * When submitted to a a global queue or to a queue not created with the
641 * DISPATCH_QUEUE_CONCURRENT attribute, barrier blocks behave identically to
642 * blocks submitted with the dispatch_async()/dispatch_sync() API.
643 */
644
645 /*!
646 * @function dispatch_barrier_async
647 *
648 * @abstract
649 * Submits a barrier block for asynchronous execution on a dispatch queue.
650 *
651 * @discussion
652 * Submits a block to a dispatch queue like dispatch_async(), but marks that
653 * block as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
654 *
655 * See dispatch_async() for details.
656 *
657 * @param queue
658 * The target dispatch queue to which the block is submitted.
659 * The system will hold a reference on the target queue until the block
660 * has finished.
661 * The result of passing NULL in this parameter is undefined.
662 *
663 * @param block
664 * The block to submit to the target dispatch queue. This function performs
665 * Block_copy() and Block_release() on behalf of callers.
666 * The result of passing NULL in this parameter is undefined.
667 */
668 #ifdef __BLOCKS__
669 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
670 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
671 void
672 dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);
673 #endif
674
675 /*!
676 * @function dispatch_barrier_async_f
677 *
678 * @abstract
679 * Submits a barrier function for asynchronous execution on a dispatch queue.
680 *
681 * @discussion
682 * Submits a function to a dispatch queue like dispatch_async_f(), but marks
683 * that function as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT
684 * queues).
685 *
686 * See dispatch_async_f() for details.
687 *
688 * @param queue
689 * The target dispatch queue to which the function is submitted.
690 * The system will hold a reference on the target queue until the function
691 * has returned.
692 * The result of passing NULL in this parameter is undefined.
693 *
694 * @param context
695 * The application-defined context parameter to pass to the function.
696 *
697 * @param work
698 * The application-defined function to invoke on the target queue. The first
699 * parameter passed to this function is the context provided to
700 * dispatch_barrier_async_f().
701 * The result of passing NULL in this parameter is undefined.
702 */
703 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
704 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
705 void
706 dispatch_barrier_async_f(dispatch_queue_t queue,
707 void *context,
708 dispatch_function_t work);
709
710 /*!
711 * @function dispatch_barrier_sync
712 *
713 * @abstract
714 * Submits a barrier block for synchronous execution on a dispatch queue.
715 *
716 * @discussion
717 * Submits a block to a dispatch queue like dispatch_sync(), but marks that
718 * block as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
719 *
720 * See dispatch_sync() for details.
721 *
722 * @param queue
723 * The target dispatch queue to which the block is submitted.
724 * The result of passing NULL in this parameter is undefined.
725 *
726 * @param block
727 * The block to be invoked on the target dispatch queue.
728 * The result of passing NULL in this parameter is undefined.
729 */
730 #ifdef __BLOCKS__
731 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
732 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
733 void
734 dispatch_barrier_sync(dispatch_queue_t queue, dispatch_block_t block);
735 #endif
736
737 /*!
738 * @function dispatch_barrier_sync_f
739 *
740 * @abstract
741 * Submits a barrier function for synchronous execution on a dispatch queue.
742 *
743 * @discussion
744 * Submits a function to a dispatch queue like dispatch_sync_f(), but marks that
745 * fuction as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
746 *
747 * See dispatch_sync_f() for details.
748 *
749 * @param queue
750 * The target dispatch queue to which the function is submitted.
751 * The result of passing NULL in this parameter is undefined.
752 *
753 * @param context
754 * The application-defined context parameter to pass to the function.
755 *
756 * @param work
757 * The application-defined function to invoke on the target queue. The first
758 * parameter passed to this function is the context provided to
759 * dispatch_barrier_sync_f().
760 * The result of passing NULL in this parameter is undefined.
761 */
762 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
763 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
764 void
765 dispatch_barrier_sync_f(dispatch_queue_t queue,
766 void *context,
767 dispatch_function_t work);
768
769 /*!
770 * @functiongroup Dispatch queue-specific contexts
771 * This API allows different subsystems to associate context to a shared queue
772 * without risk of collision and to retrieve that context from blocks executing
773 * on that queue or any of its child queues in the target queue hierarchy.
774 */
775
776 /*!
777 * @function dispatch_queue_set_specific
778 *
779 * @abstract
780 * Associates a subsystem-specific context with a dispatch queue, for a key
781 * unique to the subsystem.
782 *
783 * @discussion
784 * The specified destructor will be invoked with the context on the default
785 * priority global concurrent queue when a new context is set for the same key,
786 * or after all references to the queue have been released.
787 *
788 * @param queue
789 * The dispatch queue to modify.
790 * The result of passing NULL in this parameter is undefined.
791 *
792 * @param key
793 * The key to set the context for, typically a pointer to a static variable
794 * specific to the subsystem. Keys are only compared as pointers and never
795 * dereferenced. Passing a string constant directly is not recommended.
796 * The NULL key is reserved and attemps to set a context for it are ignored.
797 *
798 * @param context
799 * The new subsystem-specific context for the object. This may be NULL.
800 *
801 * @param destructor
802 * The destructor function pointer. This may be NULL and is ignored if context
803 * is NULL.
804 */
805 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
806 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NOTHROW
807 void
808 dispatch_queue_set_specific(dispatch_queue_t queue, const void *key,
809 void *context, dispatch_function_t destructor);
810
811 /*!
812 * @function dispatch_queue_get_specific
813 *
814 * @abstract
815 * Returns the subsystem-specific context associated with a dispatch queue, for
816 * a key unique to the subsystem.
817 *
818 * @discussion
819 * Returns the context for the specified key if it has been set on the specified
820 * queue.
821 *
822 * @param queue
823 * The dispatch queue to query.
824 * The result of passing NULL in this parameter is undefined.
825 *
826 * @param key
827 * The key to get the context for, typically a pointer to a static variable
828 * specific to the subsystem. Keys are only compared as pointers and never
829 * dereferenced. Passing a string constant directly is not recommended.
830 *
831 * @result
832 * The context for the specified key or NULL if no context was found.
833 */
834 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
835 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_PURE DISPATCH_WARN_RESULT
836 DISPATCH_NOTHROW
837 void *
838 dispatch_queue_get_specific(dispatch_queue_t queue, const void *key);
839
840 /*!
841 * @function dispatch_get_specific
842 *
843 * @abstract
844 * Returns the current subsystem-specific context for a key unique to the
845 * subsystem.
846 *
847 * @discussion
848 * When called from a block executing on a queue, returns the context for the
849 * specified key if it has been set on the queue, otherwise returns the result
850 * of dispatch_get_specific() executed on the queue's target queue or NULL
851 * if the current queue is a global concurrent queue.
852 *
853 * @param key
854 * The key to get the context for, typically a pointer to a static variable
855 * specific to the subsystem. Keys are only compared as pointers and never
856 * dereferenced. Passing a string constant directly is not recommended.
857 *
858 * @result
859 * The context for the specified key or NULL if no context was found.
860 */
861 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
862 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_PURE DISPATCH_WARN_RESULT
863 DISPATCH_NOTHROW
864 void *
865 dispatch_get_specific(const void *key);
866
867 __END_DECLS
868
869 #endif