queue.h 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. /*
  2. * Copyright (c) 2008-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. #ifndef __DISPATCH_QUEUE__
  21. #define __DISPATCH_QUEUE__
  22. #ifndef __DISPATCH_INDIRECT__
  23. #error "Please #include <dispatch/dispatch.h> instead of this file directly."
  24. #include <dispatch/base.h> // for HeaderDoc
  25. #endif
  26. #if __has_include(<sys/qos.h>)
  27. #include <sys/qos.h>
  28. #endif
  29. DISPATCH_ASSUME_NONNULL_BEGIN
  30. /*!
  31. * @header
  32. *
  33. * Dispatch is an abstract model for expressing concurrency via simple but
  34. * powerful API.
  35. *
  36. * At the core, dispatch provides serial FIFO queues to which blocks may be
  37. * submitted. Blocks submitted to these dispatch queues are invoked on a pool
  38. * of threads fully managed by the system. No guarantee is made regarding
  39. * which thread a block will be invoked on; however, it is guaranteed that only
  40. * one block submitted to the FIFO dispatch queue will be invoked at a time.
  41. *
  42. * When multiple queues have blocks to be processed, the system is free to
  43. * allocate additional threads to invoke the blocks concurrently. When the
  44. * queues become empty, these threads are automatically released.
  45. */
  46. /*!
  47. * @typedef dispatch_queue_t
  48. *
  49. * @abstract
  50. * Dispatch queues invoke workitems submitted to them.
  51. *
  52. * @discussion
  53. * Dispatch queues come in many flavors, the most common one being the dispatch
  54. * serial queue (See dispatch_queue_serial_t).
  55. *
  56. * The system manages a pool of threads which process dispatch queues and invoke
  57. * workitems 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 workitems 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. * @typedef dispatch_queue_global_t
  70. *
  71. * @abstract
  72. * Dispatch global concurrent queues are an abstraction around the system thread
  73. * pool which invokes workitems that are submitted to dispatch queues.
  74. *
  75. * @discussion
  76. * Dispatch global concurrent queues provide buckets of priorities on top of the
  77. * thread pool the system manages. The system will decide how many threads
  78. * to allocate to this pool depending on demand and system load. In particular,
  79. * the system tries to maintain a good level of concurrency for this resource,
  80. * and will create new threads when too many existing worker threads block in
  81. * system calls.
  82. *
  83. * The global concurrent queues are a shared resource and as such it is the
  84. * responsiblity of every user of this resource to not submit an unbounded
  85. * amount of work to this pool, especially work that may block, as this can
  86. * cause the system to spawn very large numbers of threads (aka. thread
  87. * explosion).
  88. *
  89. * Work items submitted to the global concurrent queues have no ordering
  90. * guarantee with respect to the order of submission, and workitems submitted
  91. * to these queues may be invoked concurrently.
  92. *
  93. * Dispatch global concurrent queues are well-known global objects that are
  94. * returned by dispatch_get_global_queue(). These objects cannot be modified.
  95. * Calls to dispatch_suspend(), dispatch_resume(), dispatch_set_context(), etc.,
  96. * will have no effect when used with queues of this type.
  97. */
  98. #if defined(__DISPATCH_BUILDING_DISPATCH__) && !defined(__OBJC__)
  99. typedef struct dispatch_queue_global_s *dispatch_queue_global_t;
  100. #else
  101. DISPATCH_DECL_SUBCLASS(dispatch_queue_global, dispatch_queue);
  102. #endif
  103. /*!
  104. * @typedef dispatch_queue_serial_t
  105. *
  106. * @abstract
  107. * Dispatch serial queues invoke workitems submitted to them serially in FIFO
  108. * order.
  109. *
  110. * @discussion
  111. * Dispatch serial queues are lightweight objects to which workitems may be
  112. * submitted to be invoked in FIFO order. A serial queue will only invoke one
  113. * workitem at a time, but independent serial queues may each invoke their work
  114. * items concurrently with respect to each other.
  115. *
  116. * Serial queues can target each other (See dispatch_set_target_queue()). The
  117. * serial queue at the bottom of a queue hierarchy provides an exclusion
  118. * context: at most one workitem submitted to any of the queues in such
  119. * a hiearchy will run at any given time.
  120. *
  121. * Such hierarchies provide a natural construct to organize an application
  122. * subsystem around.
  123. *
  124. * Serial queues are created by passing a dispatch queue attribute derived from
  125. * DISPATCH_QUEUE_SERIAL to dispatch_queue_create_with_target().
  126. */
  127. #if defined(__DISPATCH_BUILDING_DISPATCH__) && !defined(__OBJC__)
  128. typedef struct dispatch_lane_s *dispatch_queue_serial_t;
  129. #else
  130. DISPATCH_DECL_SUBCLASS(dispatch_queue_serial, dispatch_queue);
  131. #endif
  132. /*!
  133. * @typedef dispatch_queue_main_t
  134. *
  135. * @abstract
  136. * The type of the default queue that is bound to the main thread.
  137. *
  138. * @discussion
  139. * The main queue is a serial queue (See dispatch_queue_serial_t) which is bound
  140. * to the main thread of an application.
  141. *
  142. * In order to invoke workitems submitted to the main queue, the application
  143. * must call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the
  144. * main thread.
  145. *
  146. * The main queue is a well known global object that is made automatically on
  147. * behalf of the main thread during process initialization and is returned by
  148. * dispatch_get_main_queue(). This object cannot be modified. Calls to
  149. * dispatch_suspend(), dispatch_resume(), dispatch_set_context(), etc., will
  150. * have no effect when used on the main queue.
  151. */
  152. #if defined(__DISPATCH_BUILDING_DISPATCH__) && !defined(__OBJC__)
  153. typedef struct dispatch_queue_static_s *dispatch_queue_main_t;
  154. #else
  155. DISPATCH_DECL_SUBCLASS(dispatch_queue_main, dispatch_queue_serial);
  156. #endif
  157. /*!
  158. * @typedef dispatch_queue_concurrent_t
  159. *
  160. * @abstract
  161. * Dispatch concurrent queues invoke workitems submitted to them concurrently,
  162. * and admit a notion of barrier workitems.
  163. *
  164. * @discussion
  165. * Dispatch concurrent queues are lightweight objects to which regular and
  166. * barrier workitems may be submited. Barrier workitems are invoked in
  167. * exclusion of any other kind of workitem in FIFO order.
  168. *
  169. * Regular workitems can be invoked concurrently for the same concurrent queue,
  170. * in any order. However, regular workitems will not be invoked before any
  171. * barrier workitem submited ahead of them has been invoked.
  172. *
  173. * In other words, if a serial queue is equivalent to a mutex in the Dispatch
  174. * world, a concurrent queue is equivalent to a reader-writer lock, where
  175. * regular items are readers and barriers are writers.
  176. *
  177. * Concurrent queues are created by passing a dispatch queue attribute derived
  178. * from DISPATCH_QUEUE_CONCURRENT to dispatch_queue_create_with_target().
  179. *
  180. * Caveat:
  181. * Dispatch concurrent queues at this time do not implement priority inversion
  182. * avoidance when lower priority regular workitems (readers) are being invoked
  183. * and are preventing a higher priority barrier (writer) from being invoked.
  184. */
  185. #if defined(__DISPATCH_BUILDING_DISPATCH__) && !defined(__OBJC__)
  186. typedef struct dispatch_lane_s *dispatch_queue_concurrent_t;
  187. #else
  188. DISPATCH_DECL_SUBCLASS(dispatch_queue_concurrent, dispatch_queue);
  189. #endif
  190. __BEGIN_DECLS
  191. /*!
  192. * @function dispatch_async
  193. *
  194. * @abstract
  195. * Submits a block for asynchronous execution on a dispatch queue.
  196. *
  197. * @discussion
  198. * The dispatch_async() function is the fundamental mechanism for submitting
  199. * blocks to a dispatch queue.
  200. *
  201. * Calls to dispatch_async() always return immediately after the block has
  202. * been submitted, and never wait for the block to be invoked.
  203. *
  204. * The target queue determines whether the block will be invoked serially or
  205. * concurrently with respect to other blocks submitted to that same queue.
  206. * Serial queues are processed concurrently with respect to each other.
  207. *
  208. * @param queue
  209. * The target dispatch queue to which the block is submitted.
  210. * The system will hold a reference on the target queue until the block
  211. * has finished.
  212. * The result of passing NULL in this parameter is undefined.
  213. *
  214. * @param block
  215. * The block to submit to the target dispatch queue. This function performs
  216. * Block_copy() and Block_release() on behalf of callers.
  217. * The result of passing NULL in this parameter is undefined.
  218. */
  219. #ifdef __BLOCKS__
  220. API_AVAILABLE(macos(10.6), ios(4.0))
  221. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  222. void
  223. dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
  224. #endif
  225. /*!
  226. * @function dispatch_async_f
  227. *
  228. * @abstract
  229. * Submits a function for asynchronous execution on a dispatch queue.
  230. *
  231. * @discussion
  232. * See dispatch_async() for details.
  233. *
  234. * @param queue
  235. * The target dispatch queue to which the function is submitted.
  236. * The system will hold a reference on the target queue until the function
  237. * has returned.
  238. * The result of passing NULL in this parameter is undefined.
  239. *
  240. * @param context
  241. * The application-defined context parameter to pass to the function.
  242. *
  243. * @param work
  244. * The application-defined function to invoke on the target queue. The first
  245. * parameter passed to this function is the context provided to
  246. * dispatch_async_f().
  247. * The result of passing NULL in this parameter is undefined.
  248. */
  249. API_AVAILABLE(macos(10.6), ios(4.0))
  250. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
  251. void
  252. dispatch_async_f(dispatch_queue_t queue,
  253. void *_Nullable context, dispatch_function_t work);
  254. /*!
  255. * @function dispatch_sync
  256. *
  257. * @abstract
  258. * Submits a block for synchronous execution on a dispatch queue.
  259. *
  260. * @discussion
  261. * Submits a workitem to a dispatch queue like dispatch_async(), however
  262. * dispatch_sync() will not return until the workitem has finished.
  263. *
  264. * Work items submitted to a queue with dispatch_sync() do not observe certain
  265. * queue attributes of that queue when invoked (such as autorelease frequency
  266. * and QOS class).
  267. *
  268. * Calls to dispatch_sync() targeting the current queue will result
  269. * in dead-lock. Use of dispatch_sync() is also subject to the same
  270. * multi-party dead-lock problems that may result from the use of a mutex.
  271. * Use of dispatch_async() is preferred.
  272. *
  273. * Unlike dispatch_async(), no retain is performed on the target queue. Because
  274. * calls to this function are synchronous, the dispatch_sync() "borrows" the
  275. * reference of the caller.
  276. *
  277. * As an optimization, dispatch_sync() invokes the workitem on the thread which
  278. * submitted the workitem, except when the passed queue is the main queue or
  279. * a queue targetting it (See dispatch_queue_main_t,
  280. * dispatch_set_target_queue()).
  281. *
  282. * @param queue
  283. * The target dispatch queue to which the block is submitted.
  284. * The result of passing NULL in this parameter is undefined.
  285. *
  286. * @param block
  287. * The block to be invoked on the target dispatch queue.
  288. * The result of passing NULL in this parameter is undefined.
  289. */
  290. #ifdef __BLOCKS__
  291. API_AVAILABLE(macos(10.6), ios(4.0))
  292. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  293. void
  294. dispatch_sync(dispatch_queue_t queue, DISPATCH_NOESCAPE dispatch_block_t block);
  295. #endif
  296. /*!
  297. * @function dispatch_sync_f
  298. *
  299. * @abstract
  300. * Submits a function for synchronous execution on a dispatch queue.
  301. *
  302. * @discussion
  303. * See dispatch_sync() for details.
  304. *
  305. * @param queue
  306. * The target dispatch queue to which the function is submitted.
  307. * The result of passing NULL in this parameter is undefined.
  308. *
  309. * @param context
  310. * The application-defined context parameter to pass to the function.
  311. *
  312. * @param work
  313. * The application-defined function to invoke on the target queue. The first
  314. * parameter passed to this function is the context provided to
  315. * dispatch_sync_f().
  316. * The result of passing NULL in this parameter is undefined.
  317. */
  318. API_AVAILABLE(macos(10.6), ios(4.0))
  319. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
  320. void
  321. dispatch_sync_f(dispatch_queue_t queue,
  322. void *_Nullable context, dispatch_function_t work);
  323. #if defined(__APPLE__) && \
  324. (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \
  325. __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0) || \
  326. (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
  327. __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_9)
  328. #define DISPATCH_APPLY_AUTO_AVAILABLE 0
  329. #define DISPATCH_APPLY_QUEUE_ARG_NULLABILITY _Nonnull
  330. #else
  331. #define DISPATCH_APPLY_AUTO_AVAILABLE 1
  332. #define DISPATCH_APPLY_QUEUE_ARG_NULLABILITY _Nullable
  333. #endif
  334. /*!
  335. * @constant DISPATCH_APPLY_AUTO
  336. *
  337. * @abstract
  338. * Constant to pass to dispatch_apply() or dispatch_apply_f() to request that
  339. * the system automatically use worker threads that match the configuration of
  340. * the current thread as closely as possible.
  341. *
  342. * @discussion
  343. * When submitting a block for parallel invocation, passing this constant as the
  344. * queue argument will automatically use the global concurrent queue that
  345. * matches the Quality of Service of the caller most closely.
  346. *
  347. * No assumptions should be made about which global concurrent queue will
  348. * actually be used.
  349. *
  350. * Using this constant deploys backward to macOS 10.9, iOS 7.0 and any tvOS or
  351. * watchOS version.
  352. */
  353. #if DISPATCH_APPLY_AUTO_AVAILABLE
  354. #define DISPATCH_APPLY_AUTO ((dispatch_queue_t _Nonnull)0)
  355. #endif
  356. /*!
  357. * @function dispatch_apply
  358. *
  359. * @abstract
  360. * Submits a block to a dispatch queue for parallel invocation.
  361. *
  362. * @discussion
  363. * Submits a block to a dispatch queue for parallel invocation. This function
  364. * waits for the task block to complete before returning. If the specified queue
  365. * is concurrent, the block may be invoked concurrently, and it must therefore
  366. * be reentrant safe.
  367. *
  368. * Each invocation of the block will be passed the current index of iteration.
  369. *
  370. * @param iterations
  371. * The number of iterations to perform.
  372. *
  373. * @param queue
  374. * The dispatch queue to which the block is submitted.
  375. * The preferred value to pass is DISPATCH_APPLY_AUTO to automatically use
  376. * a queue appropriate for the calling thread.
  377. *
  378. * @param block
  379. * The block to be invoked the specified number of iterations.
  380. * The result of passing NULL in this parameter is undefined.
  381. */
  382. #ifdef __BLOCKS__
  383. API_AVAILABLE(macos(10.6), ios(4.0))
  384. DISPATCH_EXPORT DISPATCH_NONNULL3 DISPATCH_NOTHROW
  385. void
  386. dispatch_apply(size_t iterations,
  387. dispatch_queue_t DISPATCH_APPLY_QUEUE_ARG_NULLABILITY queue,
  388. DISPATCH_NOESCAPE void (^block)(size_t));
  389. #endif
  390. /*!
  391. * @function dispatch_apply_f
  392. *
  393. * @abstract
  394. * Submits a function to a dispatch queue for parallel invocation.
  395. *
  396. * @discussion
  397. * See dispatch_apply() for details.
  398. *
  399. * @param iterations
  400. * The number of iterations to perform.
  401. *
  402. * @param queue
  403. * The dispatch queue to which the function is submitted.
  404. * The preferred value to pass is DISPATCH_APPLY_AUTO to automatically use
  405. * a queue appropriate for the calling thread.
  406. *
  407. * @param context
  408. * The application-defined context parameter to pass to the function.
  409. *
  410. * @param work
  411. * The application-defined function to invoke on the specified queue. The first
  412. * parameter passed to this function is the context provided to
  413. * dispatch_apply_f(). The second parameter passed to this function is the
  414. * current index of iteration.
  415. * The result of passing NULL in this parameter is undefined.
  416. */
  417. API_AVAILABLE(macos(10.6), ios(4.0))
  418. DISPATCH_EXPORT DISPATCH_NONNULL4 DISPATCH_NOTHROW
  419. void
  420. dispatch_apply_f(size_t iterations,
  421. dispatch_queue_t DISPATCH_APPLY_QUEUE_ARG_NULLABILITY queue,
  422. void *_Nullable context, void (*work)(void *_Nullable, size_t));
  423. /*!
  424. * @function dispatch_get_current_queue
  425. *
  426. * @abstract
  427. * Returns the queue on which the currently executing block is running.
  428. *
  429. * @discussion
  430. * Returns the queue on which the currently executing block is running.
  431. *
  432. * When dispatch_get_current_queue() is called outside of the context of a
  433. * submitted block, it will return the default concurrent queue.
  434. *
  435. * Recommended for debugging and logging purposes only:
  436. * The code must not make any assumptions about the queue returned, unless it
  437. * is one of the global queues or a queue the code has itself created.
  438. * The code must not assume that synchronous execution onto a queue is safe
  439. * from deadlock if that queue is not the one returned by
  440. * dispatch_get_current_queue().
  441. *
  442. * When dispatch_get_current_queue() is called on the main thread, it may
  443. * or may not return the same value as dispatch_get_main_queue(). Comparing
  444. * the two is not a valid way to test whether code is executing on the
  445. * main thread (see dispatch_assert_queue() and dispatch_assert_queue_not()).
  446. *
  447. * This function is deprecated and will be removed in a future release.
  448. *
  449. * @result
  450. * Returns the current queue.
  451. */
  452. API_DEPRECATED("unsupported interface", macos(10.6,10.9), ios(4.0,6.0))
  453. DISPATCH_EXPORT DISPATCH_PURE DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  454. dispatch_queue_t
  455. dispatch_get_current_queue(void);
  456. API_AVAILABLE(macos(10.6), ios(4.0))
  457. DISPATCH_EXPORT
  458. #if defined(__DISPATCH_BUILDING_DISPATCH__) && !defined(__OBJC__)
  459. struct dispatch_queue_static_s _dispatch_main_q;
  460. #else
  461. struct dispatch_queue_s _dispatch_main_q;
  462. #endif
  463. /*!
  464. * @function dispatch_get_main_queue
  465. *
  466. * @abstract
  467. * Returns the default queue that is bound to the main thread.
  468. *
  469. * @discussion
  470. * In order to invoke blocks submitted to the main queue, the application must
  471. * call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main
  472. * thread.
  473. *
  474. * The main queue is meant to be used in application context to interact with
  475. * the main thread and the main runloop.
  476. *
  477. * Because the main queue doesn't behave entirely like a regular serial queue,
  478. * it may have unwanted side-effects when used in processes that are not UI apps
  479. * (daemons). For such processes, the main queue should be avoided.
  480. *
  481. * @see dispatch_queue_main_t
  482. *
  483. * @result
  484. * Returns the main queue. This queue is created automatically on behalf of
  485. * the main thread before main() is called.
  486. */
  487. DISPATCH_INLINE DISPATCH_ALWAYS_INLINE DISPATCH_CONST DISPATCH_NOTHROW
  488. dispatch_queue_main_t
  489. dispatch_get_main_queue(void)
  490. {
  491. return DISPATCH_GLOBAL_OBJECT(dispatch_queue_main_t, _dispatch_main_q);
  492. }
  493. /*!
  494. * @typedef dispatch_queue_priority_t
  495. * Type of dispatch_queue_priority
  496. *
  497. * @constant DISPATCH_QUEUE_PRIORITY_HIGH
  498. * Items dispatched to the queue will run at high priority,
  499. * i.e. the queue will be scheduled for execution before
  500. * any default priority or low priority queue.
  501. *
  502. * @constant DISPATCH_QUEUE_PRIORITY_DEFAULT
  503. * Items dispatched to the queue will run at the default
  504. * priority, i.e. the queue will be scheduled for execution
  505. * after all high priority queues have been scheduled, but
  506. * before any low priority queues have been scheduled.
  507. *
  508. * @constant DISPATCH_QUEUE_PRIORITY_LOW
  509. * Items dispatched to the queue will run at low priority,
  510. * i.e. the queue will be scheduled for execution after all
  511. * default priority and high priority queues have been
  512. * scheduled.
  513. *
  514. * @constant DISPATCH_QUEUE_PRIORITY_BACKGROUND
  515. * Items dispatched to the queue will run at background priority, i.e. the queue
  516. * will be scheduled for execution after all higher priority queues have been
  517. * scheduled and the system will run items on this queue on a thread with
  518. * background status as per setpriority(2) (i.e. disk I/O is throttled and the
  519. * thread's scheduling priority is set to lowest value).
  520. */
  521. #define DISPATCH_QUEUE_PRIORITY_HIGH 2
  522. #define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
  523. #define DISPATCH_QUEUE_PRIORITY_LOW (-2)
  524. #define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
  525. typedef long dispatch_queue_priority_t;
  526. /*!
  527. * @typedef dispatch_qos_class_t
  528. * Alias for qos_class_t type.
  529. */
  530. #if __has_include(<sys/qos.h>)
  531. typedef qos_class_t dispatch_qos_class_t;
  532. #else
  533. typedef unsigned int dispatch_qos_class_t;
  534. #endif
  535. /*!
  536. * @function dispatch_get_global_queue
  537. *
  538. * @abstract
  539. * Returns a well-known global concurrent queue of a given quality of service
  540. * class.
  541. *
  542. * @discussion
  543. * See dispatch_queue_global_t.
  544. *
  545. * @param identifier
  546. * A quality of service class defined in qos_class_t or a priority defined in
  547. * dispatch_queue_priority_t.
  548. *
  549. * It is recommended to use quality of service class values to identify the
  550. * well-known global concurrent queues:
  551. * - QOS_CLASS_USER_INTERACTIVE
  552. * - QOS_CLASS_USER_INITIATED
  553. * - QOS_CLASS_DEFAULT
  554. * - QOS_CLASS_UTILITY
  555. * - QOS_CLASS_BACKGROUND
  556. *
  557. * The global concurrent queues may still be identified by their priority,
  558. * which map to the following QOS classes:
  559. * - DISPATCH_QUEUE_PRIORITY_HIGH: QOS_CLASS_USER_INITIATED
  560. * - DISPATCH_QUEUE_PRIORITY_DEFAULT: QOS_CLASS_DEFAULT
  561. * - DISPATCH_QUEUE_PRIORITY_LOW: QOS_CLASS_UTILITY
  562. * - DISPATCH_QUEUE_PRIORITY_BACKGROUND: QOS_CLASS_BACKGROUND
  563. *
  564. * @param flags
  565. * Reserved for future use. Passing any value other than zero may result in
  566. * a NULL return value.
  567. *
  568. * @result
  569. * Returns the requested global queue or NULL if the requested global queue
  570. * does not exist.
  571. */
  572. API_AVAILABLE(macos(10.6), ios(4.0))
  573. DISPATCH_EXPORT DISPATCH_CONST DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  574. dispatch_queue_global_t
  575. dispatch_get_global_queue(intptr_t identifier, uintptr_t flags);
  576. /*!
  577. * @typedef dispatch_queue_attr_t
  578. *
  579. * @abstract
  580. * Attribute for dispatch queues.
  581. */
  582. DISPATCH_DECL(dispatch_queue_attr);
  583. /*!
  584. * @const DISPATCH_QUEUE_SERIAL
  585. *
  586. * @discussion
  587. * An attribute that can be used to create a dispatch queue that invokes blocks
  588. * serially in FIFO order.
  589. *
  590. * See dispatch_queue_serial_t.
  591. */
  592. #define DISPATCH_QUEUE_SERIAL NULL
  593. /*!
  594. * @const DISPATCH_QUEUE_SERIAL_INACTIVE
  595. *
  596. * @discussion
  597. * An attribute that can be used to create a dispatch queue that invokes blocks
  598. * serially in FIFO order, and that is initially inactive.
  599. *
  600. * See dispatch_queue_attr_make_initially_inactive().
  601. */
  602. #define DISPATCH_QUEUE_SERIAL_INACTIVE \
  603. dispatch_queue_attr_make_initially_inactive(DISPATCH_QUEUE_SERIAL)
  604. /*!
  605. * @const DISPATCH_QUEUE_CONCURRENT
  606. *
  607. * @discussion
  608. * An attribute that can be used to create a dispatch queue that may invoke
  609. * blocks concurrently and supports barrier blocks submitted with the dispatch
  610. * barrier API.
  611. *
  612. * See dispatch_queue_concurrent_t.
  613. */
  614. #define DISPATCH_QUEUE_CONCURRENT \
  615. DISPATCH_GLOBAL_OBJECT(dispatch_queue_attr_t, \
  616. _dispatch_queue_attr_concurrent)
  617. API_AVAILABLE(macos(10.7), ios(4.3))
  618. DISPATCH_EXPORT
  619. struct dispatch_queue_attr_s _dispatch_queue_attr_concurrent;
  620. /*!
  621. * @const DISPATCH_QUEUE_CONCURRENT_INACTIVE
  622. *
  623. * @discussion
  624. * An attribute that can be used to create a dispatch queue that may invoke
  625. * blocks concurrently and supports barrier blocks submitted with the dispatch
  626. * barrier API, and that is initially inactive.
  627. *
  628. * See dispatch_queue_attr_make_initially_inactive().
  629. */
  630. #define DISPATCH_QUEUE_CONCURRENT_INACTIVE \
  631. dispatch_queue_attr_make_initially_inactive(DISPATCH_QUEUE_CONCURRENT)
  632. /*!
  633. * @function dispatch_queue_attr_make_initially_inactive
  634. *
  635. * @abstract
  636. * Returns an attribute value which may be provided to dispatch_queue_create()
  637. * or dispatch_queue_create_with_target(), in order to make the created queue
  638. * initially inactive.
  639. *
  640. * @discussion
  641. * Dispatch queues may be created in an inactive state. Queues in this state
  642. * have to be activated before any blocks associated with them will be invoked.
  643. *
  644. * A queue in inactive state cannot be deallocated, dispatch_activate() must be
  645. * called before the last reference to a queue created with this attribute is
  646. * released.
  647. *
  648. * The target queue of a queue in inactive state can be changed using
  649. * dispatch_set_target_queue(). Change of target queue is no longer permitted
  650. * once an initially inactive queue has been activated.
  651. *
  652. * @param attr
  653. * A queue attribute value to be combined with the initially inactive attribute.
  654. *
  655. * @return
  656. * Returns an attribute value which may be provided to dispatch_queue_create()
  657. * and dispatch_queue_create_with_target().
  658. * The new value combines the attributes specified by the 'attr' parameter with
  659. * the initially inactive attribute.
  660. */
  661. API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  662. DISPATCH_EXPORT DISPATCH_WARN_RESULT DISPATCH_PURE DISPATCH_NOTHROW
  663. dispatch_queue_attr_t
  664. dispatch_queue_attr_make_initially_inactive(
  665. dispatch_queue_attr_t _Nullable attr);
  666. /*!
  667. * @const DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL
  668. *
  669. * @discussion
  670. * A dispatch queue created with this attribute invokes blocks serially in FIFO
  671. * order, and surrounds execution of any block submitted asynchronously to it
  672. * with the equivalent of a individual Objective-C <code>@autoreleasepool</code>
  673. * scope.
  674. *
  675. * See dispatch_queue_attr_make_with_autorelease_frequency().
  676. */
  677. #define DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL \
  678. dispatch_queue_attr_make_with_autorelease_frequency(\
  679. DISPATCH_QUEUE_SERIAL, DISPATCH_AUTORELEASE_FREQUENCY_WORK_ITEM)
  680. /*!
  681. * @const DISPATCH_QUEUE_CONCURRENT_WITH_AUTORELEASE_POOL
  682. *
  683. * @discussion
  684. * A dispatch queue created with this attribute may invokes blocks concurrently
  685. * and supports barrier blocks submitted with the dispatch barrier API. It also
  686. * surrounds execution of any block submitted asynchronously to it with the
  687. * equivalent of a individual Objective-C <code>@autoreleasepool</code>
  688. *
  689. * See dispatch_queue_attr_make_with_autorelease_frequency().
  690. */
  691. #define DISPATCH_QUEUE_CONCURRENT_WITH_AUTORELEASE_POOL \
  692. dispatch_queue_attr_make_with_autorelease_frequency(\
  693. DISPATCH_QUEUE_CONCURRENT, DISPATCH_AUTORELEASE_FREQUENCY_WORK_ITEM)
  694. /*!
  695. * @typedef dispatch_autorelease_frequency_t
  696. * Values to pass to the dispatch_queue_attr_make_with_autorelease_frequency()
  697. * function.
  698. *
  699. * @const DISPATCH_AUTORELEASE_FREQUENCY_INHERIT
  700. * Dispatch queues with this autorelease frequency inherit the behavior from
  701. * their target queue. This is the default behavior for manually created queues.
  702. *
  703. * @const DISPATCH_AUTORELEASE_FREQUENCY_WORK_ITEM
  704. * Dispatch queues with this autorelease frequency push and pop an autorelease
  705. * pool around the execution of every block that was submitted to it
  706. * asynchronously.
  707. * @see dispatch_queue_attr_make_with_autorelease_frequency().
  708. *
  709. * @const DISPATCH_AUTORELEASE_FREQUENCY_NEVER
  710. * Dispatch queues with this autorelease frequency never set up an individual
  711. * autorelease pool around the execution of a block that is submitted to it
  712. * asynchronously. This is the behavior of the global concurrent queues.
  713. */
  714. DISPATCH_ENUM(dispatch_autorelease_frequency, unsigned long,
  715. DISPATCH_AUTORELEASE_FREQUENCY_INHERIT DISPATCH_ENUM_API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0)) = 0,
  716. DISPATCH_AUTORELEASE_FREQUENCY_WORK_ITEM DISPATCH_ENUM_API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0)) = 1,
  717. DISPATCH_AUTORELEASE_FREQUENCY_NEVER DISPATCH_ENUM_API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0)) = 2,
  718. );
  719. /*!
  720. * @function dispatch_queue_attr_make_with_autorelease_frequency
  721. *
  722. * @abstract
  723. * Returns a dispatch queue attribute value with the autorelease frequency
  724. * set to the specified value.
  725. *
  726. * @discussion
  727. * When a queue uses the per-workitem autorelease frequency (either directly
  728. * or inherithed from its target queue), any block submitted asynchronously to
  729. * this queue (via dispatch_async(), dispatch_barrier_async(),
  730. * dispatch_group_notify(), etc...) is executed as if surrounded by a individual
  731. * Objective-C <code>@autoreleasepool</code> scope.
  732. *
  733. * Autorelease frequency has no effect on blocks that are submitted
  734. * synchronously to a queue (via dispatch_sync(), dispatch_barrier_sync()).
  735. *
  736. * The global concurrent queues have the DISPATCH_AUTORELEASE_FREQUENCY_NEVER
  737. * behavior. Manually created dispatch queues use
  738. * DISPATCH_AUTORELEASE_FREQUENCY_INHERIT by default.
  739. *
  740. * Queues created with this attribute cannot change target queues after having
  741. * been activated. See dispatch_set_target_queue() and dispatch_activate().
  742. *
  743. * @param attr
  744. * A queue attribute value to be combined with the specified autorelease
  745. * frequency or NULL.
  746. *
  747. * @param frequency
  748. * The requested autorelease frequency.
  749. *
  750. * @return
  751. * Returns an attribute value which may be provided to dispatch_queue_create()
  752. * or NULL if an invalid autorelease frequency was requested.
  753. * This new value combines the attributes specified by the 'attr' parameter and
  754. * the chosen autorelease frequency.
  755. */
  756. API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  757. DISPATCH_EXPORT DISPATCH_WARN_RESULT DISPATCH_PURE DISPATCH_NOTHROW
  758. dispatch_queue_attr_t
  759. dispatch_queue_attr_make_with_autorelease_frequency(
  760. dispatch_queue_attr_t _Nullable attr,
  761. dispatch_autorelease_frequency_t frequency);
  762. /*!
  763. * @function dispatch_queue_attr_make_with_qos_class
  764. *
  765. * @abstract
  766. * Returns an attribute value which may be provided to dispatch_queue_create()
  767. * or dispatch_queue_create_with_target(), in order to assign a QOS class and
  768. * relative priority to the queue.
  769. *
  770. * @discussion
  771. * When specified in this manner, the QOS class and relative priority take
  772. * precedence over those inherited from the dispatch queue's target queue (if
  773. * any) as long that does not result in a lower QOS class and relative priority.
  774. *
  775. * The global queue priorities map to the following QOS classes:
  776. * - DISPATCH_QUEUE_PRIORITY_HIGH: QOS_CLASS_USER_INITIATED
  777. * - DISPATCH_QUEUE_PRIORITY_DEFAULT: QOS_CLASS_DEFAULT
  778. * - DISPATCH_QUEUE_PRIORITY_LOW: QOS_CLASS_UTILITY
  779. * - DISPATCH_QUEUE_PRIORITY_BACKGROUND: QOS_CLASS_BACKGROUND
  780. *
  781. * Example:
  782. * <code>
  783. * dispatch_queue_t queue;
  784. * dispatch_queue_attr_t attr;
  785. * attr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL,
  786. * QOS_CLASS_UTILITY, 0);
  787. * queue = dispatch_queue_create("com.example.myqueue", attr);
  788. * </code>
  789. *
  790. * The QOS class and relative priority set this way on a queue have no effect on
  791. * blocks that are submitted synchronously to a queue (via dispatch_sync(),
  792. * dispatch_barrier_sync()).
  793. *
  794. * @param attr
  795. * A queue attribute value to be combined with the QOS class, or NULL.
  796. *
  797. * @param qos_class
  798. * A QOS class value:
  799. * - QOS_CLASS_USER_INTERACTIVE
  800. * - QOS_CLASS_USER_INITIATED
  801. * - QOS_CLASS_DEFAULT
  802. * - QOS_CLASS_UTILITY
  803. * - QOS_CLASS_BACKGROUND
  804. * Passing any other value results in NULL being returned.
  805. *
  806. * @param relative_priority
  807. * A relative priority within the QOS class. This value is a negative
  808. * offset from the maximum supported scheduler priority for the given class.
  809. * Passing a value greater than zero or less than QOS_MIN_RELATIVE_PRIORITY
  810. * results in NULL being returned.
  811. *
  812. * @return
  813. * Returns an attribute value which may be provided to dispatch_queue_create()
  814. * and dispatch_queue_create_with_target(), or NULL if an invalid QOS class was
  815. * requested.
  816. * The new value combines the attributes specified by the 'attr' parameter and
  817. * the new QOS class and relative priority.
  818. */
  819. API_AVAILABLE(macos(10.10), ios(8.0))
  820. DISPATCH_EXPORT DISPATCH_WARN_RESULT DISPATCH_PURE DISPATCH_NOTHROW
  821. dispatch_queue_attr_t
  822. dispatch_queue_attr_make_with_qos_class(dispatch_queue_attr_t _Nullable attr,
  823. dispatch_qos_class_t qos_class, int relative_priority);
  824. /*!
  825. * @const DISPATCH_TARGET_QUEUE_DEFAULT
  826. * @discussion Constant to pass to the dispatch_queue_create_with_target(),
  827. * dispatch_set_target_queue() and dispatch_source_create() functions to
  828. * indicate that the default target queue for the object type in question
  829. * should be used.
  830. */
  831. #define DISPATCH_TARGET_QUEUE_DEFAULT NULL
  832. /*!
  833. * @function dispatch_queue_create_with_target
  834. *
  835. * @abstract
  836. * Creates a new dispatch queue with a specified target queue.
  837. *
  838. * @discussion
  839. * Dispatch queues created with the DISPATCH_QUEUE_SERIAL or a NULL attribute
  840. * invoke blocks serially in FIFO order.
  841. *
  842. * Dispatch queues created with the DISPATCH_QUEUE_CONCURRENT attribute may
  843. * invoke blocks concurrently (similarly to the global concurrent queues, but
  844. * potentially with more overhead), and support barrier blocks submitted with
  845. * the dispatch barrier API, which e.g. enables the implementation of efficient
  846. * reader-writer schemes.
  847. *
  848. * When a dispatch queue is no longer needed, it should be released with
  849. * dispatch_release(). Note that any pending blocks submitted asynchronously to
  850. * a queue will hold a reference to that queue. Therefore a queue will not be
  851. * deallocated until all pending blocks have finished.
  852. *
  853. * When using a dispatch queue attribute @a attr specifying a QoS class (derived
  854. * from the result of dispatch_queue_attr_make_with_qos_class()), passing the
  855. * result of dispatch_get_global_queue() in @a target will ignore the QoS class
  856. * of that global queue and will use the global queue with the QoS class
  857. * specified by attr instead.
  858. *
  859. * Queues created with dispatch_queue_create_with_target() cannot have their
  860. * target queue changed, unless created inactive (See
  861. * dispatch_queue_attr_make_initially_inactive()), in which case the target
  862. * queue can be changed until the newly created queue is activated with
  863. * dispatch_activate().
  864. *
  865. * @param label
  866. * A string label to attach to the queue.
  867. * This parameter is optional and may be NULL.
  868. *
  869. * @param attr
  870. * A predefined attribute such as DISPATCH_QUEUE_SERIAL,
  871. * DISPATCH_QUEUE_CONCURRENT, or the result of a call to
  872. * a dispatch_queue_attr_make_with_* function.
  873. *
  874. * @param target
  875. * The target queue for the newly created queue. The target queue is retained.
  876. * If this parameter is DISPATCH_TARGET_QUEUE_DEFAULT, sets the queue's target
  877. * queue to the default target queue for the given queue type.
  878. *
  879. * @result
  880. * The newly created dispatch queue.
  881. */
  882. API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  883. DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
  884. DISPATCH_NOTHROW
  885. dispatch_queue_t
  886. dispatch_queue_create_with_target(const char *_Nullable label,
  887. dispatch_queue_attr_t _Nullable attr, dispatch_queue_t _Nullable target)
  888. DISPATCH_ALIAS_V2(dispatch_queue_create_with_target);
  889. /*!
  890. * @function dispatch_queue_create
  891. *
  892. * @abstract
  893. * Creates a new dispatch queue to which blocks may be submitted.
  894. *
  895. * @discussion
  896. * Dispatch queues created with the DISPATCH_QUEUE_SERIAL or a NULL attribute
  897. * invoke blocks serially in FIFO order.
  898. *
  899. * Dispatch queues created with the DISPATCH_QUEUE_CONCURRENT attribute may
  900. * invoke blocks concurrently (similarly to the global concurrent queues, but
  901. * potentially with more overhead), and support barrier blocks submitted with
  902. * the dispatch barrier API, which e.g. enables the implementation of efficient
  903. * reader-writer schemes.
  904. *
  905. * When a dispatch queue is no longer needed, it should be released with
  906. * dispatch_release(). Note that any pending blocks submitted asynchronously to
  907. * a queue will hold a reference to that queue. Therefore a queue will not be
  908. * deallocated until all pending blocks have finished.
  909. *
  910. * Passing the result of the dispatch_queue_attr_make_with_qos_class() function
  911. * to the attr parameter of this function allows a quality of service class and
  912. * relative priority to be specified for the newly created queue.
  913. * The quality of service class so specified takes precedence over the quality
  914. * of service class of the newly created dispatch queue's target queue (if any)
  915. * as long that does not result in a lower QOS class and relative priority.
  916. *
  917. * When no quality of service class is specified, the target queue of a newly
  918. * created dispatch queue is the default priority global concurrent queue.
  919. *
  920. * @param label
  921. * A string label to attach to the queue.
  922. * This parameter is optional and may be NULL.
  923. *
  924. * @param attr
  925. * A predefined attribute such as DISPATCH_QUEUE_SERIAL,
  926. * DISPATCH_QUEUE_CONCURRENT, or the result of a call to
  927. * a dispatch_queue_attr_make_with_* function.
  928. *
  929. * @result
  930. * The newly created dispatch queue.
  931. */
  932. API_AVAILABLE(macos(10.6), ios(4.0))
  933. DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
  934. DISPATCH_NOTHROW
  935. dispatch_queue_t
  936. dispatch_queue_create(const char *_Nullable label,
  937. dispatch_queue_attr_t _Nullable attr);
  938. /*!
  939. * @const DISPATCH_CURRENT_QUEUE_LABEL
  940. * @discussion Constant to pass to the dispatch_queue_get_label() function to
  941. * retrieve the label of the current queue.
  942. */
  943. #define DISPATCH_CURRENT_QUEUE_LABEL NULL
  944. /*!
  945. * @function dispatch_queue_get_label
  946. *
  947. * @abstract
  948. * Returns the label of the given queue, as specified when the queue was
  949. * created, or the empty string if a NULL label was specified.
  950. *
  951. * Passing DISPATCH_CURRENT_QUEUE_LABEL will return the label of the current
  952. * queue.
  953. *
  954. * @param queue
  955. * The queue to query, or DISPATCH_CURRENT_QUEUE_LABEL.
  956. *
  957. * @result
  958. * The label of the queue.
  959. */
  960. API_AVAILABLE(macos(10.6), ios(4.0))
  961. DISPATCH_EXPORT DISPATCH_PURE DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  962. const char *
  963. dispatch_queue_get_label(dispatch_queue_t _Nullable queue);
  964. /*!
  965. * @function dispatch_queue_get_qos_class
  966. *
  967. * @abstract
  968. * Returns the QOS class and relative priority of the given queue.
  969. *
  970. * @discussion
  971. * If the given queue was created with an attribute value returned from
  972. * dispatch_queue_attr_make_with_qos_class(), this function returns the QOS
  973. * class and relative priority specified at that time; for any other attribute
  974. * value it returns a QOS class of QOS_CLASS_UNSPECIFIED and a relative
  975. * priority of 0.
  976. *
  977. * If the given queue is one of the global queues, this function returns its
  978. * assigned QOS class value as documented under dispatch_get_global_queue() and
  979. * a relative priority of 0; in the case of the main queue it returns the QOS
  980. * value provided by qos_class_main() and a relative priority of 0.
  981. *
  982. * @param queue
  983. * The queue to query.
  984. *
  985. * @param relative_priority_ptr
  986. * A pointer to an int variable to be filled with the relative priority offset
  987. * within the QOS class, or NULL.
  988. *
  989. * @return
  990. * A QOS class value:
  991. * - QOS_CLASS_USER_INTERACTIVE
  992. * - QOS_CLASS_USER_INITIATED
  993. * - QOS_CLASS_DEFAULT
  994. * - QOS_CLASS_UTILITY
  995. * - QOS_CLASS_BACKGROUND
  996. * - QOS_CLASS_UNSPECIFIED
  997. */
  998. API_AVAILABLE(macos(10.10), ios(8.0))
  999. DISPATCH_EXPORT DISPATCH_WARN_RESULT DISPATCH_NONNULL1 DISPATCH_NOTHROW
  1000. dispatch_qos_class_t
  1001. dispatch_queue_get_qos_class(dispatch_queue_t queue,
  1002. int *_Nullable relative_priority_ptr);
  1003. /*!
  1004. * @function dispatch_set_target_queue
  1005. *
  1006. * @abstract
  1007. * Sets the target queue for the given object.
  1008. *
  1009. * @discussion
  1010. * An object's target queue is responsible for processing the object.
  1011. *
  1012. * When no quality of service class and relative priority is specified for a
  1013. * dispatch queue at the time of creation, a dispatch queue's quality of service
  1014. * class is inherited from its target queue. The dispatch_get_global_queue()
  1015. * function may be used to obtain a target queue of a specific quality of
  1016. * service class, however the use of dispatch_queue_attr_make_with_qos_class()
  1017. * is recommended instead.
  1018. *
  1019. * Blocks submitted to a serial queue whose target queue is another serial
  1020. * queue will not be invoked concurrently with blocks submitted to the target
  1021. * queue or to any other queue with that same target queue.
  1022. *
  1023. * The result of introducing a cycle into the hierarchy of target queues is
  1024. * undefined.
  1025. *
  1026. * A dispatch source's target queue specifies where its event handler and
  1027. * cancellation handler blocks will be submitted.
  1028. *
  1029. * A dispatch I/O channel's target queue specifies where where its I/O
  1030. * operations are executed. If the channel's target queue's priority is set to
  1031. * DISPATCH_QUEUE_PRIORITY_BACKGROUND, then the I/O operations performed by
  1032. * dispatch_io_read() or dispatch_io_write() on that queue will be
  1033. * throttled when there is I/O contention.
  1034. *
  1035. * For all other dispatch object types, the only function of the target queue
  1036. * is to determine where an object's finalizer function is invoked.
  1037. *
  1038. * In general, changing the target queue of an object is an asynchronous
  1039. * operation that doesn't take effect immediately, and doesn't affect blocks
  1040. * already associated with the specified object.
  1041. *
  1042. * However, if an object is inactive at the time dispatch_set_target_queue() is
  1043. * called, then the target queue change takes effect immediately, and will
  1044. * affect blocks already associated with the specified object. After an
  1045. * initially inactive object has been activated, calling
  1046. * dispatch_set_target_queue() results in an assertion and the process being
  1047. * terminated.
  1048. *
  1049. * If a dispatch queue is active and targeted by other dispatch objects,
  1050. * changing its target queue results in undefined behavior.
  1051. *
  1052. * @param object
  1053. * The object to modify.
  1054. * The result of passing NULL in this parameter is undefined.
  1055. *
  1056. * @param queue
  1057. * The new target queue for the object. The queue is retained, and the
  1058. * previous target queue, if any, is released.
  1059. * If queue is DISPATCH_TARGET_QUEUE_DEFAULT, set the object's target queue
  1060. * to the default target queue for the given object type.
  1061. */
  1062. API_AVAILABLE(macos(10.6), ios(4.0))
  1063. DISPATCH_EXPORT DISPATCH_NOTHROW
  1064. void
  1065. dispatch_set_target_queue(dispatch_object_t object,
  1066. dispatch_queue_t _Nullable queue);
  1067. /*!
  1068. * @function dispatch_main
  1069. *
  1070. * @abstract
  1071. * Execute blocks submitted to the main queue.
  1072. *
  1073. * @discussion
  1074. * This function "parks" the main thread and waits for blocks to be submitted
  1075. * to the main queue. This function never returns.
  1076. *
  1077. * Applications that call NSApplicationMain() or CFRunLoopRun() on the
  1078. * main thread do not need to call dispatch_main().
  1079. */
  1080. API_AVAILABLE(macos(10.6), ios(4.0))
  1081. DISPATCH_EXPORT DISPATCH_NOTHROW DISPATCH_NORETURN
  1082. void
  1083. dispatch_main(void);
  1084. /*!
  1085. * @function dispatch_after
  1086. *
  1087. * @abstract
  1088. * Schedule a block for execution on a given queue at a specified time.
  1089. *
  1090. * @discussion
  1091. * Passing DISPATCH_TIME_NOW as the "when" parameter is supported, but not as
  1092. * optimal as calling dispatch_async() instead. Passing DISPATCH_TIME_FOREVER
  1093. * is undefined.
  1094. *
  1095. * @param when
  1096. * A temporal milestone returned by dispatch_time() or dispatch_walltime().
  1097. *
  1098. * @param queue
  1099. * A queue to which the given block will be submitted at the specified time.
  1100. * The result of passing NULL in this parameter is undefined.
  1101. *
  1102. * @param block
  1103. * The block of code to execute.
  1104. * The result of passing NULL in this parameter is undefined.
  1105. */
  1106. #ifdef __BLOCKS__
  1107. API_AVAILABLE(macos(10.6), ios(4.0))
  1108. DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NONNULL3 DISPATCH_NOTHROW
  1109. void
  1110. dispatch_after(dispatch_time_t when, dispatch_queue_t queue,
  1111. dispatch_block_t block);
  1112. #endif
  1113. /*!
  1114. * @function dispatch_after_f
  1115. *
  1116. * @abstract
  1117. * Schedule a function for execution on a given queue at a specified time.
  1118. *
  1119. * @discussion
  1120. * See dispatch_after() for details.
  1121. *
  1122. * @param when
  1123. * A temporal milestone returned by dispatch_time() or dispatch_walltime().
  1124. *
  1125. * @param queue
  1126. * A queue to which the given function will be submitted at the specified time.
  1127. * The result of passing NULL in this parameter is undefined.
  1128. *
  1129. * @param context
  1130. * The application-defined context parameter to pass to the function.
  1131. *
  1132. * @param work
  1133. * The application-defined function to invoke on the target queue. The first
  1134. * parameter passed to this function is the context provided to
  1135. * dispatch_after_f().
  1136. * The result of passing NULL in this parameter is undefined.
  1137. */
  1138. API_AVAILABLE(macos(10.6), ios(4.0))
  1139. DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NONNULL4 DISPATCH_NOTHROW
  1140. void
  1141. dispatch_after_f(dispatch_time_t when, dispatch_queue_t queue,
  1142. void *_Nullable context, dispatch_function_t work);
  1143. /*!
  1144. * @functiongroup Dispatch Barrier API
  1145. * The dispatch barrier API is a mechanism for submitting barrier blocks to a
  1146. * dispatch queue, analogous to the dispatch_async()/dispatch_sync() API.
  1147. * It enables the implementation of efficient reader/writer schemes.
  1148. * Barrier blocks only behave specially when submitted to queues created with
  1149. * the DISPATCH_QUEUE_CONCURRENT attribute; on such a queue, a barrier block
  1150. * will not run until all blocks submitted to the queue earlier have completed,
  1151. * and any blocks submitted to the queue after a barrier block will not run
  1152. * until the barrier block has completed.
  1153. * When submitted to a a global queue or to a queue not created with the
  1154. * DISPATCH_QUEUE_CONCURRENT attribute, barrier blocks behave identically to
  1155. * blocks submitted with the dispatch_async()/dispatch_sync() API.
  1156. */
  1157. /*!
  1158. * @function dispatch_barrier_async
  1159. *
  1160. * @abstract
  1161. * Submits a barrier block for asynchronous execution on a dispatch queue.
  1162. *
  1163. * @discussion
  1164. * Submits a block to a dispatch queue like dispatch_async(), but marks that
  1165. * block as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
  1166. *
  1167. * See dispatch_async() for details.
  1168. *
  1169. * @param queue
  1170. * The target dispatch queue to which the block is submitted.
  1171. * The system will hold a reference on the target queue until the block
  1172. * has finished.
  1173. * The result of passing NULL in this parameter is undefined.
  1174. *
  1175. * @param block
  1176. * The block to submit to the target dispatch queue. This function performs
  1177. * Block_copy() and Block_release() on behalf of callers.
  1178. * The result of passing NULL in this parameter is undefined.
  1179. */
  1180. #ifdef __BLOCKS__
  1181. API_AVAILABLE(macos(10.7), ios(4.3))
  1182. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  1183. void
  1184. dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);
  1185. #endif
  1186. /*!
  1187. * @function dispatch_barrier_async_f
  1188. *
  1189. * @abstract
  1190. * Submits a barrier function for asynchronous execution on a dispatch queue.
  1191. *
  1192. * @discussion
  1193. * Submits a function to a dispatch queue like dispatch_async_f(), but marks
  1194. * that function as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT
  1195. * queues).
  1196. *
  1197. * See dispatch_async_f() for details.
  1198. *
  1199. * @param queue
  1200. * The target dispatch queue to which the function is submitted.
  1201. * The system will hold a reference on the target queue until the function
  1202. * has returned.
  1203. * The result of passing NULL in this parameter is undefined.
  1204. *
  1205. * @param context
  1206. * The application-defined context parameter to pass to the function.
  1207. *
  1208. * @param work
  1209. * The application-defined function to invoke on the target queue. The first
  1210. * parameter passed to this function is the context provided to
  1211. * dispatch_barrier_async_f().
  1212. * The result of passing NULL in this parameter is undefined.
  1213. */
  1214. API_AVAILABLE(macos(10.7), ios(4.3))
  1215. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
  1216. void
  1217. dispatch_barrier_async_f(dispatch_queue_t queue,
  1218. void *_Nullable context, dispatch_function_t work);
  1219. /*!
  1220. * @function dispatch_barrier_sync
  1221. *
  1222. * @abstract
  1223. * Submits a barrier block for synchronous execution on a dispatch queue.
  1224. *
  1225. * @discussion
  1226. * Submits a block to a dispatch queue like dispatch_sync(), but marks that
  1227. * block as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
  1228. *
  1229. * See dispatch_sync() for details.
  1230. *
  1231. * @param queue
  1232. * The target dispatch queue to which the block is submitted.
  1233. * The result of passing NULL in this parameter is undefined.
  1234. *
  1235. * @param block
  1236. * The block to be invoked on the target dispatch queue.
  1237. * The result of passing NULL in this parameter is undefined.
  1238. */
  1239. #ifdef __BLOCKS__
  1240. API_AVAILABLE(macos(10.7), ios(4.3))
  1241. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  1242. void
  1243. dispatch_barrier_sync(dispatch_queue_t queue,
  1244. DISPATCH_NOESCAPE dispatch_block_t block);
  1245. #endif
  1246. /*!
  1247. * @function dispatch_barrier_sync_f
  1248. *
  1249. * @abstract
  1250. * Submits a barrier function for synchronous execution on a dispatch queue.
  1251. *
  1252. * @discussion
  1253. * Submits a function to a dispatch queue like dispatch_sync_f(), but marks that
  1254. * fuction as a barrier (relevant only on DISPATCH_QUEUE_CONCURRENT queues).
  1255. *
  1256. * See dispatch_sync_f() for details.
  1257. *
  1258. * @param queue
  1259. * The target dispatch queue to which the function is submitted.
  1260. * The result of passing NULL in this parameter is undefined.
  1261. *
  1262. * @param context
  1263. * The application-defined context parameter to pass to the function.
  1264. *
  1265. * @param work
  1266. * The application-defined function to invoke on the target queue. The first
  1267. * parameter passed to this function is the context provided to
  1268. * dispatch_barrier_sync_f().
  1269. * The result of passing NULL in this parameter is undefined.
  1270. */
  1271. API_AVAILABLE(macos(10.7), ios(4.3))
  1272. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
  1273. void
  1274. dispatch_barrier_sync_f(dispatch_queue_t queue,
  1275. void *_Nullable context, dispatch_function_t work);
  1276. /*!
  1277. * @functiongroup Dispatch queue-specific contexts
  1278. * This API allows different subsystems to associate context to a shared queue
  1279. * without risk of collision and to retrieve that context from blocks executing
  1280. * on that queue or any of its child queues in the target queue hierarchy.
  1281. */
  1282. /*!
  1283. * @function dispatch_queue_set_specific
  1284. *
  1285. * @abstract
  1286. * Associates a subsystem-specific context with a dispatch queue, for a key
  1287. * unique to the subsystem.
  1288. *
  1289. * @discussion
  1290. * The specified destructor will be invoked with the context on the default
  1291. * priority global concurrent queue when a new context is set for the same key,
  1292. * or after all references to the queue have been released.
  1293. *
  1294. * @param queue
  1295. * The dispatch queue to modify.
  1296. * The result of passing NULL in this parameter is undefined.
  1297. *
  1298. * @param key
  1299. * The key to set the context for, typically a pointer to a static variable
  1300. * specific to the subsystem. Keys are only compared as pointers and never
  1301. * dereferenced. Passing a string constant directly is not recommended.
  1302. * The NULL key is reserved and attempts to set a context for it are ignored.
  1303. *
  1304. * @param context
  1305. * The new subsystem-specific context for the object. This may be NULL.
  1306. *
  1307. * @param destructor
  1308. * The destructor function pointer. This may be NULL and is ignored if context
  1309. * is NULL.
  1310. */
  1311. API_AVAILABLE(macos(10.7), ios(5.0))
  1312. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
  1313. void
  1314. dispatch_queue_set_specific(dispatch_queue_t queue, const void *key,
  1315. void *_Nullable context, dispatch_function_t _Nullable destructor);
  1316. /*!
  1317. * @function dispatch_queue_get_specific
  1318. *
  1319. * @abstract
  1320. * Returns the subsystem-specific context associated with a dispatch queue, for
  1321. * a key unique to the subsystem.
  1322. *
  1323. * @discussion
  1324. * Returns the context for the specified key if it has been set on the specified
  1325. * queue.
  1326. *
  1327. * @param queue
  1328. * The dispatch queue to query.
  1329. * The result of passing NULL in this parameter is undefined.
  1330. *
  1331. * @param key
  1332. * The key to get the context for, typically a pointer to a static variable
  1333. * specific to the subsystem. Keys are only compared as pointers and never
  1334. * dereferenced. Passing a string constant directly is not recommended.
  1335. *
  1336. * @result
  1337. * The context for the specified key or NULL if no context was found.
  1338. */
  1339. API_AVAILABLE(macos(10.7), ios(5.0))
  1340. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_PURE DISPATCH_WARN_RESULT
  1341. DISPATCH_NOTHROW
  1342. void *_Nullable
  1343. dispatch_queue_get_specific(dispatch_queue_t queue, const void *key);
  1344. /*!
  1345. * @function dispatch_get_specific
  1346. *
  1347. * @abstract
  1348. * Returns the current subsystem-specific context for a key unique to the
  1349. * subsystem.
  1350. *
  1351. * @discussion
  1352. * When called from a block executing on a queue, returns the context for the
  1353. * specified key if it has been set on the queue, otherwise returns the result
  1354. * of dispatch_get_specific() executed on the queue's target queue or NULL
  1355. * if the current queue is a global concurrent queue.
  1356. *
  1357. * @param key
  1358. * The key to get the context for, typically a pointer to a static variable
  1359. * specific to the subsystem. Keys are only compared as pointers and never
  1360. * dereferenced. Passing a string constant directly is not recommended.
  1361. *
  1362. * @result
  1363. * The context for the specified key or NULL if no context was found.
  1364. */
  1365. API_AVAILABLE(macos(10.7), ios(5.0))
  1366. DISPATCH_EXPORT DISPATCH_PURE DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  1367. void *_Nullable
  1368. dispatch_get_specific(const void *key);
  1369. /*!
  1370. * @functiongroup Dispatch assertion API
  1371. *
  1372. * This API asserts at runtime that code is executing in (or out of) the context
  1373. * of a given queue. It can be used to check that a block accessing a resource
  1374. * does so from the proper queue protecting the resource. It also can be used
  1375. * to verify that a block that could cause a deadlock if run on a given queue
  1376. * never executes on that queue.
  1377. */
  1378. /*!
  1379. * @function dispatch_assert_queue
  1380. *
  1381. * @abstract
  1382. * Verifies that the current block is executing on a given dispatch queue.
  1383. *
  1384. * @discussion
  1385. * Some code expects to be run on a specific dispatch queue. This function
  1386. * verifies that that expectation is true.
  1387. *
  1388. * If the currently executing block was submitted to the specified queue or to
  1389. * any queue targeting it (see dispatch_set_target_queue()), this function
  1390. * returns.
  1391. *
  1392. * If the currently executing block was submitted with a synchronous API
  1393. * (dispatch_sync(), dispatch_barrier_sync(), ...), the context of the
  1394. * submitting block is also evaluated (recursively).
  1395. * If a synchronously submitting block is found that was itself submitted to
  1396. * the specified queue or to any queue targeting it, this function returns.
  1397. *
  1398. * Otherwise this function asserts: it logs an explanation to the system log and
  1399. * terminates the application.
  1400. *
  1401. * Passing the result of dispatch_get_main_queue() to this function verifies
  1402. * that the current block was submitted to the main queue, or to a queue
  1403. * targeting it, or is running on the main thread (in any context).
  1404. *
  1405. * When dispatch_assert_queue() is called outside of the context of a
  1406. * submitted block (for example from the context of a thread created manually
  1407. * with pthread_create()) then this function will also assert and terminate
  1408. * the application.
  1409. *
  1410. * The variant dispatch_assert_queue_debug() is compiled out when the
  1411. * preprocessor macro NDEBUG is defined. (See also assert(3)).
  1412. *
  1413. * @param queue
  1414. * The dispatch queue that the current block is expected to run on.
  1415. * The result of passing NULL in this parameter is undefined.
  1416. */
  1417. API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  1418. DISPATCH_EXPORT DISPATCH_NONNULL1
  1419. void
  1420. dispatch_assert_queue(dispatch_queue_t queue)
  1421. DISPATCH_ALIAS_V2(dispatch_assert_queue);
  1422. /*!
  1423. * @function dispatch_assert_queue_barrier
  1424. *
  1425. * @abstract
  1426. * Verifies that the current block is executing on a given dispatch queue,
  1427. * and that the block acts as a barrier on that queue.
  1428. *
  1429. * @discussion
  1430. * This behaves exactly like dispatch_assert_queue(), with the additional check
  1431. * that the current block acts as a barrier on the specified queue, which is
  1432. * always true if the specified queue is serial (see DISPATCH_BLOCK_BARRIER or
  1433. * dispatch_barrier_async() for details).
  1434. *
  1435. * The variant dispatch_assert_queue_barrier_debug() is compiled out when the
  1436. * preprocessor macro NDEBUG is defined. (See also assert()).
  1437. *
  1438. * @param queue
  1439. * The dispatch queue that the current block is expected to run as a barrier on.
  1440. * The result of passing NULL in this parameter is undefined.
  1441. */
  1442. API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  1443. DISPATCH_EXPORT DISPATCH_NONNULL1
  1444. void
  1445. dispatch_assert_queue_barrier(dispatch_queue_t queue);
  1446. /*!
  1447. * @function dispatch_assert_queue_not
  1448. *
  1449. * @abstract
  1450. * Verifies that the current block is not executing on a given dispatch queue.
  1451. *
  1452. * @discussion
  1453. * This function is the equivalent of dispatch_queue_assert() with the test for
  1454. * equality inverted. That means that it will terminate the application when
  1455. * dispatch_queue_assert() would return, and vice-versa. See discussion there.
  1456. *
  1457. * The variant dispatch_assert_queue_not_debug() is compiled out when the
  1458. * preprocessor macro NDEBUG is defined. (See also assert(3)).
  1459. *
  1460. * @param queue
  1461. * The dispatch queue that the current block is expected not to run on.
  1462. * The result of passing NULL in this parameter is undefined.
  1463. */
  1464. API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  1465. DISPATCH_EXPORT DISPATCH_NONNULL1
  1466. void
  1467. dispatch_assert_queue_not(dispatch_queue_t queue)
  1468. DISPATCH_ALIAS_V2(dispatch_assert_queue_not);
  1469. #ifdef NDEBUG
  1470. #define dispatch_assert_queue_debug(q) ((void)(0 && (q)))
  1471. #define dispatch_assert_queue_barrier_debug(q) ((void)(0 && (q)))
  1472. #define dispatch_assert_queue_not_debug(q) ((void)(0 && (q)))
  1473. #else
  1474. #define dispatch_assert_queue_debug(q) dispatch_assert_queue(q)
  1475. #define dispatch_assert_queue_barrier_debug(q) dispatch_assert_queue_barrier(q)
  1476. #define dispatch_assert_queue_not_debug(q) dispatch_assert_queue_not(q)
  1477. #endif
  1478. __END_DECLS
  1479. DISPATCH_ASSUME_NONNULL_END
  1480. #endif