queue_private.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch
  22. * which are subject to change in future releases of Mac OS X. Any applications
  23. * relying on these interfaces WILL break.
  24. */
  25. #ifndef __DISPATCH_QUEUE_PRIVATE__
  26. #define __DISPATCH_QUEUE_PRIVATE__
  27. #ifndef __DISPATCH_INDIRECT__
  28. #error "Please #include <dispatch/private.h> instead of this file directly."
  29. #include <dispatch/base.h> // for HeaderDoc
  30. #endif
  31. DISPATCH_ASSUME_NONNULL_BEGIN
  32. __BEGIN_DECLS
  33. /*!
  34. * @enum dispatch_queue_flags_t
  35. *
  36. * @constant DISPATCH_QUEUE_OVERCOMMIT
  37. * The queue will create a new thread for invoking blocks, regardless of how
  38. * busy the computer is.
  39. */
  40. enum {
  41. DISPATCH_QUEUE_OVERCOMMIT = 0x2ull,
  42. };
  43. /*!
  44. * @function dispatch_set_qos_class
  45. *
  46. * @abstract
  47. * Sets the QOS class on a dispatch queue, source or mach channel.
  48. *
  49. * @discussion
  50. * This is equivalent to using dispatch_queue_make_attr_with_qos_class()
  51. * when creating a dispatch queue, but is availabile on additional dispatch
  52. * object types.
  53. *
  54. * When configured in this manner, the specified QOS class will be used over
  55. * the assigned QOS of workitems submitted asynchronously to this object,
  56. * unless the workitem has been created with ENFORCE semantics
  57. * (see DISPATCH_BLOCK_ENFORCE_QOS_CLASS).
  58. *
  59. * Calling this function will supersede any prior calls to
  60. * dispatch_set_qos_class() or dispatch_set_qos_class_floor().
  61. *
  62. * @param object
  63. * A dispatch queue, source or mach channel to configure.
  64. * The object must be inactive, and can't be a workloop.
  65. *
  66. * Passing another object type or an object that has been activated is undefined
  67. * and will cause the process to be terminated.
  68. *
  69. * @param qos_class
  70. * A QOS class value:
  71. * - QOS_CLASS_USER_INTERACTIVE
  72. * - QOS_CLASS_USER_INITIATED
  73. * - QOS_CLASS_DEFAULT
  74. * - QOS_CLASS_UTILITY
  75. * - QOS_CLASS_BACKGROUND
  76. * Passing any other value is undefined.
  77. *
  78. * @param relative_priority
  79. * A relative priority within the QOS class. This value is a negative
  80. * offset from the maximum supported scheduler priority for the given class.
  81. * Passing a value greater than zero or less than QOS_MIN_RELATIVE_PRIORITY
  82. * is undefined.
  83. */
  84. API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
  85. DISPATCH_EXPORT DISPATCH_NOTHROW
  86. void
  87. dispatch_set_qos_class(dispatch_object_t object,
  88. dispatch_qos_class_t qos_class, int relative_priority);
  89. /*!
  90. * @function dispatch_set_qos_class_floor
  91. *
  92. * @abstract
  93. * Sets the QOS class floor on a dispatch queue, source, workloop or mach
  94. * channel.
  95. *
  96. * @discussion
  97. * The QOS class of workitems submitted to this object asynchronously will be
  98. * elevated to at least the specified QOS class floor.
  99. * Unlike dispatch_set_qos_class(), the QOS of the workitem will be used if
  100. * higher than the floor even when the workitem has been created without
  101. * "ENFORCE" semantics.
  102. *
  103. * Setting the QOS class floor is equivalent to the QOS effects of configuring
  104. * a target queue whose QOS class has been set with dispatch_set_qos_class().
  105. *
  106. * Calling this function will supersede any prior calls to
  107. * dispatch_set_qos_class() or dispatch_set_qos_class_floor().
  108. *
  109. * @param object
  110. * A dispatch queue, workloop, source or mach channel to configure.
  111. * The object must be inactive.
  112. *
  113. * Passing another object type or an object that has been activated is undefined
  114. * and will cause the process to be terminated.
  115. *
  116. * @param qos_class
  117. * A QOS class value:
  118. * - QOS_CLASS_USER_INTERACTIVE
  119. * - QOS_CLASS_USER_INITIATED
  120. * - QOS_CLASS_DEFAULT
  121. * - QOS_CLASS_UTILITY
  122. * - QOS_CLASS_BACKGROUND
  123. * Passing any other value is undefined.
  124. *
  125. * @param relative_priority
  126. * A relative priority within the QOS class. This value is a negative
  127. * offset from the maximum supported scheduler priority for the given class.
  128. * Passing a value greater than zero or less than QOS_MIN_RELATIVE_PRIORITY
  129. * is undefined.
  130. */
  131. API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
  132. DISPATCH_EXPORT DISPATCH_NOTHROW
  133. void
  134. dispatch_set_qos_class_floor(dispatch_object_t object,
  135. dispatch_qos_class_t qos_class, int relative_priority);
  136. /*!
  137. * @function dispatch_set_qos_class_fallback
  138. *
  139. * @abstract
  140. * Sets the fallback QOS class on a dispatch queue, source, workloop or mach
  141. * channel.
  142. *
  143. * @discussion
  144. * Workitems submitted asynchronously to this object that don't have an assigned
  145. * QOS class will use the specified QOS class as a fallback. This interface
  146. * doesn't support relative priority.
  147. *
  148. * Workitems without an assigned QOS are:
  149. * - workitems submitted from the context of a thread opted-out of QOS,
  150. * - workitems created with the DISPATCH_BLOCK_DETACHED or
  151. * DISPATCH_BLOCK_NO_QOS_CLASS flags,
  152. * - XPC messages sent with xpc_connection_send_notification(),
  153. * - XPC connection and dispatch source handlers.
  154. *
  155. * Calling both dispatch_set_qos_class_fallback() and dispatch_set_qos_class()
  156. * on an object will only apply the effect of dispatch_set_qos_class().
  157. *
  158. * A QOS class fallback must always be at least as high as the current QOS
  159. * floor for the dispatch queue hierarchy, else it is ignored.
  160. *
  161. * When no QOS fallback has been explicitly specified:
  162. * - queues on hierarchies without a QOS class or QOS class floor have
  163. * a fallback of QOS_CLASS_DEFAULT,
  164. * - queues on hierarchies with a QOS class or QOS class floor configured will
  165. * also use that QOS class as a fallback.
  166. *
  167. * @param object
  168. * A dispatch queue, workloop, source or mach channel to configure.
  169. * The object must be inactive.
  170. *
  171. * Passing another object type or an object that has been activated is undefined
  172. * and will cause the process to be terminated.
  173. *
  174. * @param qos_class
  175. * A QOS class value:
  176. * - QOS_CLASS_USER_INTERACTIVE
  177. * - QOS_CLASS_USER_INITIATED
  178. * - QOS_CLASS_DEFAULT
  179. * - QOS_CLASS_UTILITY
  180. * - QOS_CLASS_BACKGROUND
  181. * Passing any other value is undefined.
  182. */
  183. API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
  184. DISPATCH_EXPORT DISPATCH_NOTHROW
  185. void
  186. dispatch_set_qos_class_fallback(dispatch_object_t object,
  187. dispatch_qos_class_t qos_class);
  188. #define DISPATCH_QUEUE_FLAGS_MASK (DISPATCH_QUEUE_OVERCOMMIT)
  189. // On FreeBSD pthread_attr_t is a typedef to a pointer type
  190. #if defined(__FreeBSD__)
  191. # define DISPATCH_QUEUE_NULLABLE_PTHREAD_ATTR_PTR _Nullable
  192. #else
  193. # define DISPATCH_QUEUE_NULLABLE_PTHREAD_ATTR_PTR
  194. #endif
  195. /*!
  196. * @function dispatch_queue_attr_make_with_overcommit
  197. *
  198. * @discussion
  199. * Returns a dispatch queue attribute value with the overcommit flag set to the
  200. * specified value.
  201. *
  202. * This attribute only makes sense when the specified queue is targeted at
  203. * a root queue. Passing this attribute to dispatch_queue_create_with_target()
  204. * with a target queue that is not a root queue will result in an assertion and
  205. * the process being terminated.
  206. *
  207. * It is recommended to not specify a target queue at all when using this
  208. * attribute and to use dispatch_queue_attr_make_with_qos_class() to select the
  209. * appropriate QOS class instead.
  210. *
  211. * Queues created with this attribute cannot change target after having been
  212. * activated. See dispatch_set_target_queue() and dispatch_activate().
  213. *
  214. * @param attr
  215. * A queue attribute value to be combined with the overcommit flag, or NULL.
  216. *
  217. * @param overcommit
  218. * Boolean overcommit flag.
  219. *
  220. * @return
  221. * Returns an attribute value which may be provided to dispatch_queue_create().
  222. * This new value combines the attributes specified by the 'attr' parameter and
  223. * the overcommit flag.
  224. */
  225. API_AVAILABLE(macos(10.10), ios(8.0))
  226. DISPATCH_EXPORT DISPATCH_WARN_RESULT DISPATCH_PURE DISPATCH_NOTHROW
  227. dispatch_queue_attr_t
  228. dispatch_queue_attr_make_with_overcommit(dispatch_queue_attr_t _Nullable attr,
  229. bool overcommit);
  230. /*!
  231. * @typedef dispatch_queue_priority_t
  232. *
  233. * @constant DISPATCH_QUEUE_PRIORITY_NON_INTERACTIVE
  234. * Items dispatched to the queue will run at non-interactive priority.
  235. * This priority level is intended for user-initiated application activity that
  236. * is long-running and CPU or IO intensive and that the user is actively waiting
  237. * on, but that should not interfere with interactive use of the application.
  238. *
  239. * This global queue priority level is mapped to QOS_CLASS_UTILITY.
  240. */
  241. #define DISPATCH_QUEUE_PRIORITY_NON_INTERACTIVE INT8_MIN
  242. /*!
  243. * @function dispatch_queue_set_label_nocopy
  244. *
  245. * @abstract
  246. * Set the label for a given queue, without copying the input string.
  247. *
  248. * @discussion
  249. * The queue must have been initially created with a NULL label, else using
  250. * this function to set the queue label is undefined.
  251. *
  252. * The caller of this function must make sure the label pointer remains valid
  253. * while it is used as the queue label and while any callers to
  254. * dispatch_queue_get_label() may have obtained it. Since the queue lifetime
  255. * may extend past the last release, it is advised to call this function with
  256. * a constant string or NULL before the queue is released, or to destroy the
  257. * label from a finalizer for that queue.
  258. *
  259. * This function should be called before any work item could call
  260. * dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL) or from the context of
  261. * the queue itself.
  262. *
  263. * @param queue
  264. * The queue to adjust. Attempts to set the label of the main queue or a global
  265. * concurrent queue will be ignored.
  266. *
  267. * @param label
  268. * The new label for the queue.
  269. */
  270. API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0))
  271. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
  272. void
  273. dispatch_queue_set_label_nocopy(dispatch_queue_t queue,
  274. const char * _Nullable label);
  275. /*!
  276. * @function dispatch_queue_set_width
  277. *
  278. * @abstract
  279. * Set the width of concurrency for a given queue. The width of a serial queue
  280. * is one.
  281. *
  282. * @discussion
  283. * This SPI is DEPRECATED and will be removed in a future release.
  284. * Uses of this SPI to make a queue concurrent by setting its width to LONG_MAX
  285. * should be replaced by passing DISPATCH_QUEUE_CONCURRENT to
  286. * dispatch_queue_create().
  287. * Uses of this SPI to limit queue concurrency are not recommended and should
  288. * be replaced by alternative mechanisms such as a dispatch semaphore created
  289. * with the desired concurrency width.
  290. *
  291. * @param queue
  292. * The queue to adjust. Attempts to set the width of the main queue or a global
  293. * concurrent queue will be ignored.
  294. *
  295. * @param width
  296. * The new maximum width of concurrency depending on available resources.
  297. * If zero is passed, then the value is promoted to one.
  298. * Negative values are magic values that map to automatic width values.
  299. * Unknown negative values default to DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS.
  300. */
  301. #define DISPATCH_QUEUE_WIDTH_ACTIVE_CPUS -1
  302. #define DISPATCH_QUEUE_WIDTH_MAX_PHYSICAL_CPUS -2
  303. #define DISPATCH_QUEUE_WIDTH_MAX_LOGICAL_CPUS -3
  304. API_DEPRECATED("Use dispatch_queue_create(name, DISPATCH_QUEUE_CONCURRENT)",
  305. macos(10.6,10.10), ios(4.0,8.0))
  306. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  307. void
  308. dispatch_queue_set_width(dispatch_queue_t dq, long width);
  309. #if defined(__BLOCKS__) && defined(__APPLE__)
  310. /*!
  311. * @function dispatch_pthread_root_queue_create
  312. *
  313. * @abstract
  314. * Creates a new concurrent dispatch root queue with a pthread-based pool of
  315. * worker threads owned by the application.
  316. *
  317. * @discussion
  318. * Dispatch pthread root queues are similar to the global concurrent dispatch
  319. * queues in that they invoke blocks concurrently, however the blocks are not
  320. * executed on ordinary worker threads but use a dedicated pool of pthreads not
  321. * shared with the global queues or any other pthread root queues.
  322. *
  323. * NOTE: this is a special-purpose facility that should only be used in very
  324. * limited circumstances, in almost all cases the global concurrent queues
  325. * should be preferred. While this facility allows for more flexibility in
  326. * configuring worker threads for special needs it comes at the cost of
  327. * increased overall memory usage due to reduced thread sharing and higher
  328. * latency in worker thread bringup.
  329. *
  330. * Dispatch pthread root queues do not support suspension, application context
  331. * and change of width or of target queue. They can however be used as the
  332. * target queue for serial or concurrent queues obtained via
  333. * dispatch_queue_create() or dispatch_queue_create_with_target(), which
  334. * enables the blocks submitted to those queues to be processed on the root
  335. * queue's pthread pool.
  336. *
  337. * When a dispatch pthread root queue is no longer needed, it should be
  338. * released with dispatch_release(). Existing worker pthreads and pending blocks
  339. * submitted to the root queue will hold a reference to the queue so it will not
  340. * be deallocated until all blocks have finished and worker threads exited.
  341. *
  342. * @param label
  343. * A string label to attach to the queue.
  344. * This parameter is optional and may be NULL.
  345. *
  346. * @param flags
  347. * Pass flags value returned by dispatch_pthread_root_queue_flags_pool_size()
  348. * or 0 if unused.
  349. *
  350. * @param attr
  351. * Attributes passed to pthread_create(3) when creating worker pthreads. This
  352. * parameter is copied and can be destroyed after this call returns.
  353. * This parameter is optional and may be NULL.
  354. *
  355. * @param configure
  356. * Configuration block called on newly created worker pthreads before any blocks
  357. * for the root queue are executed. The block may configure the current thread
  358. * as needed.
  359. * This parameter is optional and may be NULL.
  360. *
  361. * @result
  362. * The newly created dispatch pthread root queue.
  363. */
  364. API_AVAILABLE(macos(10.9), ios(6.0)) DISPATCH_LINUX_UNAVAILABLE()
  365. DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
  366. DISPATCH_NOTHROW
  367. dispatch_queue_global_t
  368. dispatch_pthread_root_queue_create(const char *_Nullable label,
  369. unsigned long flags, const pthread_attr_t DISPATCH_QUEUE_NULLABLE_PTHREAD_ATTR_PTR *_Nullable attr,
  370. dispatch_block_t _Nullable configure);
  371. /*!
  372. * @function dispatch_pthread_root_queue_flags_pool_size
  373. *
  374. * @abstract
  375. * Returns flags argument to pass to dispatch_pthread_root_queue_create() to
  376. * specify the maximum size of the pthread pool to use for a pthread root queue.
  377. *
  378. * @param pool_size
  379. * Maximum size of the pthread pool to use for the root queue. The number of
  380. * pthreads created for this root queue will never exceed this number but there
  381. * is no guarantee that the specified number will be reached.
  382. * Pass 0 to specify that a default pool size determined by the system should
  383. * be used.
  384. * NOTE: passing pool_size == 1 does NOT make the pthread root queue equivalent
  385. * to a serial queue.
  386. *
  387. * @result
  388. * The flags argument to pass to dispatch_pthread_root_queue_create().
  389. */
  390. DISPATCH_INLINE DISPATCH_ALWAYS_INLINE
  391. unsigned long
  392. dispatch_pthread_root_queue_flags_pool_size(uint8_t pool_size)
  393. {
  394. #define _DISPATCH_PTHREAD_ROOT_QUEUE_FLAG_POOL_SIZE (0x80000000ul)
  395. return (_DISPATCH_PTHREAD_ROOT_QUEUE_FLAG_POOL_SIZE |
  396. (unsigned long)pool_size);
  397. }
  398. /*!
  399. * @function dispatch_pthread_root_queue_copy_current
  400. *
  401. * @abstract
  402. * Returns a reference to the pthread root queue object that has created the
  403. * currently executing thread, or NULL if the current thread is not associated
  404. * to a pthread root queue.
  405. *
  406. * @result
  407. * A new reference to a pthread root queue object or NULL.
  408. */
  409. API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  410. DISPATCH_LINUX_UNAVAILABLE()
  411. DISPATCH_EXPORT DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  412. dispatch_queue_global_t _Nullable
  413. dispatch_pthread_root_queue_copy_current(void);
  414. /*!
  415. * @constant DISPATCH_APPLY_CURRENT_ROOT_QUEUE
  416. *
  417. * @discussion
  418. * This constant is deprecated, please use DISPATCH_APPLY_AUTO.
  419. *
  420. * DISPATCH_APPLY_AUTO also selects the current pthread root queue if
  421. * applicable.
  422. */
  423. #define DISPATCH_APPLY_CURRENT_ROOT_QUEUE ((dispatch_queue_t _Nonnull)0)
  424. #endif /* defined(__BLOCKS__) && defined(__APPLE__) */
  425. /*!
  426. * @function dispatch_async_enforce_qos_class_f
  427. *
  428. * @abstract
  429. * Submits a function for asynchronous execution on a dispatch queue.
  430. *
  431. * @discussion
  432. * See dispatch_async() for details. The QOS will be enforced as if
  433. * this was called:
  434. * <code>
  435. * dispatch_async(queue, dispatch_block_create(DISPATCH_BLOCK_ENFORCE_QOS_CLASS, ^{
  436. * work(context);
  437. * });
  438. * </code>
  439. *
  440. * @param queue
  441. * The target dispatch queue to which the function is submitted.
  442. * The system will hold a reference on the target queue until the function
  443. * has returned.
  444. * The result of passing NULL in this parameter is undefined.
  445. *
  446. * @param context
  447. * The application-defined context parameter to pass to the function.
  448. *
  449. * @param work
  450. * The application-defined function to invoke on the target queue. The first
  451. * parameter passed to this function is the context provided to
  452. * dispatch_async_f().
  453. * The result of passing NULL in this parameter is undefined.
  454. */
  455. API_AVAILABLE(macos(10.11), ios(9.0))
  456. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
  457. void
  458. dispatch_async_enforce_qos_class_f(dispatch_queue_t queue,
  459. void *_Nullable context, dispatch_function_t work);
  460. #ifdef __ANDROID__
  461. /*!
  462. * @function _dispatch_install_thread_detach_callback
  463. *
  464. * @param cb
  465. * Function to be called before each worker thread exits to detach JVM.
  466. *
  467. * Hook to be able to detach threads from the Java JVM before they exit.
  468. * If JNI has been used on a thread on Android it needs to have been
  469. * "detached" before the thread exits or the application will crash.
  470. */
  471. DISPATCH_EXPORT
  472. void _dispatch_install_thread_detach_callback(void (*cb)(void));
  473. #endif
  474. __END_DECLS
  475. DISPATCH_ASSUME_NONNULL_END
  476. #endif