]> git.saurik.com Git - apple/libdispatch.git/blob - private/queue_private.h
libdispatch-500.1.5.tar.gz
[apple/libdispatch.git] / private / queue_private.h
1 /*
2 * Copyright (c) 2008-2013 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 /*
22 * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch
23 * which are subject to change in future releases of Mac OS X. Any applications
24 * relying on these interfaces WILL break.
25 */
26
27 #ifndef __DISPATCH_QUEUE_PRIVATE__
28 #define __DISPATCH_QUEUE_PRIVATE__
29
30 #ifndef __DISPATCH_INDIRECT__
31 #error "Please #include <dispatch/private.h> instead of this file directly."
32 #include <dispatch/base.h> // for HeaderDoc
33 #endif
34
35 __BEGIN_DECLS
36
37 /*!
38 * @enum dispatch_queue_flags_t
39 *
40 * @constant DISPATCH_QUEUE_OVERCOMMIT
41 * The queue will create a new thread for invoking blocks, regardless of how
42 * busy the computer is.
43 */
44 enum {
45 DISPATCH_QUEUE_OVERCOMMIT = 0x2ull,
46 };
47
48 #define DISPATCH_QUEUE_FLAGS_MASK (DISPATCH_QUEUE_OVERCOMMIT)
49
50 /*!
51 * @function dispatch_queue_attr_make_with_overcommit
52 *
53 * @discussion
54 * Returns a dispatch queue attribute value with the overcommit flag set to the
55 * specified value.
56 *
57 * @param attr
58 * A queue attribute value to be combined with the overcommit flag, or NULL.
59 *
60 * @param overcommit
61 * Boolean overcommit flag.
62 *
63 * @return
64 * Returns an attribute value which may be provided to dispatch_queue_create().
65 * This new value combines the attributes specified by the 'attr' parameter and
66 * the overcommit flag.
67 */
68 __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_8_0)
69 DISPATCH_EXPORT DISPATCH_WARN_RESULT DISPATCH_PURE DISPATCH_NOTHROW
70 dispatch_queue_attr_t
71 dispatch_queue_attr_make_with_overcommit(dispatch_queue_attr_t attr,
72 bool overcommit);
73
74 /*!
75 * @typedef dispatch_queue_priority_t
76 *
77 * @constant DISPATCH_QUEUE_PRIORITY_NON_INTERACTIVE
78 * Items dispatched to the queue will run at non-interactive priority.
79 * This priority level is intended for user-initiated application activity that
80 * is long-running and CPU or IO intensive and that the user is actively waiting
81 * on, but that should not interfere with interactive use of the application.
82 *
83 * This global queue priority level is mapped to QOS_CLASS_UTILITY.
84 */
85 #define DISPATCH_QUEUE_PRIORITY_NON_INTERACTIVE INT8_MIN
86
87 /*!
88 * @function dispatch_queue_set_width
89 *
90 * @abstract
91 * Set the width of concurrency for a given queue. The width of a serial queue
92 * is one.
93 *
94 * @discussion
95 * This SPI is DEPRECATED and will be removed in a future release.
96 * Uses of this SPI to make a queue concurrent by setting its width to LONG_MAX
97 * should be replaced by passing DISPATCH_QUEUE_CONCURRENT to
98 * dispatch_queue_create().
99 * Uses of this SPI to limit queue concurrency are not recommended and should
100 * be replaced by alternative mechanisms such as a dispatch semaphore created
101 * with the desired concurrency width.
102 *
103 * @param queue
104 * The queue to adjust. Passing the main queue or a global concurrent queue
105 * will be ignored.
106 *
107 * @param width
108 * The new maximum width of concurrency depending on available resources.
109 * If zero is passed, then the value is promoted to one.
110 * Negative values are magic values that map to automatic width values.
111 * Unknown negative values default to DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS.
112 */
113 #define DISPATCH_QUEUE_WIDTH_ACTIVE_CPUS -1
114 #define DISPATCH_QUEUE_WIDTH_MAX_PHYSICAL_CPUS -2
115 #define DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS -3
116
117 __OSX_AVAILABLE_BUT_DEPRECATED_MSG(__MAC_10_6,__MAC_10_10,__IPHONE_4_0,__IPHONE_8_0, \
118 "Use dispatch_queue_create(name, DISPATCH_QUEUE_CONCURRENT) instead")
119 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
120 void
121 dispatch_queue_set_width(dispatch_queue_t dq, long width);
122
123 /*!
124 * @function dispatch_queue_create_with_target
125 *
126 * @abstract
127 * Creates a new dispatch queue with a specified target queue.
128 *
129 * @discussion
130 * Dispatch queues created with the DISPATCH_QUEUE_SERIAL or a NULL attribute
131 * invoke blocks serially in FIFO order.
132 *
133 * Dispatch queues created with the DISPATCH_QUEUE_CONCURRENT attribute may
134 * invoke blocks concurrently (similarly to the global concurrent queues, but
135 * potentially with more overhead), and support barrier blocks submitted with
136 * the dispatch barrier API, which e.g. enables the implementation of efficient
137 * reader-writer schemes.
138 *
139 * When a dispatch queue is no longer needed, it should be released with
140 * dispatch_release(). Note that any pending blocks submitted to a queue will
141 * hold a reference to that queue. Therefore a queue will not be deallocated
142 * until all pending blocks have finished.
143 *
144 * @param label
145 * A string label to attach to the queue.
146 * This parameter is optional and may be NULL.
147 *
148 * @param attr
149 * DISPATCH_QUEUE_SERIAL, DISPATCH_QUEUE_CONCURRENT, or the result of a call to
150 * the function dispatch_queue_attr_make_with_qos_class().
151 *
152 * @param target
153 * The target queue for the newly created queue. The target queue is retained.
154 * If this parameter is DISPATCH_TARGET_QUEUE_DEFAULT, sets the queue's target
155 * queue to the default target queue for the given queue type.
156 *
157 * @result
158 * The newly created dispatch queue.
159 */
160 __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_7_0)
161 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
162 DISPATCH_NOTHROW
163 dispatch_queue_t
164 dispatch_queue_create_with_target(const char *label,
165 dispatch_queue_attr_t attr, dispatch_queue_t target);
166
167 #ifdef __BLOCKS__
168 /*!
169 * @function dispatch_pthread_root_queue_create
170 *
171 * @abstract
172 * Creates a new concurrent dispatch root queue with a pthread-based pool of
173 * worker threads owned by the application.
174 *
175 * @discussion
176 * Dispatch pthread root queues are similar to the global concurrent dispatch
177 * queues in that they invoke blocks concurrently, however the blocks are not
178 * executed on ordinary worker threads but use a dedicated pool of pthreads not
179 * shared with the global queues or any other pthread root queues.
180 *
181 * NOTE: this is a special-purpose facility that should only be used in very
182 * limited circumstances, in almost all cases the global concurrent queues
183 * should be preferred. While this facility allows for more flexibility in
184 * configuring worker threads for special needs it comes at the cost of
185 * increased overall memory usage due to reduced thread sharing and higher
186 * latency in worker thread bringup.
187 *
188 * Dispatch pthread root queues do not support suspension, application context
189 * and change of width or of target queue. They can however be used as the
190 * target queue for serial or concurrent queues obtained via
191 * dispatch_queue_create() or dispatch_queue_create_with_target(), which
192 * enables the blocks submitted to those queues to be processed on the root
193 * queue's pthread pool.
194 *
195 * When a dispatch pthread root queue is no longer needed, it should be
196 * released with dispatch_release(). Existing worker pthreads and pending blocks
197 * submitted to the root queue will hold a reference to the queue so it will not
198 * be deallocated until all blocks have finished and worker threads exited.
199 *
200 * @param label
201 * A string label to attach to the queue.
202 * This parameter is optional and may be NULL.
203 *
204 * @param flags
205 * Pass flags value returned by dispatch_pthread_root_queue_flags_pool_size()
206 * or 0 if unused.
207 *
208 * @param attr
209 * Attributes passed to pthread_create(3) when creating worker pthreads. This
210 * parameter is copied and can be destroyed after this call returns.
211 * This parameter is optional and may be NULL.
212 *
213 * @param configure
214 * Configuration block called on newly created worker pthreads before any blocks
215 * for the root queue are executed. The block may configure the current thread
216 * as needed.
217 * This parameter is optional and may be NULL.
218 *
219 * @result
220 * The newly created dispatch pthread root queue.
221 */
222 __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_6_0)
223 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
224 DISPATCH_NOTHROW
225 dispatch_queue_t
226 dispatch_pthread_root_queue_create(const char *label, unsigned long flags,
227 const pthread_attr_t *attr, dispatch_block_t configure);
228
229 /*!
230 * @function dispatch_pthread_root_queue_flags_pool_size
231 *
232 * @abstract
233 * Returns flags argument to pass to dispatch_pthread_root_queue_create() to
234 * specify the maximum size of the pthread pool to use for a pthread root queue.
235 *
236 * @param pool_size
237 * Maximum size of the pthread pool to use for the root queue. The number of
238 * pthreads created for this root queue will never exceed this number but there
239 * is no guarantee that the specified number will be reached.
240 * Pass 0 to specify that a default pool size determined by the system should
241 * be used.
242 * NOTE: passing pool_size == 1 does NOT make the pthread root queue equivalent
243 * to a serial queue.
244 *
245 * @result
246 * The flags argument to pass to dispatch_pthread_root_queue_create().
247 */
248 DISPATCH_INLINE DISPATCH_ALWAYS_INLINE
249 unsigned long
250 dispatch_pthread_root_queue_flags_pool_size(uint8_t pool_size)
251 {
252 #define _DISPATCH_PTHREAD_ROOT_QUEUE_FLAG_POOL_SIZE (0x80000000ul)
253 return (_DISPATCH_PTHREAD_ROOT_QUEUE_FLAG_POOL_SIZE |
254 (unsigned long)pool_size);
255 }
256
257 #endif /* __BLOCKS__ */
258
259 /*!
260 * @constant DISPATCH_APPLY_CURRENT_ROOT_QUEUE
261 * @discussion Constant to pass to the dispatch_apply() and dispatch_apply_f()
262 * functions to indicate that the root queue for the current thread should be
263 * used (i.e. one of the global concurrent queues or a queue created with
264 * dispatch_pthread_root_queue_create()). If there is no such queue, the
265 * default priority global concurrent queue will be used.
266 */
267 #define DISPATCH_APPLY_CURRENT_ROOT_QUEUE NULL
268
269 /*!
270 * @function dispatch_assert_queue
271 *
272 * @abstract
273 * Verifies that the current block is executing on a certain dispatch queue.
274 *
275 * @discussion
276 * Some code expects to be run on a specific dispatch queue. This function
277 * verifies that expectation for debugging.
278 *
279 * This function will only return if the currently executing block was submitted
280 * to the specified queue or to any queue targeting it (see
281 * dispatch_set_target_queue()). Otherwise, it logs an explanation to the system
282 * log, then terminates the application.
283 *
284 * When dispatch_assert_queue() is called outside of the context of a
285 * submitted block, its behavior is undefined.
286 *
287 * Passing the result of dispatch_get_main_queue() to this function verifies
288 * that the current block was submitted to the main queue or to a queue
289 * targeting it.
290 * IMPORTANT: this is NOT the same as verifying that the current block is
291 * executing on the main thread.
292 *
293 * The variant dispatch_assert_queue_debug() is compiled out when the
294 * preprocessor macro NDEBUG is defined. (See also assert(3)).
295 *
296 * @param queue
297 * The dispatch queue that the current block is expected to run on.
298 * The result of passing NULL in this parameter is undefined.
299 */
300 __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_7_0)
301 DISPATCH_EXPORT DISPATCH_NONNULL1
302 void
303 dispatch_assert_queue(dispatch_queue_t queue);
304
305 /*!
306 * @function dispatch_assert_queue_not
307 *
308 * @abstract
309 * Verifies that the current block is not executing on a certain dispatch queue.
310 *
311 * @discussion
312 * This function is the equivalent of dispatch_queue_assert() with the test for
313 * equality inverted. See discussion there.
314 *
315 * The variant dispatch_assert_queue_not_debug() is compiled out when the
316 * preprocessor macro NDEBUG is defined. (See also assert(3)).
317 *
318 * @param queue
319 * The dispatch queue that the current block is expected not to run on.
320 * The result of passing NULL in this parameter is undefined.
321 */
322 __OSX_AVAILABLE_STARTING(__MAC_10_9,__IPHONE_7_0)
323 DISPATCH_EXPORT DISPATCH_NONNULL1
324 void
325 dispatch_assert_queue_not(dispatch_queue_t queue);
326
327 #ifdef NDEBUG
328 #define dispatch_assert_queue_debug(q) ((void)0)
329 #define dispatch_assert_queue_not_debug(q) ((void)0)
330 #else
331 #define dispatch_assert_queue_debug(q) dispatch_assert_queue(q)
332 #define dispatch_assert_queue_not_debug(q) dispatch_assert_queue_not(q)
333 #endif
334
335 /*!
336 * @function dispatch_async_enforce_qos_class_f
337 *
338 * @abstract
339 * Submits a function for asynchronous execution on a dispatch queue.
340 *
341 * @discussion
342 * See dispatch_async() for details. The QOS will be enforced as if
343 * this was called:
344 * <code>
345 * dispatch_async(queue, dispatch_block_create(DISPATCH_BLOCK_ENFORCE_QOS_CLASS, ^{
346 * work(context);
347 * });
348 * </code>
349 *
350 * @param queue
351 * The target dispatch queue to which the function is submitted.
352 * The system will hold a reference on the target queue until the function
353 * has returned.
354 * The result of passing NULL in this parameter is undefined.
355 *
356 * @param context
357 * The application-defined context parameter to pass to the function.
358 *
359 * @param work
360 * The application-defined function to invoke on the target queue. The first
361 * parameter passed to this function is the context provided to
362 * dispatch_async_f().
363 * The result of passing NULL in this parameter is undefined.
364 */
365 __OSX_AVAILABLE_STARTING(__MAC_10_11,__IPHONE_9_0)
366 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
367 void
368 dispatch_async_enforce_qos_class_f(dispatch_queue_t queue,
369 void *context,
370 dispatch_function_t work);
371
372
373 __END_DECLS
374
375 #endif