2 * Copyright (c) 2009-2010 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
21 #ifndef __DISPATCH_IO__
22 #define __DISPATCH_IO__
24 #ifndef __DISPATCH_INDIRECT__
25 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
26 #include <dispatch/base.h> // for HeaderDoc
32 * Dispatch I/O provides both stream and random access asynchronous read and
33 * write operations on file descriptors. One or more dispatch I/O channels may
34 * be created from a file descriptor as either the DISPATCH_IO_STREAM type or
35 * DISPATCH_IO_RANDOM type. Once a channel has been created the application may
36 * schedule asynchronous read and write operations.
38 * The application may set policies on the dispatch I/O channel to indicate the
39 * desired frequency of I/O handlers for long-running operations.
41 * Dispatch I/O also provides a memory managment model for I/O buffers that
42 * avoids unnecessary copying of data when pipelined between channels. Dispatch
43 * I/O monitors the overall memory pressure and I/O access patterns for the
44 * application to optimize resource utilization.
48 * @typedef dispatch_fd_t
49 * Native file descriptor type for the platform.
51 typedef int dispatch_fd_t
;
56 * @functiongroup Dispatch I/O Convenience API
57 * Convenience wrappers around the dispatch I/O channel API, with simpler
58 * callback handler semantics and no explicit management of channel objects.
59 * File descriptors passed to the convenience API are treated as streams, and
60 * scheduling multiple operations on one file descriptor via the convenience API
61 * may incur more overhead than by using the dispatch I/O channel API directly.
65 * @function dispatch_read
66 * Schedule a read operation for asynchronous execution on the specified file
67 * descriptor. The specified handler is enqueued with the data read from the
68 * file descriptor when the operation has completed or an error occurs.
70 * The data object passed to the handler will be automatically released by the
71 * system when the handler returns. It is the responsibility of the application
72 * to retain, concatenate or copy the data object if it is needed after the
75 * The data object passed to the handler will only contain as much data as is
76 * currently available from the file descriptor (up to the specified length).
78 * If an unrecoverable error occurs on the file descriptor, the handler will be
79 * enqueued with the appropriate error code along with a data object of any data
80 * that could be read successfully.
82 * An invocation of the handler with an error code of zero and an empty data
83 * object indicates that EOF was reached.
85 * The system takes control of the file descriptor until the handler is
86 * enqueued, and during this time file descriptor flags such as O_NONBLOCK will
87 * be modified by the system on behalf of the application. It is an error for
88 * the application to modify a file descriptor directly while it is under the
89 * control of the system, but it may create additional dispatch I/O convenience
90 * operations or dispatch I/O channels associated with that file descriptor.
92 * @param fd The file descriptor from which to read the data.
93 * @param length The length of data to read from the file descriptor,
94 * or SIZE_MAX to indicate that all of the data currently
95 * available from the file descriptor should be read.
96 * @param queue The dispatch queue to which the handler should be
98 * @param handler The handler to enqueue when data is ready to be
100 * @param data The data read from the file descriptor.
101 * @param error An errno condition for the read operation or
102 * zero if the read was successful.
104 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
105 DISPATCH_EXPORT DISPATCH_NONNULL3 DISPATCH_NONNULL4 DISPATCH_NOTHROW
107 dispatch_read(dispatch_fd_t fd
,
109 dispatch_queue_t queue
,
110 void (^handler
)(dispatch_data_t data
, int error
));
113 * @function dispatch_write
114 * Schedule a write operation for asynchronous execution on the specified file
115 * descriptor. The specified handler is enqueued when the operation has
116 * completed or an error occurs.
118 * If an unrecoverable error occurs on the file descriptor, the handler will be
119 * enqueued with the appropriate error code along with the data that could not
120 * be successfully written.
122 * An invocation of the handler with an error code of zero indicates that the
123 * data was fully written to the channel.
125 * The system takes control of the file descriptor until the handler is
126 * enqueued, and during this time file descriptor flags such as O_NONBLOCK will
127 * be modified by the system on behalf of the application. It is an error for
128 * the application to modify a file descriptor directly while it is under the
129 * control of the system, but it may create additional dispatch I/O convenience
130 * operations or dispatch I/O channels associated with that file descriptor.
132 * @param fd The file descriptor to which to write the data.
133 * @param data The data object to write to the file descriptor.
134 * @param queue The dispatch queue to which the handler should be
136 * @param handler The handler to enqueue when the data has been written.
137 * @param data The data that could not be written to the I/O
139 * @param error An errno condition for the write operation or
140 * zero if the write was successful.
142 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
143 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_NONNULL3 DISPATCH_NONNULL4
146 dispatch_write(dispatch_fd_t fd
,
147 dispatch_data_t data
,
148 dispatch_queue_t queue
,
149 void (^handler
)(dispatch_data_t data
, int error
));
152 * @functiongroup Dispatch I/O Channel API
156 * @typedef dispatch_io_t
157 * A dispatch I/O channel represents the asynchronous I/O policy applied to a
158 * file descriptor. I/O channels are first class dispatch objects and may be
159 * retained and released, suspended and resumed, etc.
161 DISPATCH_DECL(dispatch_io
);
164 * @typedef dispatch_io_handler_t
165 * The prototype of I/O handler blocks for dispatch I/O operations.
167 * @param done A flag indicating whether the operation is complete.
168 * @param data The data object to be handled.
169 * @param error An errno condition for the operation.
171 typedef void (^dispatch_io_handler_t
)(bool done
, dispatch_data_t data
,
175 * @typedef dispatch_io_type_t
176 * The type of a dispatch I/O channel:
178 * @const DISPATCH_IO_STREAM A dispatch I/O channel representing a stream of
179 * bytes. Read and write operations on a channel of this type are performed
180 * serially (in order of creation) and read/write data at the file pointer
181 * position that is current at the time the operation starts executing.
182 * Operations of different type (read vs. write) may be perfomed simultaneously.
183 * Offsets passed to operations on a channel of this type are ignored.
185 * @const DISPATCH_IO_RANDOM A dispatch I/O channel representing a random
186 * access file. Read and write operations on a channel of this type may be
187 * performed concurrently and read/write data at the specified offset. Offsets
188 * are interpreted relative to the file pointer position current at the time the
189 * I/O channel is created. Attempting to create a channel of this type for a
190 * file descriptor that is not seekable will result in an error.
192 #define DISPATCH_IO_STREAM 0
193 #define DISPATCH_IO_RANDOM 1
195 typedef unsigned long dispatch_io_type_t
;
198 * @function dispatch_io_create
199 * Create a dispatch I/O channel associated with a file descriptor. The system
200 * takes control of the file descriptor until the channel is closed, an error
201 * occurs on the file descriptor or all references to the channel are released.
202 * At that time the specified cleanup handler will be enqueued and control over
203 * the file descriptor relinquished.
205 * While a file descriptor is under the control of a dispatch I/O channel, file
206 * descriptor flags such as O_NONBLOCK will be modified by the system on behalf
207 * of the application. It is an error for the application to modify a file
208 * descriptor directly while it is under the control of a dispatch I/O channel,
209 * but it may create additional channels associated with that file descriptor.
211 * @param type The desired type of I/O channel (DISPATCH_IO_STREAM
212 * or DISPATCH_IO_RANDOM).
213 * @param fd The file descriptor to associate with the I/O channel.
214 * @param queue The dispatch queue to which the handler should be submitted.
215 * @param cleanup_handler The handler to enqueue when the system
216 * relinquishes control over the file descriptor.
217 * @param error An errno condition if control is relinquished
218 * because channel creation failed, zero otherwise.
219 * @result The newly created dispatch I/O channel or NULL if an error
222 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
223 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_WARN_RESULT DISPATCH_NOTHROW
225 dispatch_io_create(dispatch_io_type_t type
,
227 dispatch_queue_t queue
,
228 void (^cleanup_handler
)(int error
));
231 * @function dispatch_io_create_with_path
232 * Create a dispatch I/O channel associated with a path name. The specified
233 * path, oflag and mode parameters will be passed to open(2) when the first I/O
234 * operation on the channel is ready to execute and the resulting file
235 * descriptor will remain open and under the control of the system until the
236 * channel is closed, an error occurs on the file descriptor or all references
237 * to the channel are released. At that time the file descriptor will be closed
238 * and the specified cleanup handler will be enqueued.
240 * @param type The desired type of I/O channel (DISPATCH_IO_STREAM
241 * or DISPATCH_IO_RANDOM).
242 * @param path The path to associate with the I/O channel.
243 * @param oflag The flags to pass to open(2) when opening the file at
245 * @param mode The mode to pass to open(2) when creating the file at
246 * path (i.e. with flag O_CREAT), zero otherwise.
247 * @param queue The dispatch queue to which the handler should be
249 * @param cleanup_handler The handler to enqueue when the system
250 * has closed the file at path.
251 * @param error An errno condition if control is relinquished
252 * because channel creation or opening of the
253 * specified file failed, zero otherwise.
254 * @result The newly created dispatch I/O channel or NULL if an error
257 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
258 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_NONNULL2 DISPATCH_WARN_RESULT
261 dispatch_io_create_with_path(dispatch_io_type_t type
,
262 const char *path
, int oflag
, mode_t mode
,
263 dispatch_queue_t queue
,
264 void (^cleanup_handler
)(int error
));
267 * @function dispatch_io_create_with_io
268 * Create a new dispatch I/O channel from an existing dispatch I/O channel.
269 * The new channel inherits the file descriptor or path name associated with
270 * the existing channel, but not its channel type or policies.
272 * If the existing channel is associated with a file descriptor, control by the
273 * system over that file descriptor is extended until the new channel is also
274 * closed, an error occurs on the file descriptor, or all references to both
275 * channels are released. At that time the specified cleanup handler will be
276 * enqueued and control over the file descriptor relinquished.
278 * While a file descriptor is under the control of a dispatch I/O channel, file
279 * descriptor flags such as O_NONBLOCK will be modified by the system on behalf
280 * of the application. It is an error for the application to modify a file
281 * descriptor directly while it is under the control of a dispatch I/O channel,
282 * but it may create additional channels associated with that file descriptor.
284 * @param type The desired type of I/O channel (DISPATCH_IO_STREAM
285 * or DISPATCH_IO_RANDOM).
286 * @param io The existing channel to create the new I/O channel from.
287 * @param queue The dispatch queue to which the handler should be submitted.
288 * @param cleanup_handler The handler to enqueue when the system
289 * relinquishes control over the file descriptor
290 * (resp. closes the file at path) associated with
291 * the existing channel.
292 * @param error An errno condition if control is relinquished
293 * because channel creation failed, zero otherwise.
294 * @result The newly created dispatch I/O channel or NULL if an error
297 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
298 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_MALLOC DISPATCH_WARN_RESULT
301 dispatch_io_create_with_io(dispatch_io_type_t type
,
303 dispatch_queue_t queue
,
304 void (^cleanup_handler
)(int error
));
307 * @function dispatch_io_read
308 * Schedule a read operation for asynchronous execution on the specified I/O
309 * channel. The I/O handler is enqueued one or more times depending on the
310 * general load of the system and the policy specified on the I/O channel.
312 * Any data read from the channel is described by the dispatch data object
313 * passed to the I/O handler. This object will be automatically released by the
314 * system when the I/O handler returns. It is the responsibility of the
315 * application to retain, concatenate or copy the data object if it is needed
316 * after the I/O handler returns.
318 * Dispatch I/O handlers are not reentrant. The system will ensure that no new
319 * I/O handler instance is invoked until the previously enqueued handler block
322 * An invocation of the I/O handler with the done flag set indicates that the
323 * read operation is complete and that the handler will not be enqueued again.
325 * If an unrecoverable error occurs on the I/O channel's underlying file
326 * descriptor, the I/O handler will be enqueued with the done flag set, the
327 * appropriate error code and a NULL data object.
329 * An invocation of the I/O handler with the done flag set, an error code of
330 * zero and an empty data object indicates that EOF was reached.
332 * @param channel The dispatch I/O channel from which to read the data.
333 * @param offset The offset relative to the channel position from which
334 * to start reading (only for DISPATCH_IO_RANDOM).
335 * @param length The length of data to read from the I/O channel, or
336 * SIZE_MAX to indicate that data should be read until EOF
338 * @param queue The dispatch queue to which the I/O handler should be
340 * @param io_handler The I/O handler to enqueue when data is ready to be
342 * @param done A flag indicating whether the operation is complete.
343 * @param data An object with the data most recently read from the
344 * I/O channel as part of this read operation, or NULL.
345 * @param error An errno condition for the read operation or zero if
346 * the read was successful.
348 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
349 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL4 DISPATCH_NONNULL5
352 dispatch_io_read(dispatch_io_t channel
,
355 dispatch_queue_t queue
,
356 dispatch_io_handler_t io_handler
);
359 * @function dispatch_io_write
360 * Schedule a write operation for asynchronous execution on the specified I/O
361 * channel. The I/O handler is enqueued one or more times depending on the
362 * general load of the system and the policy specified on the I/O channel.
364 * Any data remaining to be written to the I/O channel is described by the
365 * dispatch data object passed to the I/O handler. This object will be
366 * automatically released by the system when the I/O handler returns. It is the
367 * responsibility of the application to retain, concatenate or copy the data
368 * object if it is needed after the I/O handler returns.
370 * Dispatch I/O handlers are not reentrant. The system will ensure that no new
371 * I/O handler instance is invoked until the previously enqueued handler block
374 * An invocation of the I/O handler with the done flag set indicates that the
375 * write operation is complete and that the handler will not be enqueued again.
377 * If an unrecoverable error occurs on the I/O channel's underlying file
378 * descriptor, the I/O handler will be enqueued with the done flag set, the
379 * appropriate error code and an object containing the data that could not be
382 * An invocation of the I/O handler with the done flag set and an error code of
383 * zero indicates that the data was fully written to the channel.
385 * @param channel The dispatch I/O channel on which to write the data.
386 * @param offset The offset relative to the channel position from which
387 * to start writing (only for DISPATCH_IO_RANDOM).
388 * @param data The data to write to the I/O channel. The data object
389 * will be retained by the system until the write operation
391 * @param queue The dispatch queue to which the I/O handler should be
393 * @param io_handler The I/O handler to enqueue when data has been delivered.
394 * @param done A flag indicating whether the operation is complete.
395 * @param data An object of the data remaining to be
396 * written to the I/O channel as part of this write
397 * operation, or NULL.
398 * @param error An errno condition for the write operation or zero
399 * if the write was successful.
401 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
402 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NONNULL4
403 DISPATCH_NONNULL5 DISPATCH_NOTHROW
405 dispatch_io_write(dispatch_io_t channel
,
407 dispatch_data_t data
,
408 dispatch_queue_t queue
,
409 dispatch_io_handler_t io_handler
);
412 * @typedef dispatch_io_close_flags_t
413 * The type of flags you can set on a dispatch_io_close() call
415 * @const DISPATCH_IO_STOP Stop outstanding operations on a channel when
416 * the channel is closed.
418 #define DISPATCH_IO_STOP 0x1
420 typedef unsigned long dispatch_io_close_flags_t
;
423 * @function dispatch_io_close
424 * Close the specified I/O channel to new read or write operations; scheduling
425 * operations on a closed channel results in their handler returning an error.
427 * If the DISPATCH_IO_STOP flag is provided, the system will make a best effort
428 * to interrupt any outstanding read and write operations on the I/O channel,
429 * otherwise those operations will run to completion normally.
430 * Partial results of read and write operations may be returned even after a
431 * channel is closed with the DISPATCH_IO_STOP flag.
432 * The final invocation of an I/O handler of an interrupted operation will be
433 * passed an ECANCELED error code, as will the I/O handler of an operation
434 * scheduled on a closed channel.
436 * @param channel The dispatch I/O channel to close.
437 * @param flags The flags for the close operation.
439 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
440 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
442 dispatch_io_close(dispatch_io_t channel
, dispatch_io_close_flags_t flags
);
445 * @function dispatch_io_barrier
446 * Schedule a barrier operation on the specified I/O channel; all previously
447 * scheduled operations on the channel will complete before the provided
448 * barrier block is enqueued onto the global queue determined by the channel's
449 * target queue, and no subsequently scheduled operations will start until the
450 * barrier block has returned.
452 * If multiple channels are associated with the same file descriptor, a barrier
453 * operation scheduled on any of these channels will act as a barrier across all
454 * channels in question, i.e. all previously scheduled operations on any of the
455 * channels will complete before the barrier block is enqueued, and no
456 * operations subsequently scheduled on any of the channels will start until the
457 * barrier block has returned.
459 * While the barrier block is running, it may safely operate on the channel's
460 * underlying file descriptor with fsync(2), lseek(2) etc. (but not close(2)).
462 * @param channel The dispatch I/O channel to close.
463 * @param barrier The flags for the close operation.
465 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
466 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
468 dispatch_io_barrier(dispatch_io_t channel
, dispatch_block_t barrier
);
471 * @function dispatch_io_get_descriptor
472 * Returns the file descriptor underlying a dispatch I/O channel.
474 * Will return -1 for a channel closed with dispatch_io_close() and for a
475 * channel associated with a path name that has not yet been open(2)ed.
477 * If called from a barrier block scheduled on a channel associated with a path
478 * name that has not yet been open(2)ed, this will trigger the channel open(2)
479 * operation and return the resulting file descriptor.
481 * @param channel The dispatch I/O channel to query.
482 * @result The file descriptor underlying the channel, or -1.
484 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
485 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_NOTHROW
487 dispatch_io_get_descriptor(dispatch_io_t channel
);
490 * @function dispatch_io_set_high_water
491 * Set a high water mark on the I/O channel for all operations.
493 * The system will make a best effort to enqueue I/O handlers with partial
494 * results as soon the number of bytes processed by an operation (i.e. read or
495 * written) reaches the high water mark.
497 * The size of data objects passed to I/O handlers for this channel will never
498 * exceed the specified high water mark.
500 * The default value for the high water mark is unlimited (i.e. SIZE_MAX).
502 * @param channel The dispatch I/O channel on which to set the policy.
503 * @param high_water The number of bytes to use as a high water mark.
505 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
506 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
508 dispatch_io_set_high_water(dispatch_io_t channel
, size_t high_water
);
511 * @function dispatch_io_set_low_water
512 * Set a low water mark on the I/O channel for all operations.
514 * The system will process (i.e. read or write) at least the low water mark
515 * number of bytes for an operation before enqueueing I/O handlers with partial
518 * The size of data objects passed to intermediate I/O handler invocations for
519 * this channel (i.e. excluding the final invocation) will never be smaller than
520 * the specified low water mark, except if the channel has an interval with the
521 * DISPATCH_IO_STRICT_INTERVAL flag set or if EOF or an error was encountered.
523 * I/O handlers should be prepared to receive amounts of data significantly
524 * larger than the low water mark in general. If an I/O handler requires
525 * intermediate results of fixed size, set both the low and and the high water
528 * The default value for the low water mark is unspecified, but must be assumed
529 * to be such that intermediate handler invocations may occur.
530 * If I/O handler invocations with partial results are not desired, set the
531 * low water mark to SIZE_MAX.
533 * @param channel The dispatch I/O channel on which to set the policy.
534 * @param low_water The number of bytes to use as a low water mark.
536 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
537 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
539 dispatch_io_set_low_water(dispatch_io_t channel
, size_t low_water
);
542 * @typedef dispatch_io_interval_flags_t
543 * Type of flags to set on dispatch_io_set_interval()
545 * @const DISPATCH_IO_STRICT_INTERVAL Enqueue I/O handlers at a channel's
546 * interval setting even if the amount of data ready to be delivered is inferior
547 * to the low water mark (or zero).
549 #define DISPATCH_IO_STRICT_INTERVAL 0x1
551 typedef unsigned long dispatch_io_interval_flags_t
;
554 * @function dispatch_io_set_interval
555 * Set a nanosecond interval at which I/O handlers are to be enqueued on the
556 * I/O channel for all operations.
558 * This allows an application to receive periodic feedback on the progress of
559 * read and write operations, e.g. for the purposes of displaying progress bars.
561 * If the amount of data ready to be delivered to an I/O handler at the interval
562 * is inferior to the channel low water mark, the handler will only be enqueued
563 * if the DISPATCH_IO_STRICT_INTERVAL flag is set.
565 * Note that the system may defer enqueueing interval I/O handlers by a small
566 * unspecified amount of leeway in order to align with other system activity for
567 * improved system performance or power consumption.
569 * @param channel The dispatch I/O channel on which to set the policy.
570 * @param interval The interval in nanoseconds at which delivery of the I/O
571 * handler is desired.
572 * @param flags Flags indicating desired data delivery behavior at
575 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
576 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
578 dispatch_io_set_interval(dispatch_io_t channel
,
580 dispatch_io_interval_flags_t flags
);
582 #endif /* __BLOCKS__ */
586 #endif /* __DISPATCH_IO__ */