]> git.saurik.com Git - apple/libdispatch.git/blob - dispatch/block.h
libdispatch-913.1.6.tar.gz
[apple/libdispatch.git] / dispatch / block.h
1 /*
2 * Copyright (c) 2014 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_BLOCK__
22 #define __DISPATCH_BLOCK__
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 #ifdef __BLOCKS__
30
31 /*!
32 * @group Dispatch block objects
33 */
34
35 DISPATCH_ASSUME_NONNULL_BEGIN
36
37 __BEGIN_DECLS
38
39 /*!
40 * @typedef dispatch_block_flags_t
41 * Flags to pass to the dispatch_block_create* functions.
42 *
43 * @const DISPATCH_BLOCK_BARRIER
44 * Flag indicating that a dispatch block object should act as a barrier block
45 * when submitted to a DISPATCH_QUEUE_CONCURRENT queue.
46 * See dispatch_barrier_async() for details.
47 * This flag has no effect when the dispatch block object is invoked directly.
48 *
49 * @const DISPATCH_BLOCK_DETACHED
50 * Flag indicating that a dispatch block object should execute disassociated
51 * from current execution context attributes such as QOS class, os_activity_t
52 * and properties of the current IPC request (if any). If invoked directly, the
53 * block object will remove these attributes from the calling thread for the
54 * duration of the block body (before applying attributes assigned to the block
55 * object, if any). If submitted to a queue, the block object will be executed
56 * with the attributes of the queue (or any attributes specifically assigned to
57 * the block object).
58 *
59 * @const DISPATCH_BLOCK_ASSIGN_CURRENT
60 * Flag indicating that a dispatch block object should be assigned the execution
61 * context attributes that are current at the time the block object is created.
62 * This applies to attributes such as QOS class, os_activity_t and properties of
63 * the current IPC request (if any). If invoked directly, the block object will
64 * apply these attributes to the calling thread for the duration of the block
65 * body. If the block object is submitted to a queue, this flag replaces the
66 * default behavior of associating the submitted block instance with the
67 * execution context attributes that are current at the time of submission.
68 * If a specific QOS class is assigned with DISPATCH_BLOCK_NO_QOS_CLASS or
69 * dispatch_block_create_with_qos_class(), that QOS class takes precedence over
70 * the QOS class assignment indicated by this flag.
71 *
72 * @const DISPATCH_BLOCK_NO_QOS_CLASS
73 * Flag indicating that a dispatch block object should be not be assigned a QOS
74 * class. If invoked directly, the block object will be executed with the QOS
75 * class of the calling thread. If the block object is submitted to a queue,
76 * this replaces the default behavior of associating the submitted block
77 * instance with the QOS class current at the time of submission.
78 * This flag is ignored if a specific QOS class is assigned with
79 * dispatch_block_create_with_qos_class().
80 *
81 * @const DISPATCH_BLOCK_INHERIT_QOS_CLASS
82 * Flag indicating that execution of a dispatch block object submitted to a
83 * queue should prefer the QOS class assigned to the queue over the QOS class
84 * assigned to the block (resp. associated with the block at the time of
85 * submission). The latter will only be used if the queue in question does not
86 * have an assigned QOS class, as long as doing so does not result in a QOS
87 * class lower than the QOS class inherited from the queue's target queue.
88 * This flag is the default when a dispatch block object is submitted to a queue
89 * for asynchronous execution and has no effect when the dispatch block object
90 * is invoked directly. It is ignored if DISPATCH_BLOCK_ENFORCE_QOS_CLASS is
91 * also passed.
92 *
93 * @const DISPATCH_BLOCK_ENFORCE_QOS_CLASS
94 * Flag indicating that execution of a dispatch block object submitted to a
95 * queue should prefer the QOS class assigned to the block (resp. associated
96 * with the block at the time of submission) over the QOS class assigned to the
97 * queue, as long as doing so will not result in a lower QOS class.
98 * This flag is the default when a dispatch block object is submitted to a queue
99 * for synchronous execution or when the dispatch block object is invoked
100 * directly.
101 */
102 DISPATCH_ENUM(dispatch_block_flags, unsigned long,
103 DISPATCH_BLOCK_BARRIER
104 DISPATCH_ENUM_API_AVAILABLE(macos(10.10), ios(8.0)) = 0x1,
105 DISPATCH_BLOCK_DETACHED
106 DISPATCH_ENUM_API_AVAILABLE(macos(10.10), ios(8.0)) = 0x2,
107 DISPATCH_BLOCK_ASSIGN_CURRENT
108 DISPATCH_ENUM_API_AVAILABLE(macos(10.10), ios(8.0)) = 0x4,
109 DISPATCH_BLOCK_NO_QOS_CLASS
110 DISPATCH_ENUM_API_AVAILABLE(macos(10.10), ios(8.0)) = 0x8,
111 DISPATCH_BLOCK_INHERIT_QOS_CLASS
112 DISPATCH_ENUM_API_AVAILABLE(macos(10.10), ios(8.0)) = 0x10,
113 DISPATCH_BLOCK_ENFORCE_QOS_CLASS
114 DISPATCH_ENUM_API_AVAILABLE(macos(10.10), ios(8.0)) = 0x20,
115 );
116
117 /*!
118 * @function dispatch_block_create
119 *
120 * @abstract
121 * Create a new dispatch block object on the heap from an existing block and
122 * the given flags.
123 *
124 * @discussion
125 * The provided block is Block_copy'ed to the heap and retained by the newly
126 * created dispatch block object.
127 *
128 * The returned dispatch block object is intended to be submitted to a dispatch
129 * queue with dispatch_async() and related functions, but may also be invoked
130 * directly. Both operations can be performed an arbitrary number of times but
131 * only the first completed execution of a dispatch block object can be waited
132 * on with dispatch_block_wait() or observed with dispatch_block_notify().
133 *
134 * If the returned dispatch block object is submitted to a dispatch queue, the
135 * submitted block instance will be associated with the QOS class current at the
136 * time of submission, unless one of the following flags assigned a specific QOS
137 * class (or no QOS class) at the time of block creation:
138 * - DISPATCH_BLOCK_ASSIGN_CURRENT
139 * - DISPATCH_BLOCK_NO_QOS_CLASS
140 * - DISPATCH_BLOCK_DETACHED
141 * The QOS class the block object will be executed with also depends on the QOS
142 * class assigned to the queue and which of the following flags was specified or
143 * defaulted to:
144 * - DISPATCH_BLOCK_INHERIT_QOS_CLASS (default for asynchronous execution)
145 * - DISPATCH_BLOCK_ENFORCE_QOS_CLASS (default for synchronous execution)
146 * See description of dispatch_block_flags_t for details.
147 *
148 * If the returned dispatch block object is submitted directly to a serial queue
149 * and is configured to execute with a specific QOS class, the system will make
150 * a best effort to apply the necessary QOS overrides to ensure that blocks
151 * submitted earlier to the serial queue are executed at that same QOS class or
152 * higher.
153 *
154 * @param flags
155 * Configuration flags for the block object.
156 * Passing a value that is not a bitwise OR of flags from dispatch_block_flags_t
157 * results in NULL being returned.
158 *
159 * @param block
160 * The block to create the dispatch block object from.
161 *
162 * @result
163 * The newly created dispatch block object, or NULL.
164 * When not building with Objective-C ARC, must be released with a -[release]
165 * message or the Block_release() function.
166 */
167 API_AVAILABLE(macos(10.10), ios(8.0))
168 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_RETURNS_RETAINED_BLOCK
169 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
170 dispatch_block_t
171 dispatch_block_create(dispatch_block_flags_t flags, dispatch_block_t block);
172
173 /*!
174 * @function dispatch_block_create_with_qos_class
175 *
176 * @abstract
177 * Create a new dispatch block object on the heap from an existing block and
178 * the given flags, and assign it the specified QOS class and relative priority.
179 *
180 * @discussion
181 * The provided block is Block_copy'ed to the heap and retained by the newly
182 * created dispatch block object.
183 *
184 * The returned dispatch block object is intended to be submitted to a dispatch
185 * queue with dispatch_async() and related functions, but may also be invoked
186 * directly. Both operations can be performed an arbitrary number of times but
187 * only the first completed execution of a dispatch block object can be waited
188 * on with dispatch_block_wait() or observed with dispatch_block_notify().
189 *
190 * If invoked directly, the returned dispatch block object will be executed with
191 * the assigned QOS class as long as that does not result in a lower QOS class
192 * than what is current on the calling thread.
193 *
194 * If the returned dispatch block object is submitted to a dispatch queue, the
195 * QOS class it will be executed with depends on the QOS class assigned to the
196 * block, the QOS class assigned to the queue and which of the following flags
197 * was specified or defaulted to:
198 * - DISPATCH_BLOCK_INHERIT_QOS_CLASS: default for asynchronous execution
199 * - DISPATCH_BLOCK_ENFORCE_QOS_CLASS: default for synchronous execution
200 * See description of dispatch_block_flags_t for details.
201 *
202 * If the returned dispatch block object is submitted directly to a serial queue
203 * and is configured to execute with a specific QOS class, the system will make
204 * a best effort to apply the necessary QOS overrides to ensure that blocks
205 * submitted earlier to the serial queue are executed at that same QOS class or
206 * higher.
207 *
208 * @param flags
209 * Configuration flags for the new block object.
210 * Passing a value that is not a bitwise OR of flags from dispatch_block_flags_t
211 * results in NULL being returned.
212 *
213 * @param qos_class
214 * A QOS class value:
215 * - QOS_CLASS_USER_INTERACTIVE
216 * - QOS_CLASS_USER_INITIATED
217 * - QOS_CLASS_DEFAULT
218 * - QOS_CLASS_UTILITY
219 * - QOS_CLASS_BACKGROUND
220 * - QOS_CLASS_UNSPECIFIED
221 * Passing QOS_CLASS_UNSPECIFIED is equivalent to specifying the
222 * DISPATCH_BLOCK_NO_QOS_CLASS flag. Passing any other value results in NULL
223 * being returned.
224 *
225 * @param relative_priority
226 * A relative priority within the QOS class. This value is a negative
227 * offset from the maximum supported scheduler priority for the given class.
228 * Passing a value greater than zero or less than QOS_MIN_RELATIVE_PRIORITY
229 * results in NULL being returned.
230 *
231 * @param block
232 * The block to create the dispatch block object from.
233 *
234 * @result
235 * The newly created dispatch block object, or NULL.
236 * When not building with Objective-C ARC, must be released with a -[release]
237 * message or the Block_release() function.
238 */
239 API_AVAILABLE(macos(10.10), ios(8.0))
240 DISPATCH_EXPORT DISPATCH_NONNULL4 DISPATCH_RETURNS_RETAINED_BLOCK
241 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
242 dispatch_block_t
243 dispatch_block_create_with_qos_class(dispatch_block_flags_t flags,
244 dispatch_qos_class_t qos_class, int relative_priority,
245 dispatch_block_t block);
246
247 /*!
248 * @function dispatch_block_perform
249 *
250 * @abstract
251 * Create, synchronously execute and release a dispatch block object from the
252 * specified block and flags.
253 *
254 * @discussion
255 * Behaves identically to the sequence
256 * <code>
257 * dispatch_block_t b = dispatch_block_create(flags, block);
258 * b();
259 * Block_release(b);
260 * </code>
261 * but may be implemented more efficiently internally by not requiring a copy
262 * to the heap of the specified block or the allocation of a new block object.
263 *
264 * @param flags
265 * Configuration flags for the temporary block object.
266 * The result of passing a value that is not a bitwise OR of flags from
267 * dispatch_block_flags_t is undefined.
268 *
269 * @param block
270 * The block to create the temporary block object from.
271 */
272 API_AVAILABLE(macos(10.10), ios(8.0))
273 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NOTHROW
274 void
275 dispatch_block_perform(dispatch_block_flags_t flags,
276 DISPATCH_NOESCAPE dispatch_block_t block);
277
278 /*!
279 * @function dispatch_block_wait
280 *
281 * @abstract
282 * Wait synchronously until execution of the specified dispatch block object has
283 * completed or until the specified timeout has elapsed.
284 *
285 * @discussion
286 * This function will return immediately if execution of the block object has
287 * already completed.
288 *
289 * It is not possible to wait for multiple executions of the same block object
290 * with this interface; use dispatch_group_wait() for that purpose. A single
291 * dispatch block object may either be waited on once and executed once,
292 * or it may be executed any number of times. The behavior of any other
293 * combination is undefined. Submission to a dispatch queue counts as an
294 * execution, even if cancellation (dispatch_block_cancel) means the block's
295 * code never runs.
296 *
297 * The result of calling this function from multiple threads simultaneously
298 * with the same dispatch block object is undefined, but note that doing so
299 * would violate the rules described in the previous paragraph.
300 *
301 * If this function returns indicating that the specified timeout has elapsed,
302 * then that invocation does not count as the one allowed wait.
303 *
304 * If at the time this function is called, the specified dispatch block object
305 * has been submitted directly to a serial queue, the system will make a best
306 * effort to apply the necessary QOS overrides to ensure that the block and any
307 * blocks submitted earlier to that serial queue are executed at the QOS class
308 * (or higher) of the thread calling dispatch_block_wait().
309 *
310 * @param block
311 * The dispatch block object to wait on.
312 * The result of passing NULL or a block object not returned by one of the
313 * dispatch_block_create* functions is undefined.
314 *
315 * @param timeout
316 * When to timeout (see dispatch_time). As a convenience, there are the
317 * DISPATCH_TIME_NOW and DISPATCH_TIME_FOREVER constants.
318 *
319 * @result
320 * Returns zero on success (the dispatch block object completed within the
321 * specified timeout) or non-zero on error (i.e. timed out).
322 */
323 API_AVAILABLE(macos(10.10), ios(8.0))
324 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
325 long
326 dispatch_block_wait(dispatch_block_t block, dispatch_time_t timeout);
327
328 /*!
329 * @function dispatch_block_notify
330 *
331 * @abstract
332 * Schedule a notification block to be submitted to a queue when the execution
333 * of a specified dispatch block object has completed.
334 *
335 * @discussion
336 * This function will submit the notification block immediately if execution of
337 * the observed block object has already completed.
338 *
339 * It is not possible to be notified of multiple executions of the same block
340 * object with this interface, use dispatch_group_notify() for that purpose.
341 *
342 * A single dispatch block object may either be observed one or more times
343 * and executed once, or it may be executed any number of times. The behavior
344 * of any other combination is undefined. Submission to a dispatch queue
345 * counts as an execution, even if cancellation (dispatch_block_cancel) means
346 * the block's code never runs.
347 *
348 * If multiple notification blocks are scheduled for a single block object,
349 * there is no defined order in which the notification blocks will be submitted
350 * to their associated queues.
351 *
352 * @param block
353 * The dispatch block object to observe.
354 * The result of passing NULL or a block object not returned by one of the
355 * dispatch_block_create* functions is undefined.
356 *
357 * @param queue
358 * The queue to which the supplied notification block will be submitted when
359 * the observed block completes.
360 *
361 * @param notification_block
362 * The notification block to submit when the observed block object completes.
363 */
364 API_AVAILABLE(macos(10.10), ios(8.0))
365 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
366 void
367 dispatch_block_notify(dispatch_block_t block, dispatch_queue_t queue,
368 dispatch_block_t notification_block);
369
370 /*!
371 * @function dispatch_block_cancel
372 *
373 * @abstract
374 * Asynchronously cancel the specified dispatch block object.
375 *
376 * @discussion
377 * Cancellation causes any future execution of the dispatch block object to
378 * return immediately, but does not affect any execution of the block object
379 * that is already in progress.
380 *
381 * Release of any resources associated with the block object will be delayed
382 * until execution of the block object is next attempted (or any execution
383 * already in progress completes).
384 *
385 * NOTE: care needs to be taken to ensure that a block object that may be
386 * canceled does not capture any resources that require execution of the
387 * block body in order to be released (e.g. memory allocated with
388 * malloc(3) that the block body calls free(3) on). Such resources will
389 * be leaked if the block body is never executed due to cancellation.
390 *
391 * @param block
392 * The dispatch block object to cancel.
393 * The result of passing NULL or a block object not returned by one of the
394 * dispatch_block_create* functions is undefined.
395 */
396 API_AVAILABLE(macos(10.10), ios(8.0))
397 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
398 void
399 dispatch_block_cancel(dispatch_block_t block);
400
401 /*!
402 * @function dispatch_block_testcancel
403 *
404 * @abstract
405 * Tests whether the given dispatch block object has been canceled.
406 *
407 * @param block
408 * The dispatch block object to test.
409 * The result of passing NULL or a block object not returned by one of the
410 * dispatch_block_create* functions is undefined.
411 *
412 * @result
413 * Non-zero if canceled and zero if not canceled.
414 */
415 API_AVAILABLE(macos(10.10), ios(8.0))
416 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE
417 DISPATCH_NOTHROW
418 long
419 dispatch_block_testcancel(dispatch_block_t block);
420
421 __END_DECLS
422
423 DISPATCH_ASSUME_NONNULL_END
424
425 #endif // __BLOCKS__
426
427 #endif // __DISPATCH_BLOCK__