mach_private.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*
  2. * Copyright (c) 2012-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_MACH_PRIVATE__
  26. #define __DISPATCH_MACH_PRIVATE__
  27. #ifndef __DISPATCH_INDIRECT__
  28. #error "Please #include <dispatch/dispatch.h> instead of this file directly."
  29. #include <dispatch/base.h> // for HeaderDoc
  30. #endif
  31. __BEGIN_DECLS
  32. #if DISPATCH_MACH_SPI
  33. #define DISPATCH_MACH_SPI_VERSION 20161026
  34. #include <mach/mach.h>
  35. DISPATCH_ASSUME_NONNULL_BEGIN
  36. /*!
  37. * @functiongroup Dispatch Mach Channel SPI
  38. *
  39. * IMPORTANT: This is Libsystem-internal SPI not intended for general use and
  40. * is subject to change at any time without warning.
  41. */
  42. /*!
  43. * @typedef dispatch_mach_t
  44. * A dispatch mach channel asynchronously recevives and sends mach messages.
  45. */
  46. DISPATCH_DECL(dispatch_mach);
  47. /*!
  48. * @typedef dispatch_mach_reason_t
  49. * Reasons for a mach channel handler to be invoked, or the result of an
  50. * immediate send attempt.
  51. *
  52. * @const DISPATCH_MACH_CONNECTED
  53. * The channel has been connected. The first handler invocation on a channel
  54. * after calling dispatch_mach_connect() will have this reason.
  55. *
  56. * @const DISPATCH_MACH_MESSAGE_RECEIVED
  57. * A message was received, it is passed in the message parameter.
  58. * It is the responsibility of the client of this API to handle this and consume
  59. * or dispose of the rights in the message (for example by calling
  60. * mach_msg_destroy()).
  61. *
  62. * @const DISPATCH_MACH_MESSAGE_SENT
  63. * A message was sent, it is passed in the message parameter (so that associated
  64. * resources can be disposed of).
  65. *
  66. * @const DISPATCH_MACH_MESSAGE_SEND_FAILED
  67. * A message failed to be sent, it is passed in the message parameter (so that
  68. * associated resources can be disposed of), along with the error code from
  69. * mach_msg().
  70. *
  71. * @const DISPATCH_MACH_MESSAGE_NOT_SENT
  72. * A message was not sent due to the channel being canceled or reconnected, it
  73. * is passed in the message parameter (so that associated resources can be
  74. * disposed of).
  75. *
  76. * @const DISPATCH_MACH_BARRIER_COMPLETED
  77. * A barrier block has finished executing.
  78. *
  79. * @const DISPATCH_MACH_DISCONNECTED
  80. * The channel has been disconnected by a call to dispatch_mach_reconnect() or
  81. * dispatch_mach_cancel(), an empty message is passed in the message parameter
  82. * (so that associated port rights can be disposed of).
  83. * The message header will contain either a remote port with a previously
  84. * connected send right, or a local port with a previously connected receive
  85. * right (if the channel was canceled), or a local port with a receive right
  86. * that was being monitored for a direct reply to a message previously sent to
  87. * the channel (if no reply was received).
  88. *
  89. * @const DISPATCH_MACH_CANCELED
  90. * The channel has been canceled.
  91. *
  92. * @const DISPATCH_MACH_REPLY_RECEIVED
  93. * A synchronous reply to a call to dispatch_mach_send_and_wait_for_reply() has
  94. * been received on another thread, an empty message is passed in the message
  95. * parameter (so that associated port rights can be disposed of).
  96. * The message header will contain a local port with a receive right associated
  97. * with the reply to the message that was synchronously sent to the channel.
  98. *
  99. * @const DISPATCH_MACH_NEEDS_DEFERRED_SEND
  100. * The message could not be sent synchronously. Only returned from a send with
  101. * result operation and never passed to a channel handler. Indicates that the
  102. * message passed to the send operation must not be disposed of until it is
  103. * returned via the channel handler.
  104. *
  105. * @const DISPATCH_MACH_SIGTERM_RECEIVED
  106. * A SIGTERM signal has been received. This notification is delivered at most
  107. * once during the lifetime of the channel. This event is sent only for XPC
  108. * channels (i.e. channels that were created by calling
  109. * dispatch_mach_create_4libxpc()) and only if the
  110. * dmxh_enable_sigterm_notification function in the XPC hooks structure returned
  111. * true when it was called at channel activation time.
  112. *
  113. * @const DISPATCH_MACH_ASYNC_WAITER_DISCONNECTED
  114. * The channel has been disconnected by a call to dispatch_mach_reconnect() or
  115. * dispatch_mach_cancel(), an empty message is passed in the message parameter
  116. * (so that associated port rights can be disposed of). The message header will
  117. * contain a local port with the receive right previously allocated to receive
  118. * an asynchronous reply to a message previously sent to the channel. Used
  119. * only if the channel is disconnected while waiting for a reply to a message
  120. * sent with dispatch_mach_send_with_result_and_async_reply_4libxpc().
  121. */
  122. DISPATCH_ENUM(dispatch_mach_reason, unsigned long,
  123. DISPATCH_MACH_CONNECTED = 1,
  124. DISPATCH_MACH_MESSAGE_RECEIVED,
  125. DISPATCH_MACH_MESSAGE_SENT,
  126. DISPATCH_MACH_MESSAGE_SEND_FAILED,
  127. DISPATCH_MACH_MESSAGE_NOT_SENT,
  128. DISPATCH_MACH_BARRIER_COMPLETED,
  129. DISPATCH_MACH_DISCONNECTED,
  130. DISPATCH_MACH_CANCELED,
  131. DISPATCH_MACH_REPLY_RECEIVED,
  132. DISPATCH_MACH_NEEDS_DEFERRED_SEND,
  133. DISPATCH_MACH_SIGTERM_RECEIVED,
  134. DISPATCH_MACH_ASYNC_WAITER_DISCONNECTED,
  135. DISPATCH_MACH_REASON_LAST, /* unused */
  136. );
  137. /*!
  138. * @typedef dispatch_mach_send_flags_t
  139. * Flags that can be passed to the *with_flags send functions.
  140. */
  141. DISPATCH_ENUM(dispatch_mach_send_flags, unsigned long,
  142. DISPATCH_MACH_SEND_DEFAULT = 0,
  143. );
  144. /*!
  145. * @typedef dispatch_mach_trailer_t
  146. * Trailer type of mach message received by dispatch mach channels
  147. */
  148. typedef mach_msg_context_trailer_t dispatch_mach_trailer_t;
  149. /*!
  150. * @constant DISPATCH_MACH_RECEIVE_MAX_INLINE_MESSAGE_SIZE
  151. * Maximum size of a message that can be received inline by a dispatch mach
  152. * channel, reception of larger messages requires an extra roundtrip through
  153. * the kernel.
  154. */
  155. #define DISPATCH_MACH_RECEIVE_MAX_INLINE_MESSAGE_SIZE \
  156. (0x4000 - sizeof(dispatch_mach_trailer_t))
  157. /*!
  158. * @typedef dispatch_mach_msg_t
  159. * A dispatch mach message encapsulates messages received or sent with dispatch
  160. * mach channels.
  161. */
  162. DISPATCH_DECL(dispatch_mach_msg);
  163. /*!
  164. * @typedef dispatch_mach_msg_destructor_t
  165. * Dispatch mach message object destructors.
  166. *
  167. * @const DISPATCH_MACH_MSG_DESTRUCTOR_DEFAULT
  168. * Message buffer storage is internal to the object, if a buffer is supplied
  169. * during object creation, its contents are copied.
  170. *
  171. * @const DISPATCH_MACH_MSG_DESTRUCTOR_FREE
  172. * Message buffer will be deallocated with free(3).
  173. *
  174. * @const DISPATCH_MACH_MSG_DESTRUCTOR_FREE
  175. * Message buffer will be deallocated with vm_deallocate.
  176. */
  177. DISPATCH_ENUM(dispatch_mach_msg_destructor, unsigned int,
  178. DISPATCH_MACH_MSG_DESTRUCTOR_DEFAULT = 0,
  179. DISPATCH_MACH_MSG_DESTRUCTOR_FREE,
  180. DISPATCH_MACH_MSG_DESTRUCTOR_VM_DEALLOCATE,
  181. );
  182. /*!
  183. * @function dispatch_mach_msg_create
  184. * Creates a dispatch mach message object, either with a newly allocated message
  185. * buffer of given size, or from an existing message buffer that will be
  186. * deallocated with the specified destructor when the object is released.
  187. *
  188. * If a non-NULL reference to a pointer is provided in 'msg_ptr', it is filled
  189. * with the location of the (possibly newly allocated) message buffer.
  190. *
  191. * It is the responsibility of the application to ensure that it does not modify
  192. * the underlying message buffer once the dispatch mach message object is passed
  193. * to other dispatch mach API.
  194. *
  195. * @param msg The message buffer to create the message object from.
  196. * If 'destructor' is DISPATCH_MACH_MSG_DESTRUCTOR_DEFAULT,
  197. * this argument may be NULL to leave the newly allocated
  198. * message buffer zero-initialized.
  199. * @param size The size of the message buffer.
  200. * Must be >= sizeof(mach_msg_header_t)
  201. * @param destructor The destructor to use to deallocate the message buffer
  202. * when the object is released.
  203. * @param msg_ptr A pointer to a pointer variable to be filled with the
  204. * location of the (possibly newly allocated) message
  205. * buffer, or NULL.
  206. * @result A newly created dispatch mach message object.
  207. */
  208. API_AVAILABLE(macos(10.9), ios(7.0))
  209. DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
  210. DISPATCH_NOTHROW
  211. dispatch_mach_msg_t
  212. dispatch_mach_msg_create(mach_msg_header_t *_Nullable msg, size_t size,
  213. dispatch_mach_msg_destructor_t destructor,
  214. mach_msg_header_t *_Nonnull *_Nullable msg_ptr);
  215. /*!
  216. * @function dispatch_mach_msg_get_msg
  217. * Returns the message buffer underlying a dispatch mach message object.
  218. *
  219. * @param message The dispatch mach message object to query.
  220. * @param size_ptr A pointer to a size_t variable to be filled with the
  221. * size of the message buffer, or NULL.
  222. * @result Pointer to message buffer underlying the object.
  223. */
  224. API_AVAILABLE(macos(10.9), ios(7.0))
  225. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
  226. mach_msg_header_t*
  227. dispatch_mach_msg_get_msg(dispatch_mach_msg_t message,
  228. size_t *_Nullable size_ptr);
  229. #ifdef __BLOCKS__
  230. /*!
  231. * @typedef dispatch_mach_handler_t
  232. * Prototype of dispatch mach channel handler blocks.
  233. *
  234. * @param reason Reason the handler was invoked.
  235. * @param message Message object that was sent or received.
  236. * @param error Mach error code for the send operation.
  237. */
  238. typedef void (^dispatch_mach_handler_t)(dispatch_mach_reason_t reason,
  239. dispatch_mach_msg_t _Nullable message, mach_error_t error);
  240. /*!
  241. * @function dispatch_mach_create
  242. * Create a dispatch mach channel to asynchronously receive and send mach
  243. * messages.
  244. *
  245. * The specified handler will be called with the corresponding reason parameter
  246. * for each message received and for each message that was successfully sent,
  247. * that failed to be sent, or was not sent; as well as when a barrier block
  248. * has completed, or when channel connection, reconnection or cancellation has
  249. * taken effect.
  250. *
  251. * Dispatch mach channels are created in a disconnected state, they must be
  252. * connected via dispatch_mach_connect() to begin receiving and sending
  253. * messages.
  254. *
  255. * @param label
  256. * An optional string label to attach to the channel. The string is not copied,
  257. * if it is non-NULL it must point to storage that remains valid for the
  258. * lifetime of the channel object. May be NULL.
  259. *
  260. * @param queue
  261. * The target queue of the channel, where the handler and barrier blocks will
  262. * be submitted.
  263. *
  264. * @param handler
  265. * The handler block to submit when a message has been sent or received.
  266. *
  267. * @result
  268. * The newly created dispatch mach channel.
  269. */
  270. API_AVAILABLE(macos(10.9), ios(6.0))
  271. DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
  272. DISPATCH_NONNULL3 DISPATCH_NOTHROW
  273. dispatch_mach_t
  274. dispatch_mach_create(const char *_Nullable label,
  275. dispatch_queue_t _Nullable queue, dispatch_mach_handler_t handler);
  276. #endif
  277. /*!
  278. * @typedef dispatch_mach_handler_function_t
  279. * Prototype of dispatch mach channel handler functions.
  280. *
  281. * @param context Application-defined context parameter.
  282. * @param reason Reason the handler was invoked.
  283. * @param message Message object that was sent or received.
  284. * @param error Mach error code for the send operation.
  285. */
  286. typedef void (*dispatch_mach_handler_function_t)(void *_Nullable context,
  287. dispatch_mach_reason_t reason, dispatch_mach_msg_t _Nullable message,
  288. mach_error_t error);
  289. /*!
  290. * @function dispatch_mach_create_f
  291. * Create a dispatch mach channel to asynchronously receive and send mach
  292. * messages.
  293. *
  294. * The specified handler will be called with the corresponding reason parameter
  295. * for each message received and for each message that was successfully sent,
  296. * that failed to be sent, or was not sent; as well as when a barrier block
  297. * has completed, or when channel connection, reconnection or cancellation has
  298. * taken effect.
  299. *
  300. * Dispatch mach channels are created in a disconnected state, they must be
  301. * connected via dispatch_mach_connect() to begin receiving and sending
  302. * messages.
  303. *
  304. * @param label
  305. * An optional string label to attach to the channel. The string is not copied,
  306. * if it is non-NULL it must point to storage that remains valid for the
  307. * lifetime of the channel object. May be NULL.
  308. *
  309. * @param queue
  310. * The target queue of the channel, where the handler and barrier blocks will
  311. * be submitted.
  312. *
  313. * @param context
  314. * The application-defined context to pass to the handler.
  315. *
  316. * @param handler
  317. * The handler function to submit when a message has been sent or received.
  318. *
  319. * @result
  320. * The newly created dispatch mach channel.
  321. */
  322. API_AVAILABLE(macos(10.9), ios(6.0))
  323. DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
  324. DISPATCH_NONNULL4 DISPATCH_NOTHROW
  325. dispatch_mach_t
  326. dispatch_mach_create_f(const char *_Nullable label,
  327. dispatch_queue_t _Nullable queue, void *_Nullable context,
  328. dispatch_mach_handler_function_t handler);
  329. /*!
  330. * @function dispatch_mach_connect
  331. * Connect a mach channel to the specified receive and send rights.
  332. *
  333. * This function must only be called once during the lifetime of a channel, it
  334. * will initiate message reception and perform any already submitted message
  335. * sends or barrier operations.
  336. *
  337. * @param channel
  338. * The mach channel to connect.
  339. *
  340. * @param receive
  341. * The receive right to associate with the channel. May be MACH_PORT_NULL.
  342. *
  343. * @param send
  344. * The send right to associate with the channel. May be MACH_PORT_NULL.
  345. *
  346. * @param checkin
  347. * An optional message object encapsulating the initial check-in message to send
  348. * upon channel connection. The check-in message is sent immediately before the
  349. * first message submitted via dispatch_mach_send(). The message object will be
  350. * retained until the initial send operation is complete (or not peformed due
  351. * to channel cancellation or reconnection) and the channel handler has
  352. * returned. May be NULL.
  353. */
  354. API_AVAILABLE(macos(10.9), ios(6.0))
  355. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
  356. void
  357. dispatch_mach_connect(dispatch_mach_t channel, mach_port_t receive,
  358. mach_port_t send, dispatch_mach_msg_t _Nullable checkin);
  359. /*!
  360. * @function dispatch_mach_reconnect
  361. * Reconnect a mach channel to the specified send right.
  362. *
  363. * Disconnects the channel from the current send right, interrupts any pending
  364. * message sends (and returns the messages as unsent), and reconnects the
  365. * channel to a new send right.
  366. *
  367. * The application must wait for the channel handler to be invoked with
  368. * DISPATCH_MACH_DISCONNECTED before releasing the previous send right.
  369. *
  370. * @param channel
  371. * The mach channel to reconnect.
  372. *
  373. * @param send
  374. * The new send right to associate with the channel. May be MACH_PORT_NULL.
  375. *
  376. * @param checkin
  377. * An optional message object encapsulating the initial check-in message to send
  378. * upon channel reconnection. The check-in message is sent immediately before
  379. * the first message submitted via dispatch_mach_send() after this function
  380. * returns. The message object will be retained until the initial send operation
  381. * is complete (or not peformed due to channel cancellation or reconnection)
  382. * and the channel handler has returned. May be NULL.
  383. */
  384. API_AVAILABLE(macos(10.9), ios(6.0))
  385. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
  386. void
  387. dispatch_mach_reconnect(dispatch_mach_t channel, mach_port_t send,
  388. dispatch_mach_msg_t _Nullable checkin);
  389. /*!
  390. * @function dispatch_mach_cancel
  391. * Cancel a mach channel, preventing any further messages from being sent or
  392. * received.
  393. *
  394. * The application must wait for the channel handler to be invoked with
  395. * DISPATCH_MACH_DISCONNECTED before releasing the underlying send and receive
  396. * rights.
  397. *
  398. * Note: explicit cancellation of mach channels is required, no implicit
  399. * cancellation takes place on release of the last application reference
  400. * to the channel object. Failure to cancel will cause the channel and
  401. * its associated resources to be leaked.
  402. *
  403. * @param channel
  404. * The mach channel to cancel.
  405. */
  406. API_AVAILABLE(macos(10.9), ios(6.0))
  407. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  408. void
  409. dispatch_mach_cancel(dispatch_mach_t channel);
  410. /*!
  411. * @function dispatch_mach_mig_demux
  412. *
  413. * @abstract
  414. * Handles an incoming DISPATCH_MACH_MESSAGE_RECEIVED event through a series of
  415. * MIG subsystem demultiplexers.
  416. *
  417. * @discussion
  418. * This function can be used with a static array of MIG subsystems to try.
  419. * If it returns true, then the dispatch mach message has been consumed as per
  420. * usual MIG rules.
  421. *
  422. * If it returns false, then the mach message has not been touched, and
  423. * consuming or disposing of the rights in the message is mandatory.
  424. *
  425. * It is hence possible to write a manual demuxer this way:
  426. *
  427. * <code>
  428. * if (!dispatch_mach_mig_demux(context, subsystems, count, message)) {
  429. * mach_msg_header_t hdr = dispatch_mach_msg_get_msg(message, NULL);
  430. * switch (hdr->msgh_id) {
  431. * case ...: // manual consumption of messages
  432. * ...
  433. * break;
  434. * default:
  435. * mach_msg_destroy(hdr); // no one claimed the message, destroy it
  436. * }
  437. * }
  438. * </code>
  439. *
  440. * @param context
  441. * An optional context that the MIG routines can query with
  442. * dispatch_mach_mig_demux_get_context() as MIG doesn't support contexts.
  443. *
  444. * @param subsystems
  445. * An array of mig_subsystem structs for all the demuxers to try.
  446. * These are exposed by MIG in the Server header of the generated interface.
  447. *
  448. * @param count
  449. * The number of entries in the subsystems array.
  450. *
  451. * @param msg
  452. * The dispatch mach message to process.
  453. *
  454. * @returns
  455. * Whether or not the dispatch mach message has been consumed.
  456. * If false is returned, then it is the responsibility of the caller to consume
  457. * or dispose of the received message rights.
  458. */
  459. API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
  460. DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NONNULL4 DISPATCH_NOTHROW
  461. bool
  462. dispatch_mach_mig_demux(void *_Nullable context,
  463. const struct mig_subsystem *_Nonnull const subsystems[_Nonnull],
  464. size_t count, dispatch_mach_msg_t msg);
  465. /*!
  466. * @function dispatch_mach_mig_demux_get_context
  467. *
  468. * @abstract
  469. * Returns the context passed to dispatch_mach_mig_demux() from the context of
  470. * a MIG routine implementation.
  471. *
  472. * @discussion
  473. * Calling this function from another context than a MIG routine called from the
  474. * context of dispatch_mach_mig_demux_get_context() is invalid and will cause
  475. * your process to be terminated.
  476. *
  477. * @returns
  478. * The context passed to the outer call to dispatch_mach_mig_demux().
  479. */
  480. API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
  481. DISPATCH_EXPORT DISPATCH_NOTHROW
  482. void *_Nullable
  483. dispatch_mach_mig_demux_get_context(void);
  484. /*!
  485. * @function dispatch_mach_send
  486. * Asynchronously send a message encapsulated in a dispatch mach message object
  487. * to the specified mach channel.
  488. *
  489. * Unless the message is being sent to a send-once right (as determined by the
  490. * presence of MACH_MSG_TYPE_MOVE_SEND_ONCE in the message header remote bits),
  491. * the message header remote port is set to the channel send right before the
  492. * send operation is performed.
  493. *
  494. * If the message expects a direct reply (as determined by the presence of
  495. * MACH_MSG_TYPE_MAKE_SEND_ONCE in the message header local bits) the receive
  496. * right specified in the message header local port will be monitored until a
  497. * reply message (or a send-once notification) is received, or the channel is
  498. * canceled. Hence the application must wait for the channel handler to be
  499. * invoked with a DISPATCH_MACH_DISCONNECTED message before releasing that
  500. * receive right.
  501. *
  502. * If the message send operation is attempted but the channel is canceled
  503. * before the send operation succesfully completes, the message returned to the
  504. * channel handler with DISPATCH_MACH_MESSAGE_NOT_SENT may be the result of a
  505. * pseudo-receive operation. If the message expected a direct reply, the
  506. * receive right originally specified in the message header local port will
  507. * returned in a DISPATCH_MACH_DISCONNECTED message.
  508. *
  509. * @param channel
  510. * The mach channel to which to send the message.
  511. *
  512. * @param message
  513. * The message object encapsulating the message to send. The object will be
  514. * retained until the send operation is complete and the channel handler has
  515. * returned. The storage underlying the message object may be modified by the
  516. * send operation.
  517. *
  518. * @param options
  519. * Additional send options to pass to mach_msg() when performing the send
  520. * operation.
  521. */
  522. API_AVAILABLE(macos(10.9), ios(6.0))
  523. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NOTHROW
  524. void
  525. dispatch_mach_send(dispatch_mach_t channel, dispatch_mach_msg_t message,
  526. mach_msg_option_t options);
  527. /*!
  528. * @function dispatch_mach_send_with_result
  529. * Asynchronously send a message encapsulated in a dispatch mach message object
  530. * to the specified mach channel. If an immediate send can be performed, return
  531. * its result via out parameters.
  532. *
  533. * Unless the message is being sent to a send-once right (as determined by the
  534. * presence of MACH_MSG_TYPE_MOVE_SEND_ONCE in the message header remote bits),
  535. * the message header remote port is set to the channel send right before the
  536. * send operation is performed.
  537. *
  538. * If the message expects a direct reply (as determined by the presence of
  539. * MACH_MSG_TYPE_MAKE_SEND_ONCE in the message header local bits) the receive
  540. * right specified in the message header local port will be monitored until a
  541. * reply message (or a send-once notification) is received, or the channel is
  542. * canceled. Hence the application must wait for the channel handler to be
  543. * invoked with a DISPATCH_MACH_DISCONNECTED message before releasing that
  544. * receive right.
  545. *
  546. * If the message send operation is attempted but the channel is canceled
  547. * before the send operation succesfully completes, the message returned to the
  548. * channel handler with DISPATCH_MACH_MESSAGE_NOT_SENT may be the result of a
  549. * pseudo-receive operation. If the message expected a direct reply, the
  550. * receive right originally specified in the message header local port will
  551. * returned in a DISPATCH_MACH_DISCONNECTED message.
  552. *
  553. * If an immediate send could be performed, returns the resulting reason
  554. * (e.g. DISPATCH_MACH_MESSAGE_SENT) and possible error to the caller in the
  555. * send_result and send_error out parameters (instead of via the channel
  556. * handler), in which case the passed-in message and associated resources
  557. * can be disposed of synchronously.
  558. *
  559. * If a deferred send is required, returns DISPATCH_MACH_NEEDS_DEFERRED_SEND
  560. * in the send_result out parameter to indicate that the passed-in message has
  561. * been retained and associated resources must not be disposed of until the
  562. * message is returned asynchronusly via the channel handler.
  563. *
  564. * @param channel
  565. * The mach channel to which to send the message.
  566. *
  567. * @param message
  568. * The message object encapsulating the message to send. Unless an immediate
  569. * send could be performed, the object will be retained until the asynchronous
  570. * send operation is complete and the channel handler has returned. The storage
  571. * underlying the message object may be modified by the send operation.
  572. *
  573. * @param options
  574. * Additional send options to pass to mach_msg() when performing the send
  575. * operation.
  576. *
  577. * @param send_flags
  578. * Flags to configure the send operation. Must be 0 for now.
  579. *
  580. * @param send_result
  581. * Out parameter to return the result of the immediate send attempt.
  582. * If a deferred send is required, returns DISPATCH_MACH_NEEDS_DEFERRED_SEND.
  583. * Must not be NULL.
  584. *
  585. * @param send_error
  586. * Out parameter to return the error from the immediate send attempt.
  587. * If a deferred send is required, returns 0. Must not be NULL.
  588. */
  589. API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  590. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NONNULL5
  591. DISPATCH_NONNULL6 DISPATCH_NOTHROW
  592. void
  593. dispatch_mach_send_with_result(dispatch_mach_t channel,
  594. dispatch_mach_msg_t message, mach_msg_option_t options,
  595. dispatch_mach_send_flags_t send_flags,
  596. dispatch_mach_reason_t *send_result, mach_error_t *send_error);
  597. /*!
  598. * @function dispatch_mach_send_and_wait_for_reply
  599. * Synchronously send a message encapsulated in a dispatch mach message object
  600. * to the specified mach channel and wait for a reply.
  601. *
  602. * Unless the message is being sent to a send-once right (as determined by the
  603. * presence of MACH_MSG_TYPE_MOVE_SEND_ONCE in the message header remote bits),
  604. * the message header remote port is set to the channel send right before the
  605. * send operation is performed.
  606. *
  607. * The message is required to expect a direct reply (as determined by the
  608. * presence of MACH_MSG_TYPE_MAKE_SEND_ONCE in the message header local bits)
  609. * and this function will not complete until the receive right specified in the
  610. * message header local port receives a reply message (or a send-once
  611. * notification) which will be returned, or until that receive right is
  612. * destroyed in response to the channel being canceled, in which case NULL will
  613. * be returned.
  614. * In all these cases the application must wait for the channel handler to
  615. * be invoked with a DISPATCH_MACH_REPLY_RECEIVED or DISPATCH_MACH_DISCONNECTED
  616. * message before releasing that receive right.
  617. *
  618. * Alternatively, the application may specify MACH_PORT_NULL in the header local
  619. * port to indicate that the channel should create and manage the reply receive
  620. * right internally, including destroying it upon channel cancellation.
  621. * This is a more efficient mode of operation as no asynchronous operations are
  622. * required to return the receive right (i.e. the channel handler will not be
  623. * called as described above).
  624. *
  625. * If the message send operation is attempted but the channel is canceled
  626. * before the send operation succesfully completes, the message returned to the
  627. * channel handler with DISPATCH_MACH_MESSAGE_NOT_SENT may be the result of a
  628. * pseudo-receive operation. The receive right originally specified in the
  629. * message header local port will returned in a DISPATCH_MACH_DISCONNECTED
  630. * message (unless it was MACH_PORT_NULL).
  631. *
  632. * @param channel
  633. * The mach channel to which to send the message.
  634. *
  635. * @param message
  636. * The message object encapsulating the message to send. The object will be
  637. * retained until the send operation is complete and the channel handler has
  638. * returned. The storage underlying the message object may be modified by the
  639. * send operation.
  640. *
  641. * @param options
  642. * Additional send options to pass to mach_msg() when performing the send
  643. * operation.
  644. *
  645. * @result
  646. * The received reply message object, or NULL if the channel was canceled.
  647. */
  648. API_AVAILABLE(macos(10.11), ios(9.0))
  649. DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
  650. DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NOTHROW
  651. dispatch_mach_msg_t _Nullable
  652. dispatch_mach_send_and_wait_for_reply(dispatch_mach_t channel,
  653. dispatch_mach_msg_t message, mach_msg_option_t options);
  654. /*!
  655. * @function dispatch_mach_send_with_result_and_wait_for_reply
  656. * Synchronously send a message encapsulated in a dispatch mach message object
  657. * to the specified mach channel and wait for a reply. If an immediate send can
  658. * be performed, return its result via out parameters.
  659. *
  660. * Unless the message is being sent to a send-once right (as determined by the
  661. * presence of MACH_MSG_TYPE_MOVE_SEND_ONCE in the message header remote bits),
  662. * the message header remote port is set to the channel send right before the
  663. * send operation is performed.
  664. *
  665. * The message is required to expect a direct reply (as determined by the
  666. * presence of MACH_MSG_TYPE_MAKE_SEND_ONCE in the message header local bits)
  667. * and this function will not complete until the receive right specified in the
  668. * message header local port receives a reply message (or a send-once
  669. * notification) which will be returned, or until that receive right is
  670. * destroyed in response to the channel being canceled, in which case NULL will
  671. * be returned.
  672. * In all these cases the application must wait for the channel handler to
  673. * be invoked with a DISPATCH_MACH_REPLY_RECEIVED or DISPATCH_MACH_DISCONNECTED
  674. * message before releasing that receive right.
  675. *
  676. * Alternatively, the application may specify MACH_PORT_NULL in the header local
  677. * port to indicate that the channel should create and manage the reply receive
  678. * right internally, including destroying it upon channel cancellation.
  679. * This is a more efficient mode of operation as no asynchronous operations are
  680. * required to return the receive right (i.e. the channel handler will not be
  681. * called as described above).
  682. *
  683. * If the message send operation is attempted but the channel is canceled
  684. * before the send operation succesfully completes, the message returned to the
  685. * channel handler with DISPATCH_MACH_MESSAGE_NOT_SENT may be the result of a
  686. * pseudo-receive operation. The receive right originally specified in the
  687. * message header local port will returned in a DISPATCH_MACH_DISCONNECTED
  688. * message (unless it was MACH_PORT_NULL).
  689. *
  690. * If an immediate send could be performed, returns the resulting reason
  691. * (e.g. DISPATCH_MACH_MESSAGE_SENT) and possible error to the caller in the
  692. * send_result and send_error out parameters (instead of via the channel
  693. * handler), in which case the passed-in message and associated resources
  694. * can be disposed of synchronously.
  695. *
  696. * If a deferred send is required, returns DISPATCH_MACH_NEEDS_DEFERRED_SEND
  697. * in the send_result out parameter to indicate that the passed-in message has
  698. * been retained and associated resources must not be disposed of until the
  699. * message is returned asynchronusly via the channel handler.
  700. *
  701. * @param channel
  702. * The mach channel to which to send the message.
  703. *
  704. * @param message
  705. * The message object encapsulating the message to send. Unless an immediate
  706. * send could be performed, the object will be retained until the asynchronous
  707. * send operation is complete and the channel handler has returned. The storage
  708. * underlying the message object may be modified by the send operation.
  709. *
  710. * @param options
  711. * Additional send options to pass to mach_msg() when performing the send
  712. * operation.
  713. *
  714. * @param send_flags
  715. * Flags to configure the send operation. Must be 0 for now.
  716. *
  717. * @param send_result
  718. * Out parameter to return the result of the immediate send attempt.
  719. * If a deferred send is required, returns DISPATCH_MACH_NEEDS_DEFERRED_SEND.
  720. * Must not be NULL.
  721. *
  722. * @param send_error
  723. * Out parameter to return the error from the immediate send attempt.
  724. * If a deferred send is required, returns 0. Must not be NULL.
  725. *
  726. * @result
  727. * The received reply message object, or NULL if the channel was canceled.
  728. */
  729. API_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  730. DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
  731. DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NONNULL5 DISPATCH_NONNULL6
  732. DISPATCH_NOTHROW
  733. dispatch_mach_msg_t _Nullable
  734. dispatch_mach_send_with_result_and_wait_for_reply(dispatch_mach_t channel,
  735. dispatch_mach_msg_t message, mach_msg_option_t options,
  736. dispatch_mach_send_flags_t send_flags,
  737. dispatch_mach_reason_t *send_result, mach_error_t *send_error);
  738. #ifdef __BLOCKS__
  739. /*!
  740. * @function dispatch_mach_send_barrier
  741. * Submit a send barrier to the specified mach channel. Messages submitted to
  742. * the channel before the barrier will be sent before the barrier block is
  743. * executed, and messages submitted to the channel after the barrier will only
  744. * be sent once the barrier block has completed and the channel handler
  745. * invocation for the barrier has returned.
  746. *
  747. * @param channel
  748. * The mach channel to which to submit the barrier.
  749. *
  750. * @param barrier
  751. * The barrier block to submit to the channel target queue.
  752. */
  753. API_AVAILABLE(macos(10.9), ios(6.0))
  754. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  755. void
  756. dispatch_mach_send_barrier(dispatch_mach_t channel, dispatch_block_t barrier);
  757. #endif
  758. /*!
  759. * @function dispatch_mach_send_barrier_f
  760. * Submit a send barrier to the specified mach channel. Messages submitted to
  761. * the channel before the barrier will be sent before the barrier block is
  762. * executed, and messages submitted to the channel after the barrier will only
  763. * be sent once the barrier block has completed and the channel handler
  764. * invocation for the barrier has returned.
  765. *
  766. * @param channel
  767. * The mach channel to which to submit the barrier.
  768. *
  769. * @param context
  770. * The application-defined context parameter to pass to the function.
  771. *
  772. * @param barrier
  773. * The barrier function to submit to the channel target queue.
  774. */
  775. API_AVAILABLE(macos(10.9), ios(6.0))
  776. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
  777. void
  778. dispatch_mach_send_barrier_f(dispatch_mach_t channel, void *_Nullable context,
  779. dispatch_function_t barrier);
  780. #ifdef __BLOCKS__
  781. /*!
  782. * @function dispatch_mach_receive_barrier
  783. * Submit a receive barrier to the specified mach channel. Channel handlers for
  784. * messages received by the channel after the receive barrier has been
  785. * submitted will only be invoked once the barrier block has completed and the
  786. * channel handler invocation for the barrier has returned.
  787. *
  788. * @param channel
  789. * The mach channel to which to submit the receive barrier.
  790. *
  791. * @param barrier
  792. * The barrier block to submit to the channel target queue.
  793. */
  794. API_AVAILABLE(macos(10.9), ios(6.0))
  795. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  796. void
  797. dispatch_mach_receive_barrier(dispatch_mach_t channel,
  798. dispatch_block_t barrier);
  799. #endif
  800. /*!
  801. * @function dispatch_mach_receive_barrier_f
  802. * Submit a receive barrier to the specified mach channel. Channel handlers for
  803. * messages received by the channel after the receive barrier has been
  804. * submitted will only be invoked once the barrier block has completed and the
  805. * channel handler invocation for the barrier has returned.
  806. *
  807. * @param channel
  808. * The mach channel to which to submit the receive barrier.
  809. *
  810. * @param context
  811. * The application-defined context parameter to pass to the function.
  812. *
  813. * @param barrier
  814. * The barrier function to submit to the channel target queue.
  815. */
  816. API_AVAILABLE(macos(10.9), ios(6.0))
  817. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
  818. void
  819. dispatch_mach_receive_barrier_f(dispatch_mach_t channel, void *_Nullable context,
  820. dispatch_function_t barrier);
  821. /*!
  822. * @function dispatch_mach_get_checkin_port
  823. * Returns the port specified in the message header remote port of the check-in
  824. * message passed to the most recent invocation of dispatch_mach_connect() or
  825. * dispatch_mach_reconnect() for the provided mach channel (irrespective of the
  826. * completion of the (re)connect or check-in operations in question).
  827. *
  828. * Returns MACH_PORT_NULL if dispatch_mach_connect() has not yet been called or
  829. * if the most recently specified check-in message was NULL, and MACH_PORT_DEAD
  830. * if the channel has been canceled.
  831. *
  832. * It is the responsibility of the application to ensure that the port
  833. * specified in a check-in message remains valid at the time this function is
  834. * called.
  835. *
  836. * @param channel
  837. * The mach channel to query.
  838. *
  839. * @result
  840. * The most recently specified check-in port for the channel.
  841. */
  842. API_AVAILABLE(macos(10.9), ios(6.0))
  843. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  844. mach_port_t
  845. dispatch_mach_get_checkin_port(dispatch_mach_t channel);
  846. // SPI for libxpc
  847. /*
  848. * Type for the callback for receipt of asynchronous replies to
  849. * dispatch_mach_send_with_result_and_async_reply_4libxpc().
  850. */
  851. typedef void (*_Nonnull dispatch_mach_async_reply_callback_t)(void *context,
  852. dispatch_mach_reason_t reason, dispatch_mach_msg_t message);
  853. API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0))
  854. typedef const struct dispatch_mach_xpc_hooks_s {
  855. #define DISPATCH_MACH_XPC_MIN_HOOKS_VERSION 3
  856. #define DISPATCH_MACH_XPC_HOOKS_VERSION 3
  857. unsigned long version;
  858. /* Fields available in version 1. */
  859. /*
  860. * Called to handle a Mach message event inline if possible. Returns true
  861. * if the event was handled, false if the event should be delivered to the
  862. * channel event handler. The implementation should not make any assumptions
  863. * about the thread in which the function is called and cannot assume that
  864. * invocations of this function are serialized relative to each other or
  865. * relative to the channel's event handler function. In addition, the
  866. * handler must not throw an exception or call out to any code that might
  867. * throw an exception.
  868. */
  869. bool (* _Nonnull dmxh_direct_message_handler)(void *_Nullable context,
  870. dispatch_mach_reason_t reason, dispatch_mach_msg_t message,
  871. mach_error_t error);
  872. /* Fields available in version 2. */
  873. /*
  874. * Gets the queue to which a reply to a message sent using
  875. * dispatch_mach_send_with_result_and_async_reply_4libxpc() should be
  876. * delivered. The msg_context argument is the value of the do_ctxt field
  877. * of the outgoing message, as returned by dispatch_get_context().
  878. *
  879. * This function should return a consistent result until an event is
  880. * received for this message. This function must return NULL if
  881. * dispatch_mach_send_with_result_and_async_reply_4libxpc() wasn't used to
  882. * send the message, and non NULL otherwise.
  883. */
  884. dispatch_queue_t _Nullable (*_Nonnull dmxh_msg_context_reply_queue)(
  885. void *_Nonnull msg_context);
  886. /*
  887. * Called when a reply to a message sent by
  888. * dispatch_mach_send_with_result_and_async_reply_4libxpc() is received. The
  889. * message argument points to the reply message and the context argument is
  890. * the context value passed to dispatch_mach_create_4libxpc() when creating
  891. * the Mach channel. The handler is called on the queue that is returned by
  892. * dmxh_msg_context_reply_queue() when the reply is received or if the
  893. * channel is disconnected. The reason argument is
  894. * DISPATCH_MACH_MESSAGE_RECEIVED if a reply has been received or
  895. * DISPATCH_MACH_ASYNC_WAITER_DISCONNECTED if the channel has been
  896. * disconnected. Refer to the documentation for
  897. * dispatch_mach_send_with_result_and_async_reply_4libxpc() for more
  898. * details.
  899. */
  900. dispatch_mach_async_reply_callback_t dmxh_async_reply_handler;
  901. /* Fields available in version 3. */
  902. /**
  903. * Called once when the Mach channel has been activated. If this function
  904. * returns true, a DISPATCH_MACH_SIGTERM_RECEIVED notification will be
  905. * delivered to the channel's event handler when a SIGTERM is received.
  906. */
  907. bool (*_Nonnull dmxh_enable_sigterm_notification)(
  908. void *_Nullable context);
  909. } *dispatch_mach_xpc_hooks_t;
  910. /*!
  911. * @function dispatch_mach_hooks_install_4libxpc
  912. *
  913. * @abstract
  914. * installs XPC callbacks for dispatch Mach channels.
  915. *
  916. * @discussion
  917. * In order to improve the performance of the XPC/dispatch interface, it is
  918. * sometimes useful for dispatch to be able to call directly into XPC. The
  919. * channel hooks structure should be initialized with pointers to XPC callback
  920. * functions, or NULL for callbacks that XPC does not support. The version
  921. * number in the structure must be set to reflect the fields that have been
  922. * initialized. This function may be called only once.
  923. *
  924. * @param hooks
  925. * A pointer to the channel hooks structure. This must remain valid once set.
  926. */
  927. API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0))
  928. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  929. void
  930. dispatch_mach_hooks_install_4libxpc(dispatch_mach_xpc_hooks_t hooks);
  931. /*!
  932. * @function dispatch_mach_create_4libxpc
  933. * Create a dispatch mach channel to asynchronously receive and send mach
  934. * messages, specifically for libxpc.
  935. *
  936. * The specified handler will be called with the corresponding reason parameter
  937. * for each message received and for each message that was successfully sent,
  938. * that failed to be sent, or was not sent; as well as when a barrier block
  939. * has completed, or when channel connection, reconnection or cancellation has
  940. * taken effect. However, the handler will not be called for messages that
  941. * were passed to the XPC hooks dmxh_direct_message_handler function if that
  942. * function returned true.
  943. *
  944. * Dispatch mach channels are created in a disconnected state, they must be
  945. * connected via dispatch_mach_connect() to begin receiving and sending
  946. * messages.
  947. *
  948. * @param label
  949. * An optional string label to attach to the channel. The string is not copied,
  950. * if it is non-NULL it must point to storage that remains valid for the
  951. * lifetime of the channel object. May be NULL.
  952. *
  953. * @param queue
  954. * The target queue of the channel, where the handler and barrier blocks will
  955. * be submitted.
  956. *
  957. * @param context
  958. * The application-defined context to pass to the handler.
  959. *
  960. * @param handler
  961. * The handler function to submit when a message has been sent or received.
  962. *
  963. * @result
  964. * The newly created dispatch mach channel.
  965. */
  966. API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0))
  967. DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
  968. DISPATCH_NONNULL4 DISPATCH_NOTHROW
  969. dispatch_mach_t
  970. dispatch_mach_create_4libxpc(const char *_Nullable label,
  971. dispatch_queue_t _Nullable queue, void *_Nullable context,
  972. dispatch_mach_handler_function_t handler);
  973. /*!
  974. * @function dispatch_mach_send_with_result_and_async_reply_4libxpc
  975. * SPI for XPC that asynchronously sends a message encapsulated in a dispatch
  976. * mach message object to the specified mach channel. If an immediate send can
  977. * be performed, returns its result via out parameters.
  978. *
  979. * The reply message is processed on the queue returned by the
  980. * dmxh_msg_context_reply_queue function in the dispatch_mach_xpc_hooks_s
  981. * structure, which is called with a single argument whose value is the
  982. * do_ctxt field of the message argument to this function. The reply message is
  983. * delivered to the dmxh_async_reply_handler hook function instead of being
  984. * passed to the channel event handler.
  985. *
  986. * If the dmxh_msg_context_reply_queue function is not implemented or returns
  987. * NULL, the reply message is delivered to the channel event handler on the
  988. * channel queue.
  989. *
  990. * Unless the message is being sent to a send-once right (as determined by the
  991. * presence of MACH_MSG_TYPE_MOVE_SEND_ONCE in the message header remote bits),
  992. * the message header remote port is set to the channel send right before the
  993. * send operation is performed.
  994. *
  995. * The message is required to expect a direct reply (as determined by the
  996. * presence of MACH_MSG_TYPE_MAKE_SEND_ONCE in the message header local bits).
  997. * The receive right specified in the message header local port will be
  998. * monitored until a reply message (or a send-once notification) is received, or
  999. * the channel is canceled. Hence the application must wait for the reply
  1000. * to be received or for a DISPATCH_MACH_ASYNC_WAITER_DISCONNECTED message
  1001. * before releasing that receive right.
  1002. *
  1003. * If the message send operation is attempted but the channel is canceled
  1004. * before the send operation succesfully completes, the message returned to the
  1005. * channel handler with DISPATCH_MACH_MESSAGE_NOT_SENT may be the result of a
  1006. * pseudo-receive operation and the receive right originally specified in the
  1007. * message header local port will be returned in a
  1008. * DISPATCH_MACH_ASYNC_WAITER_DISCONNECTED message.
  1009. *
  1010. * If an immediate send could be performed, returns the resulting reason
  1011. * (e.g. DISPATCH_MACH_MESSAGE_SENT) and possible error to the caller in the
  1012. * send_result and send_error out parameters (instead of via the channel
  1013. * handler), in which case the passed-in message and associated resources
  1014. * can be disposed of synchronously.
  1015. *
  1016. * If a deferred send is required, returns DISPATCH_MACH_NEEDS_DEFERRED_SEND
  1017. * in the send_result out parameter to indicate that the passed-in message has
  1018. * been retained and associated resources must not be disposed of until the
  1019. * message is returned asynchronusly via the channel handler.
  1020. *
  1021. * @param channel
  1022. * The mach channel to which to send the message.
  1023. *
  1024. * @param message
  1025. * The message object encapsulating the message to send. Unless an immediate
  1026. * send could be performed, the object will be retained until the asynchronous
  1027. * send operation is complete and the channel handler has returned. The storage
  1028. * underlying the message object may be modified by the send operation.
  1029. *
  1030. * @param options
  1031. * Additional send options to pass to mach_msg() when performing the send
  1032. * operation.
  1033. *
  1034. * @param send_flags
  1035. * Flags to configure the send operation. Must be 0 for now.
  1036. *
  1037. * @param send_result
  1038. * Out parameter to return the result of the immediate send attempt.
  1039. * If a deferred send is required, returns DISPATCH_MACH_NEEDS_DEFERRED_SEND.
  1040. * Must not be NULL.
  1041. *
  1042. * @param send_error
  1043. * Out parameter to return the error from the immediate send attempt.
  1044. * If a deferred send is required, returns 0. Must not be NULL.
  1045. */
  1046. API_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0))
  1047. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL2 DISPATCH_NONNULL5
  1048. DISPATCH_NONNULL6 DISPATCH_NOTHROW
  1049. void
  1050. dispatch_mach_send_with_result_and_async_reply_4libxpc(dispatch_mach_t channel,
  1051. dispatch_mach_msg_t message, mach_msg_option_t options,
  1052. dispatch_mach_send_flags_t send_flags,
  1053. dispatch_mach_reason_t *send_result, mach_error_t *send_error);
  1054. /*!
  1055. * @function dispatch_mach_handoff_reply_f
  1056. *
  1057. * @abstract
  1058. * Inform the runtime that a given sync IPC is being handed off to a new queue
  1059. * hierarchy.
  1060. *
  1061. * @discussion
  1062. * This function can only be called from the context of an IPC handler, or from
  1063. * a work item created by dispatch_mach_handoff_reply_f. Calling
  1064. * dispatch_mach_handoff_reply_f from a different context is undefined and will
  1065. * cause the process to be terminated.
  1066. *
  1067. * dispatch_mach_handoff_reply_f will only take effect when the work item that
  1068. * issued it returns.
  1069. *
  1070. * @param queue
  1071. * The queue the IPC reply will be handed off to. This queue must be an
  1072. * immutable queue hierarchy (with all nodes created with
  1073. * dispatch_queue_create_with_target() for example).
  1074. *
  1075. * @param port
  1076. * The send once right that will be replied to.
  1077. */
  1078. API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
  1079. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL4 DISPATCH_NOTHROW
  1080. void
  1081. dispatch_mach_handoff_reply_f(dispatch_queue_t queue, mach_port_t port,
  1082. void *_Nullable ctxt, dispatch_function_t func);
  1083. /*!
  1084. * @function dispatch_mach_handoff_reply
  1085. *
  1086. * @abstract
  1087. * Inform the runtime that a given sync IPC is being handed off to a new queue
  1088. * hierarchy.
  1089. *
  1090. * @see dispatch_mach_handoff_reply_f
  1091. */
  1092. API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0))
  1093. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
  1094. void
  1095. dispatch_mach_handoff_reply(dispatch_queue_t queue, mach_port_t port,
  1096. dispatch_block_t block);
  1097. DISPATCH_ASSUME_NONNULL_END
  1098. #endif // DISPATCH_MACH_SPI
  1099. __END_DECLS
  1100. #endif