io.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. #ifndef __DISPATCH_IO__
  21. #define __DISPATCH_IO__
  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. DISPATCH_ASSUME_NONNULL_BEGIN
  27. __BEGIN_DECLS
  28. /*! @header
  29. * Dispatch I/O provides both stream and random access asynchronous read and
  30. * write operations on file descriptors. One or more dispatch I/O channels may
  31. * be created from a file descriptor as either the DISPATCH_IO_STREAM type or
  32. * DISPATCH_IO_RANDOM type. Once a channel has been created the application may
  33. * schedule asynchronous read and write operations.
  34. *
  35. * The application may set policies on the dispatch I/O channel to indicate the
  36. * desired frequency of I/O handlers for long-running operations.
  37. *
  38. * Dispatch I/O also provides a memory management model for I/O buffers that
  39. * avoids unnecessary copying of data when pipelined between channels. Dispatch
  40. * I/O monitors the overall memory pressure and I/O access patterns for the
  41. * application to optimize resource utilization.
  42. */
  43. /*!
  44. * @typedef dispatch_fd_t
  45. * Native file descriptor type for the platform.
  46. */
  47. #if defined(_WIN32)
  48. typedef intptr_t dispatch_fd_t;
  49. #else
  50. typedef int dispatch_fd_t;
  51. #endif
  52. /*!
  53. * @functiongroup Dispatch I/O Convenience API
  54. * Convenience wrappers around the dispatch I/O channel API, with simpler
  55. * callback handler semantics and no explicit management of channel objects.
  56. * File descriptors passed to the convenience API are treated as streams, and
  57. * scheduling multiple operations on one file descriptor via the convenience API
  58. * may incur more overhead than by using the dispatch I/O channel API directly.
  59. */
  60. #ifdef __BLOCKS__
  61. /*!
  62. * @function dispatch_read
  63. * Schedule a read operation for asynchronous execution on the specified file
  64. * descriptor. The specified handler is enqueued with the data read from the
  65. * file descriptor when the operation has completed or an error occurs.
  66. *
  67. * The data object passed to the handler will be automatically released by the
  68. * system when the handler returns. It is the responsibility of the application
  69. * to retain, concatenate or copy the data object if it is needed after the
  70. * handler returns.
  71. *
  72. * The data object passed to the handler will only contain as much data as is
  73. * currently available from the file descriptor (up to the specified length).
  74. *
  75. * If an unrecoverable error occurs on the file descriptor, the handler will be
  76. * enqueued with the appropriate error code along with a data object of any data
  77. * that could be read successfully.
  78. *
  79. * An invocation of the handler with an error code of zero and an empty data
  80. * object indicates that EOF was reached.
  81. *
  82. * The system takes control of the file descriptor until the handler is
  83. * enqueued, and during this time file descriptor flags such as O_NONBLOCK will
  84. * be modified by the system on behalf of the application. It is an error for
  85. * the application to modify a file descriptor directly while it is under the
  86. * control of the system, but it may create additional dispatch I/O convenience
  87. * operations or dispatch I/O channels associated with that file descriptor.
  88. *
  89. * @param fd The file descriptor from which to read the data.
  90. * @param length The length of data to read from the file descriptor,
  91. * or SIZE_MAX to indicate that all of the data currently
  92. * available from the file descriptor should be read.
  93. * @param queue The dispatch queue to which the handler should be
  94. * submitted.
  95. * @param handler The handler to enqueue when data is ready to be
  96. * delivered.
  97. * param data The data read from the file descriptor.
  98. * param error An errno condition for the read operation or
  99. * zero if the read was successful.
  100. */
  101. API_AVAILABLE(macos(10.7), ios(5.0))
  102. DISPATCH_EXPORT DISPATCH_NONNULL3 DISPATCH_NONNULL4 DISPATCH_NOTHROW
  103. void
  104. dispatch_read(dispatch_fd_t fd,
  105. size_t length,
  106. dispatch_queue_t queue,
  107. void (^handler)(dispatch_data_t data, int error));
  108. /*!
  109. * @function dispatch_write
  110. * Schedule a write operation for asynchronous execution on the specified file
  111. * descriptor. The specified handler is enqueued when the operation has
  112. * completed or an error occurs.
  113. *
  114. * If an unrecoverable error occurs on the file descriptor, the handler will be
  115. * enqueued with the appropriate error code along with the data that could not
  116. * be successfully written.
  117. *
  118. * An invocation of the handler with an error code of zero indicates that the
  119. * data was fully written to the channel.
  120. *
  121. * The system takes control of the file descriptor until the handler is
  122. * enqueued, and during this time file descriptor flags such as O_NONBLOCK will
  123. * be modified by the system on behalf of the application. It is an error for
  124. * the application to modify a file descriptor directly while it is under the
  125. * control of the system, but it may create additional dispatch I/O convenience
  126. * operations or dispatch I/O channels associated with that file descriptor.
  127. *
  128. * @param fd The file descriptor to which to write the data.
  129. * @param data The data object to write to the file descriptor.
  130. * @param queue The dispatch queue to which the handler should be
  131. * submitted.
  132. * @param handler The handler to enqueue when the data has been written.
  133. * param data The data that could not be written to the I/O
  134. * channel, or NULL.
  135. * param error An errno condition for the write operation or
  136. * zero if the write was successful.
  137. */
  138. API_AVAILABLE(macos(10.7), ios(5.0))
  139. DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NONNULL3 DISPATCH_NONNULL4
  140. DISPATCH_NOTHROW
  141. void
  142. dispatch_write(dispatch_fd_t fd,
  143. dispatch_data_t data,
  144. dispatch_queue_t queue,
  145. void (^handler)(dispatch_data_t _Nullable data, int error));
  146. #endif /* __BLOCKS__ */
  147. /*!
  148. * @functiongroup Dispatch I/O Channel API
  149. */
  150. /*!
  151. * @typedef dispatch_io_t
  152. * A dispatch I/O channel represents the asynchronous I/O policy applied to a
  153. * file descriptor. I/O channels are first class dispatch objects and may be
  154. * retained and released, suspended and resumed, etc.
  155. */
  156. DISPATCH_DECL(dispatch_io);
  157. /*!
  158. * @typedef dispatch_io_type_t
  159. * The type of a dispatch I/O channel:
  160. *
  161. * @const DISPATCH_IO_STREAM A dispatch I/O channel representing a stream of
  162. * bytes. Read and write operations on a channel of this type are performed
  163. * serially (in order of creation) and read/write data at the file pointer
  164. * position that is current at the time the operation starts executing.
  165. * Operations of different type (read vs. write) may be performed simultaneously.
  166. * Offsets passed to operations on a channel of this type are ignored.
  167. *
  168. * @const DISPATCH_IO_RANDOM A dispatch I/O channel representing a random
  169. * access file. Read and write operations on a channel of this type may be
  170. * performed concurrently and read/write data at the specified offset. Offsets
  171. * are interpreted relative to the file pointer position current at the time the
  172. * I/O channel is created. Attempting to create a channel of this type for a
  173. * file descriptor that is not seekable will result in an error.
  174. */
  175. #define DISPATCH_IO_STREAM 0
  176. #define DISPATCH_IO_RANDOM 1
  177. typedef unsigned long dispatch_io_type_t;
  178. #ifdef __BLOCKS__
  179. /*!
  180. * @function dispatch_io_create
  181. * Create a dispatch I/O channel associated with a file descriptor. The system
  182. * takes control of the file descriptor until the channel is closed, an error
  183. * occurs on the file descriptor or all references to the channel are released.
  184. * At that time the specified cleanup handler will be enqueued and control over
  185. * the file descriptor relinquished.
  186. *
  187. * While a file descriptor is under the control of a dispatch I/O channel, file
  188. * descriptor flags such as O_NONBLOCK will be modified by the system on behalf
  189. * of the application. It is an error for the application to modify a file
  190. * descriptor directly while it is under the control of a dispatch I/O channel,
  191. * but it may create additional channels associated with that file descriptor.
  192. *
  193. * @param type The desired type of I/O channel (DISPATCH_IO_STREAM
  194. * or DISPATCH_IO_RANDOM).
  195. * @param fd The file descriptor to associate with the I/O channel.
  196. * @param queue The dispatch queue to which the handler should be submitted.
  197. * @param cleanup_handler The handler to enqueue when the system
  198. * relinquishes control over the file descriptor.
  199. * param error An errno condition if control is relinquished
  200. * because channel creation failed, zero otherwise.
  201. * @result The newly created dispatch I/O channel or NULL if an error
  202. * occurred (invalid type specified).
  203. */
  204. API_AVAILABLE(macos(10.7), ios(5.0))
  205. DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT
  206. DISPATCH_NOTHROW
  207. dispatch_io_t
  208. dispatch_io_create(dispatch_io_type_t type,
  209. dispatch_fd_t fd,
  210. dispatch_queue_t queue,
  211. void (^cleanup_handler)(int error));
  212. /*!
  213. * @function dispatch_io_create_with_path
  214. * Create a dispatch I/O channel associated with a path name. The specified
  215. * path, oflag and mode parameters will be passed to open(2) when the first I/O
  216. * operation on the channel is ready to execute and the resulting file
  217. * descriptor will remain open and under the control of the system until the
  218. * channel is closed, an error occurs on the file descriptor or all references
  219. * to the channel are released. At that time the file descriptor will be closed
  220. * and the specified cleanup handler will be enqueued.
  221. *
  222. * @param type The desired type of I/O channel (DISPATCH_IO_STREAM
  223. * or DISPATCH_IO_RANDOM).
  224. * @param path The absolute path to associate with the I/O channel.
  225. * @param oflag The flags to pass to open(2) when opening the file at
  226. * path.
  227. * @param mode The mode to pass to open(2) when creating the file at
  228. * path (i.e. with flag O_CREAT), zero otherwise.
  229. * @param queue The dispatch queue to which the handler should be
  230. * submitted.
  231. * @param cleanup_handler The handler to enqueue when the system
  232. * has closed the file at path.
  233. * param error An errno condition if control is relinquished
  234. * because channel creation or opening of the
  235. * specified file failed, zero otherwise.
  236. * @result The newly created dispatch I/O channel or NULL if an error
  237. * occurred (invalid type or non-absolute path specified).
  238. */
  239. API_AVAILABLE(macos(10.7), ios(5.0))
  240. DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED
  241. DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  242. dispatch_io_t
  243. dispatch_io_create_with_path(dispatch_io_type_t type,
  244. const char *path, int oflag, mode_t mode,
  245. dispatch_queue_t queue,
  246. void (^cleanup_handler)(int error));
  247. /*!
  248. * @function dispatch_io_create_with_io
  249. * Create a new dispatch I/O channel from an existing dispatch I/O channel.
  250. * The new channel inherits the file descriptor or path name associated with
  251. * the existing channel, but not its channel type or policies.
  252. *
  253. * If the existing channel is associated with a file descriptor, control by the
  254. * system over that file descriptor is extended until the new channel is also
  255. * closed, an error occurs on the file descriptor, or all references to both
  256. * channels are released. At that time the specified cleanup handler will be
  257. * enqueued and control over the file descriptor relinquished.
  258. *
  259. * While a file descriptor is under the control of a dispatch I/O channel, file
  260. * descriptor flags such as O_NONBLOCK will be modified by the system on behalf
  261. * of the application. It is an error for the application to modify a file
  262. * descriptor directly while it is under the control of a dispatch I/O channel,
  263. * but it may create additional channels associated with that file descriptor.
  264. *
  265. * @param type The desired type of I/O channel (DISPATCH_IO_STREAM
  266. * or DISPATCH_IO_RANDOM).
  267. * @param io The existing channel to create the new I/O channel from.
  268. * @param queue The dispatch queue to which the handler should be submitted.
  269. * @param cleanup_handler The handler to enqueue when the system
  270. * relinquishes control over the file descriptor
  271. * (resp. closes the file at path) associated with
  272. * the existing channel.
  273. * param error An errno condition if control is relinquished
  274. * because channel creation failed, zero otherwise.
  275. * @result The newly created dispatch I/O channel or NULL if an error
  276. * occurred (invalid type specified).
  277. */
  278. API_AVAILABLE(macos(10.7), ios(5.0))
  279. DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED
  280. DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  281. dispatch_io_t
  282. dispatch_io_create_with_io(dispatch_io_type_t type,
  283. dispatch_io_t io,
  284. dispatch_queue_t queue,
  285. void (^cleanup_handler)(int error));
  286. /*!
  287. * @typedef dispatch_io_handler_t
  288. * The prototype of I/O handler blocks for dispatch I/O operations.
  289. *
  290. * @param done A flag indicating whether the operation is complete.
  291. * @param data The data object to be handled.
  292. * @param error An errno condition for the operation.
  293. */
  294. typedef void (^dispatch_io_handler_t)(bool done, dispatch_data_t _Nullable data,
  295. int error);
  296. /*!
  297. * @function dispatch_io_read
  298. * Schedule a read operation for asynchronous execution on the specified I/O
  299. * channel. The I/O handler is enqueued one or more times depending on the
  300. * general load of the system and the policy specified on the I/O channel.
  301. *
  302. * Any data read from the channel is described by the dispatch data object
  303. * passed to the I/O handler. This object will be automatically released by the
  304. * system when the I/O handler returns. It is the responsibility of the
  305. * application to retain, concatenate or copy the data object if it is needed
  306. * after the I/O handler returns.
  307. *
  308. * Dispatch I/O handlers are not reentrant. The system will ensure that no new
  309. * I/O handler instance is invoked until the previously enqueued handler block
  310. * has returned.
  311. *
  312. * An invocation of the I/O handler with the done flag set indicates that the
  313. * read operation is complete and that the handler will not be enqueued again.
  314. *
  315. * If an unrecoverable error occurs on the I/O channel's underlying file
  316. * descriptor, the I/O handler will be enqueued with the done flag set, the
  317. * appropriate error code and a NULL data object.
  318. *
  319. * An invocation of the I/O handler with the done flag set, an error code of
  320. * zero and an empty data object indicates that EOF was reached.
  321. *
  322. * @param channel The dispatch I/O channel from which to read the data.
  323. * @param offset The offset relative to the channel position from which
  324. * to start reading (only for DISPATCH_IO_RANDOM).
  325. * @param length The length of data to read from the I/O channel, or
  326. * SIZE_MAX to indicate that data should be read until EOF
  327. * is reached.
  328. * @param queue The dispatch queue to which the I/O handler should be
  329. * submitted.
  330. * @param io_handler The I/O handler to enqueue when data is ready to be
  331. * delivered.
  332. * param done A flag indicating whether the operation is complete.
  333. * param data An object with the data most recently read from the
  334. * I/O channel as part of this read operation, or NULL.
  335. * param error An errno condition for the read operation or zero if
  336. * the read was successful.
  337. */
  338. API_AVAILABLE(macos(10.7), ios(5.0))
  339. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL4 DISPATCH_NONNULL5
  340. DISPATCH_NOTHROW
  341. void
  342. dispatch_io_read(dispatch_io_t channel,
  343. off_t offset,
  344. size_t length,
  345. dispatch_queue_t queue,
  346. dispatch_io_handler_t io_handler);
  347. /*!
  348. * @function dispatch_io_write
  349. * Schedule a write operation for asynchronous execution on the specified I/O
  350. * channel. The I/O handler is enqueued one or more times depending on the
  351. * general load of the system and the policy specified on the I/O channel.
  352. *
  353. * Any data remaining to be written to the I/O channel is described by the
  354. * dispatch data object passed to the I/O handler. This object will be
  355. * automatically released by the system when the I/O handler returns. It is the
  356. * responsibility of the application to retain, concatenate or copy the data
  357. * object if it is needed after the I/O handler returns.
  358. *
  359. * Dispatch I/O handlers are not reentrant. The system will ensure that no new
  360. * I/O handler instance is invoked until the previously enqueued handler block
  361. * has returned.
  362. *
  363. * An invocation of the I/O handler with the done flag set indicates that the
  364. * write operation is complete and that the handler will not be enqueued again.
  365. *
  366. * If an unrecoverable error occurs on the I/O channel's underlying file
  367. * descriptor, the I/O handler will be enqueued with the done flag set, the
  368. * appropriate error code and an object containing the data that could not be
  369. * written.
  370. *
  371. * An invocation of the I/O handler with the done flag set and an error code of
  372. * zero indicates that the data was fully written to the channel.
  373. *
  374. * @param channel The dispatch I/O channel on which to write the data.
  375. * @param offset The offset relative to the channel position from which
  376. * to start writing (only for DISPATCH_IO_RANDOM).
  377. * @param data The data to write to the I/O channel. The data object
  378. * will be retained by the system until the write operation
  379. * is complete.
  380. * @param queue The dispatch queue to which the I/O handler should be
  381. * submitted.
  382. * @param io_handler The I/O handler to enqueue when data has been delivered.
  383. * param done A flag indicating whether the operation is complete.
  384. * param data An object of the data remaining to be
  385. * written to the I/O channel as part of this write
  386. * operation, or NULL.
  387. * param error An errno condition for the write operation or zero
  388. * if the write was successful.
  389. */
  390. API_AVAILABLE(macos(10.7), ios(5.0))
  391. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NONNULL4
  392. DISPATCH_NONNULL5 DISPATCH_NOTHROW
  393. void
  394. dispatch_io_write(dispatch_io_t channel,
  395. off_t offset,
  396. dispatch_data_t data,
  397. dispatch_queue_t queue,
  398. dispatch_io_handler_t io_handler);
  399. #endif /* __BLOCKS__ */
  400. /*!
  401. * @typedef dispatch_io_close_flags_t
  402. * The type of flags you can set on a dispatch_io_close() call
  403. *
  404. * @const DISPATCH_IO_STOP Stop outstanding operations on a channel when
  405. * the channel is closed.
  406. */
  407. #define DISPATCH_IO_STOP 0x1
  408. typedef unsigned long dispatch_io_close_flags_t;
  409. /*!
  410. * @function dispatch_io_close
  411. * Close the specified I/O channel to new read or write operations; scheduling
  412. * operations on a closed channel results in their handler returning an error.
  413. *
  414. * If the DISPATCH_IO_STOP flag is provided, the system will make a best effort
  415. * to interrupt any outstanding read and write operations on the I/O channel,
  416. * otherwise those operations will run to completion normally.
  417. * Partial results of read and write operations may be returned even after a
  418. * channel is closed with the DISPATCH_IO_STOP flag.
  419. * The final invocation of an I/O handler of an interrupted operation will be
  420. * passed an ECANCELED error code, as will the I/O handler of an operation
  421. * scheduled on a closed channel.
  422. *
  423. * @param channel The dispatch I/O channel to close.
  424. * @param flags The flags for the close operation.
  425. */
  426. API_AVAILABLE(macos(10.7), ios(5.0))
  427. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
  428. void
  429. dispatch_io_close(dispatch_io_t channel, dispatch_io_close_flags_t flags);
  430. #ifdef __BLOCKS__
  431. /*!
  432. * @function dispatch_io_barrier
  433. * Schedule a barrier operation on the specified I/O channel; all previously
  434. * scheduled operations on the channel will complete before the provided
  435. * barrier block is enqueued onto the global queue determined by the channel's
  436. * target queue, and no subsequently scheduled operations will start until the
  437. * barrier block has returned.
  438. *
  439. * If multiple channels are associated with the same file descriptor, a barrier
  440. * operation scheduled on any of these channels will act as a barrier across all
  441. * channels in question, i.e. all previously scheduled operations on any of the
  442. * channels will complete before the barrier block is enqueued, and no
  443. * operations subsequently scheduled on any of the channels will start until the
  444. * barrier block has returned.
  445. *
  446. * While the barrier block is running, it may safely operate on the channel's
  447. * underlying file descriptor with fsync(2), lseek(2) etc. (but not close(2)).
  448. *
  449. * @param channel The dispatch I/O channel to schedule the barrier on.
  450. * @param barrier The barrier block.
  451. */
  452. API_AVAILABLE(macos(10.7), ios(5.0))
  453. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  454. void
  455. dispatch_io_barrier(dispatch_io_t channel, dispatch_block_t barrier);
  456. #endif /* __BLOCKS__ */
  457. /*!
  458. * @function dispatch_io_get_descriptor
  459. * Returns the file descriptor underlying a dispatch I/O channel.
  460. *
  461. * Will return -1 for a channel closed with dispatch_io_close() and for a
  462. * channel associated with a path name that has not yet been open(2)ed.
  463. *
  464. * If called from a barrier block scheduled on a channel associated with a path
  465. * name that has not yet been open(2)ed, this will trigger the channel open(2)
  466. * operation and return the resulting file descriptor.
  467. *
  468. * @param channel The dispatch I/O channel to query.
  469. * @result The file descriptor underlying the channel, or -1.
  470. */
  471. API_AVAILABLE(macos(10.7), ios(5.0))
  472. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  473. dispatch_fd_t
  474. dispatch_io_get_descriptor(dispatch_io_t channel);
  475. /*!
  476. * @function dispatch_io_set_high_water
  477. * Set a high water mark on the I/O channel for all operations.
  478. *
  479. * The system will make a best effort to enqueue I/O handlers with partial
  480. * results as soon the number of bytes processed by an operation (i.e. read or
  481. * written) reaches the high water mark.
  482. *
  483. * The size of data objects passed to I/O handlers for this channel will never
  484. * exceed the specified high water mark.
  485. *
  486. * The default value for the high water mark is unlimited (i.e. SIZE_MAX).
  487. *
  488. * @param channel The dispatch I/O channel on which to set the policy.
  489. * @param high_water The number of bytes to use as a high water mark.
  490. */
  491. API_AVAILABLE(macos(10.7), ios(5.0))
  492. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
  493. void
  494. dispatch_io_set_high_water(dispatch_io_t channel, size_t high_water);
  495. /*!
  496. * @function dispatch_io_set_low_water
  497. * Set a low water mark on the I/O channel for all operations.
  498. *
  499. * The system will process (i.e. read or write) at least the low water mark
  500. * number of bytes for an operation before enqueueing I/O handlers with partial
  501. * results.
  502. *
  503. * The size of data objects passed to intermediate I/O handler invocations for
  504. * this channel (i.e. excluding the final invocation) will never be smaller than
  505. * the specified low water mark, except if the channel has an interval with the
  506. * DISPATCH_IO_STRICT_INTERVAL flag set or if EOF or an error was encountered.
  507. *
  508. * I/O handlers should be prepared to receive amounts of data significantly
  509. * larger than the low water mark in general. If an I/O handler requires
  510. * intermediate results of fixed size, set both the low and and the high water
  511. * mark to that size.
  512. *
  513. * The default value for the low water mark is unspecified, but must be assumed
  514. * to be such that intermediate handler invocations may occur.
  515. * If I/O handler invocations with partial results are not desired, set the
  516. * low water mark to SIZE_MAX.
  517. *
  518. * @param channel The dispatch I/O channel on which to set the policy.
  519. * @param low_water The number of bytes to use as a low water mark.
  520. */
  521. API_AVAILABLE(macos(10.7), ios(5.0))
  522. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
  523. void
  524. dispatch_io_set_low_water(dispatch_io_t channel, size_t low_water);
  525. /*!
  526. * @typedef dispatch_io_interval_flags_t
  527. * Type of flags to set on dispatch_io_set_interval()
  528. *
  529. * @const DISPATCH_IO_STRICT_INTERVAL Enqueue I/O handlers at a channel's
  530. * interval setting even if the amount of data ready to be delivered is inferior
  531. * to the low water mark (or zero).
  532. */
  533. #define DISPATCH_IO_STRICT_INTERVAL 0x1
  534. typedef unsigned long dispatch_io_interval_flags_t;
  535. /*!
  536. * @function dispatch_io_set_interval
  537. * Set a nanosecond interval at which I/O handlers are to be enqueued on the
  538. * I/O channel for all operations.
  539. *
  540. * This allows an application to receive periodic feedback on the progress of
  541. * read and write operations, e.g. for the purposes of displaying progress bars.
  542. *
  543. * If the amount of data ready to be delivered to an I/O handler at the interval
  544. * is inferior to the channel low water mark, the handler will only be enqueued
  545. * if the DISPATCH_IO_STRICT_INTERVAL flag is set.
  546. *
  547. * Note that the system may defer enqueueing interval I/O handlers by a small
  548. * unspecified amount of leeway in order to align with other system activity for
  549. * improved system performance or power consumption.
  550. *
  551. * @param channel The dispatch I/O channel on which to set the policy.
  552. * @param interval The interval in nanoseconds at which delivery of the I/O
  553. * handler is desired.
  554. * @param flags Flags indicating desired data delivery behavior at
  555. * interval time.
  556. */
  557. API_AVAILABLE(macos(10.7), ios(5.0))
  558. DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
  559. void
  560. dispatch_io_set_interval(dispatch_io_t channel,
  561. uint64_t interval,
  562. dispatch_io_interval_flags_t flags);
  563. __END_DECLS
  564. DISPATCH_ASSUME_NONNULL_END
  565. #endif /* __DISPATCH_IO__ */