io_private.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright (c) 2009-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_IO_PRIVATE__
  26. #define __DISPATCH_IO_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. DISPATCH_ASSUME_NONNULL_BEGIN
  32. __BEGIN_DECLS
  33. /*!
  34. * @function dispatch_read_f
  35. * Schedule a read operation for asynchronous execution on the specified file
  36. * descriptor. The specified handler is enqueued with the data read from the
  37. * file descriptor when the operation has completed or an error occurs.
  38. *
  39. * The data object passed to the handler will be automatically released by the
  40. * system when the handler returns. It is the responsibility of the application
  41. * to retain, concatenate or copy the data object if it is needed after the
  42. * handler returns.
  43. *
  44. * The data object passed to the handler will only contain as much data as is
  45. * currently available from the file descriptor (up to the specified length).
  46. *
  47. * If an unrecoverable error occurs on the file descriptor, the handler will be
  48. * enqueued with the appropriate error code along with a data object of any data
  49. * that could be read successfully.
  50. *
  51. * An invocation of the handler with an error code of zero and an empty data
  52. * object indicates that EOF was reached.
  53. *
  54. * The system takes control of the file descriptor until the handler is
  55. * enqueued, and during this time file descriptor flags such as O_NONBLOCK will
  56. * be modified by the system on behalf of the application. It is an error for
  57. * the application to modify a file descriptor directly while it is under the
  58. * control of the system, but it may create additional dispatch I/O convenience
  59. * operations or dispatch I/O channels associated with that file descriptor.
  60. *
  61. * @param fd The file descriptor from which to read the data.
  62. * @param length The length of data to read from the file descriptor,
  63. * or SIZE_MAX to indicate that all of the data currently
  64. * available from the file descriptor should be read.
  65. * @param queue The dispatch queue to which the handler should be
  66. * submitted.
  67. * @param context The application-defined context parameter to pass to
  68. * the handler function.
  69. * @param handler The handler to enqueue when data is ready to be
  70. * delivered.
  71. * param context Application-defined context parameter.
  72. * param data The data read from the file descriptor.
  73. * param error An errno condition for the read operation or
  74. * zero if the read was successful.
  75. */
  76. API_AVAILABLE(macos(10.9), ios(7.0))
  77. DISPATCH_EXPORT DISPATCH_NONNULL3 DISPATCH_NONNULL5 DISPATCH_NOTHROW
  78. void
  79. dispatch_read_f(dispatch_fd_t fd,
  80. size_t length,
  81. dispatch_queue_t queue,
  82. void *_Nullable context,
  83. void (*handler)(void *_Nullable context, dispatch_data_t data, int error));
  84. /*!
  85. * @function dispatch_write_f
  86. * Schedule a write operation for asynchronous execution on the specified file
  87. * descriptor. The specified handler is enqueued when the operation has
  88. * completed or an error occurs.
  89. *
  90. * If an unrecoverable error occurs on the file descriptor, the handler will be
  91. * enqueued with the appropriate error code along with the data that could not
  92. * be successfully written.
  93. *
  94. * An invocation of the handler with an error code of zero indicates that the
  95. * data was fully written to the channel.
  96. *
  97. * The system takes control of the file descriptor until the handler is
  98. * enqueued, and during this time file descriptor flags such as O_NONBLOCK will
  99. * be modified by the system on behalf of the application. It is an error for
  100. * the application to modify a file descriptor directly while it is under the
  101. * control of the system, but it may create additional dispatch I/O convenience
  102. * operations or dispatch I/O channels associated with that file descriptor.
  103. *
  104. * @param fd The file descriptor to which to write the data.
  105. * @param data The data object to write to the file descriptor.
  106. * @param queue The dispatch queue to which the handler should be
  107. * submitted.
  108. * @param context The application-defined context parameter to pass to
  109. * the handler function.
  110. * @param handler The handler to enqueue when the data has been written.
  111. * param context Application-defined context parameter.
  112. * param data The data that could not be written to the I/O
  113. * channel, or NULL.
  114. * param error An errno condition for the write operation or
  115. * zero if the write was successful.
  116. */
  117. API_AVAILABLE(macos(10.9), ios(7.0))
  118. DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NONNULL3 DISPATCH_NONNULL5
  119. DISPATCH_NOTHROW
  120. void
  121. dispatch_write_f(dispatch_fd_t fd,
  122. dispatch_data_t data,
  123. dispatch_queue_t queue,
  124. void *_Nullable context,
  125. void (*handler)(void *_Nullable context, dispatch_data_t _Nullable data,
  126. int error));
  127. /*!
  128. * @function dispatch_io_create_f
  129. * Create a dispatch I/O channel associated with a file descriptor. The system
  130. * takes control of the file descriptor until the channel is closed, an error
  131. * occurs on the file descriptor or all references to the channel are released.
  132. * At that time the specified cleanup handler will be enqueued and control over
  133. * the file descriptor relinquished.
  134. *
  135. * While a file descriptor is under the control of a dispatch I/O channel, file
  136. * descriptor flags such as O_NONBLOCK will be modified by the system on behalf
  137. * of the application. It is an error for the application to modify a file
  138. * descriptor directly while it is under the control of a dispatch I/O channel,
  139. * but it may create additional channels associated with that file descriptor.
  140. *
  141. * @param type The desired type of I/O channel (DISPATCH_IO_STREAM
  142. * or DISPATCH_IO_RANDOM).
  143. * @param fd The file descriptor to associate with the I/O channel.
  144. * @param queue The dispatch queue to which the handler should be submitted.
  145. * @param context The application-defined context parameter to pass to
  146. * the cleanup handler function.
  147. * @param cleanup_handler The handler to enqueue when the system
  148. * relinquishes control over the file descriptor.
  149. * param context Application-defined context parameter.
  150. * param error An errno condition if control is relinquished
  151. * because channel creation failed, zero otherwise.
  152. * @result The newly created dispatch I/O channel or NULL if an error
  153. * occurred (invalid type specified).
  154. */
  155. API_AVAILABLE(macos(10.9), ios(7.0))
  156. DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
  157. DISPATCH_NOTHROW
  158. dispatch_io_t
  159. dispatch_io_create_f(dispatch_io_type_t type,
  160. dispatch_fd_t fd,
  161. dispatch_queue_t queue,
  162. void *_Nullable context,
  163. void (*cleanup_handler)(void *_Nullable context, int error));
  164. /*!
  165. * @function dispatch_io_create_with_path_f
  166. * Create a dispatch I/O channel associated with a path name. The specified
  167. * path, oflag and mode parameters will be passed to open(2) when the first I/O
  168. * operation on the channel is ready to execute and the resulting file
  169. * descriptor will remain open and under the control of the system until the
  170. * channel is closed, an error occurs on the file descriptor or all references
  171. * to the channel are released. At that time the file descriptor will be closed
  172. * and the specified cleanup handler will be enqueued.
  173. *
  174. * @param type The desired type of I/O channel (DISPATCH_IO_STREAM
  175. * or DISPATCH_IO_RANDOM).
  176. * @param path The absolute path to associate with the I/O channel.
  177. * @param oflag The flags to pass to open(2) when opening the file at
  178. * path.
  179. * @param mode The mode to pass to open(2) when creating the file at
  180. * path (i.e. with flag O_CREAT), zero otherwise.
  181. * @param queue The dispatch queue to which the handler should be
  182. * submitted.
  183. * @param context The application-defined context parameter to pass to
  184. * the cleanup handler function.
  185. * @param cleanup_handler The handler to enqueue when the system
  186. * has closed the file at path.
  187. * param context Application-defined context parameter.
  188. * param error An errno condition if control is relinquished
  189. * because channel creation or opening of the
  190. * specified file failed, zero otherwise.
  191. * @result The newly created dispatch I/O channel or NULL if an error
  192. * occurred (invalid type or non-absolute path specified).
  193. */
  194. API_AVAILABLE(macos(10.9), ios(7.0))
  195. DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED
  196. DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  197. dispatch_io_t
  198. dispatch_io_create_with_path_f(dispatch_io_type_t type,
  199. const char *path, int oflag, mode_t mode,
  200. dispatch_queue_t queue,
  201. void *_Nullable context,
  202. void (*cleanup_handler)(void *_Nullable context, int error));
  203. /*!
  204. * @function dispatch_io_create_with_io_f
  205. * Create a new dispatch I/O channel from an existing dispatch I/O channel.
  206. * The new channel inherits the file descriptor or path name associated with
  207. * the existing channel, but not its channel type or policies.
  208. *
  209. * If the existing channel is associated with a file descriptor, control by the
  210. * system over that file descriptor is extended until the new channel is also
  211. * closed, an error occurs on the file descriptor, or all references to both
  212. * channels are released. At that time the specified cleanup handler will be
  213. * enqueued and control over the file descriptor relinquished.
  214. *
  215. * While a file descriptor is under the control of a dispatch I/O channel, file
  216. * descriptor flags such as O_NONBLOCK will be modified by the system on behalf
  217. * of the application. It is an error for the application to modify a file
  218. * descriptor directly while it is under the control of a dispatch I/O channel,
  219. * but it may create additional channels associated with that file descriptor.
  220. *
  221. * @param type The desired type of I/O channel (DISPATCH_IO_STREAM
  222. * or DISPATCH_IO_RANDOM).
  223. * @param io The existing channel to create the new I/O channel from.
  224. * @param queue The dispatch queue to which the handler should be submitted.
  225. * @param context The application-defined context parameter to pass to
  226. * the cleanup handler function.
  227. * @param cleanup_handler The handler to enqueue when the system
  228. * relinquishes control over the file descriptor
  229. * (resp. closes the file at path) associated with
  230. * the existing channel.
  231. * param context Application-defined context parameter.
  232. * param error An errno condition if control is relinquished
  233. * because channel creation failed, zero otherwise.
  234. * @result The newly created dispatch I/O channel or NULL if an error
  235. * occurred (invalid type specified).
  236. */
  237. API_AVAILABLE(macos(10.9), ios(7.0))
  238. DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED
  239. DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  240. dispatch_io_t
  241. dispatch_io_create_with_io_f(dispatch_io_type_t type,
  242. dispatch_io_t io,
  243. dispatch_queue_t queue,
  244. void *_Nullable context,
  245. void (*cleanup_handler)(void *_Nullable context, int error));
  246. /*!
  247. * @typedef dispatch_io_handler_function_t
  248. * The prototype of I/O handler functions for dispatch I/O operations.
  249. *
  250. * @param context Application-defined context parameter.
  251. * @param done A flag indicating whether the operation is complete.
  252. * @param data The data object to be handled.
  253. * @param error An errno condition for the operation.
  254. */
  255. typedef void (*dispatch_io_handler_function_t)(void *_Nullable context,
  256. bool done, dispatch_data_t _Nullable data, int error);
  257. /*!
  258. * @function dispatch_io_read_f
  259. * Schedule a read operation for asynchronous execution on the specified I/O
  260. * channel. The I/O handler is enqueued one or more times depending on the
  261. * general load of the system and the policy specified on the I/O channel.
  262. *
  263. * Any data read from the channel is described by the dispatch data object
  264. * passed to the I/O handler. This object will be automatically released by the
  265. * system when the I/O handler returns. It is the responsibility of the
  266. * application to retain, concatenate or copy the data object if it is needed
  267. * after the I/O handler returns.
  268. *
  269. * Dispatch I/O handlers are not reentrant. The system will ensure that no new
  270. * I/O handler instance is invoked until the previously enqueued handler
  271. * function has returned.
  272. *
  273. * An invocation of the I/O handler with the done flag set indicates that the
  274. * read operation is complete and that the handler will not be enqueued again.
  275. *
  276. * If an unrecoverable error occurs on the I/O channel's underlying file
  277. * descriptor, the I/O handler will be enqueued with the done flag set, the
  278. * appropriate error code and a NULL data object.
  279. *
  280. * An invocation of the I/O handler with the done flag set, an error code of
  281. * zero and an empty data object indicates that EOF was reached.
  282. *
  283. * @param channel The dispatch I/O channel from which to read the data.
  284. * @param offset The offset relative to the channel position from which
  285. * to start reading (only for DISPATCH_IO_RANDOM).
  286. * @param length The length of data to read from the I/O channel, or
  287. * SIZE_MAX to indicate that data should be read until EOF
  288. * is reached.
  289. * @param queue The dispatch queue to which the I/O handler should be
  290. * submitted.
  291. * @param context The application-defined context parameter to pass to
  292. * the handler function.
  293. * @param io_handler The I/O handler to enqueue when data is ready to be
  294. * delivered.
  295. * param context Application-defined context parameter.
  296. * param done A flag indicating whether the operation is complete.
  297. * param data An object with the data most recently read from the
  298. * I/O channel as part of this read operation, or NULL.
  299. * param error An errno condition for the read operation or zero if
  300. * the read was successful.
  301. */
  302. API_AVAILABLE(macos(10.9), ios(7.0))
  303. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL4 DISPATCH_NONNULL6
  304. DISPATCH_NOTHROW
  305. void
  306. dispatch_io_read_f(dispatch_io_t channel,
  307. off_t offset,
  308. size_t length,
  309. dispatch_queue_t queue,
  310. void *_Nullable context,
  311. dispatch_io_handler_function_t io_handler);
  312. /*!
  313. * @function dispatch_io_write_f
  314. * Schedule a write operation for asynchronous execution on the specified I/O
  315. * channel. The I/O handler is enqueued one or more times depending on the
  316. * general load of the system and the policy specified on the I/O channel.
  317. *
  318. * Any data remaining to be written to the I/O channel is described by the
  319. * dispatch data object passed to the I/O handler. This object will be
  320. * automatically released by the system when the I/O handler returns. It is the
  321. * responsibility of the application to retain, concatenate or copy the data
  322. * object if it is needed after the I/O handler returns.
  323. *
  324. * Dispatch I/O handlers are not reentrant. The system will ensure that no new
  325. * I/O handler instance is invoked until the previously enqueued handler
  326. * function has returned.
  327. *
  328. * An invocation of the I/O handler with the done flag set indicates that the
  329. * write operation is complete and that the handler will not be enqueued again.
  330. *
  331. * If an unrecoverable error occurs on the I/O channel's underlying file
  332. * descriptor, the I/O handler will be enqueued with the done flag set, the
  333. * appropriate error code and an object containing the data that could not be
  334. * written.
  335. *
  336. * An invocation of the I/O handler with the done flag set and an error code of
  337. * zero indicates that the data was fully written to the channel.
  338. *
  339. * @param channel The dispatch I/O channel on which to write the data.
  340. * @param offset The offset relative to the channel position from which
  341. * to start writing (only for DISPATCH_IO_RANDOM).
  342. * @param data The data to write to the I/O channel. The data object
  343. * will be retained by the system until the write operation
  344. * is complete.
  345. * @param queue The dispatch queue to which the I/O handler should be
  346. * submitted.
  347. * @param context The application-defined context parameter to pass to
  348. * the handler function.
  349. * @param io_handler The I/O handler to enqueue when data has been delivered.
  350. * param context Application-defined context parameter.
  351. * param done A flag indicating whether the operation is complete.
  352. * param data An object of the data remaining to be
  353. * written to the I/O channel as part of this write
  354. * operation, or NULL.
  355. * param error An errno condition for the write operation or zero
  356. * if the write was successful.
  357. */
  358. API_AVAILABLE(macos(10.9), ios(7.0))
  359. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NONNULL4
  360. DISPATCH_NONNULL6 DISPATCH_NOTHROW
  361. void
  362. dispatch_io_write_f(dispatch_io_t channel,
  363. off_t offset,
  364. dispatch_data_t data,
  365. dispatch_queue_t queue,
  366. void *_Nullable context,
  367. dispatch_io_handler_function_t io_handler);
  368. /*!
  369. * @function dispatch_io_barrier_f
  370. * Schedule a barrier operation on the specified I/O channel; all previously
  371. * scheduled operations on the channel will complete before the provided
  372. * barrier function is enqueued onto the global queue determined by the
  373. * channel's target queue, and no subsequently scheduled operations will start
  374. * until the barrier function has returned.
  375. *
  376. * If multiple channels are associated with the same file descriptor, a barrier
  377. * operation scheduled on any of these channels will act as a barrier across all
  378. * channels in question, i.e. all previously scheduled operations on any of the
  379. * channels will complete before the barrier function is enqueued, and no
  380. * operations subsequently scheduled on any of the channels will start until the
  381. * barrier function has returned.
  382. *
  383. * While the barrier function is running, it may safely operate on the channel's
  384. * underlying file descriptor with fsync(2), lseek(2) etc. (but not close(2)).
  385. *
  386. * @param channel The dispatch I/O channel to schedule the barrier on.
  387. * @param context The application-defined context parameter to pass to
  388. * the barrier function.
  389. * @param barrier The barrier function.
  390. */
  391. API_AVAILABLE(macos(10.9), ios(7.0))
  392. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NOTHROW
  393. void
  394. dispatch_io_barrier_f(dispatch_io_t channel,
  395. void *_Nullable context,
  396. dispatch_function_t barrier);
  397. __END_DECLS
  398. DISPATCH_ASSUME_NONNULL_END
  399. #endif /* __DISPATCH_IO_PRIVATE__ */