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_RETURNS_RETAINED DISPATCH_WARN_RESULT
226 dispatch_io_create(dispatch_io_type_t type
,
228 dispatch_queue_t queue
,
229 void (^cleanup_handler
)(int error
));
232 * @function dispatch_io_create_with_path
233 * Create a dispatch I/O channel associated with a path name. The specified
234 * path, oflag and mode parameters will be passed to open(2) when the first I/O
235 * operation on the channel is ready to execute and the resulting file
236 * descriptor will remain open and under the control of the system until the
237 * channel is closed, an error occurs on the file descriptor or all references
238 * to the channel are released. At that time the file descriptor will be closed
239 * and the specified cleanup handler will be enqueued.
241 * @param type The desired type of I/O channel (DISPATCH_IO_STREAM
242 * or DISPATCH_IO_RANDOM).
243 * @param path The path to associate with the I/O channel.
244 * @param oflag The flags to pass to open(2) when opening the file at
246 * @param mode The mode to pass to open(2) when creating the file at
247 * path (i.e. with flag O_CREAT), zero otherwise.
248 * @param queue The dispatch queue to which the handler should be
250 * @param cleanup_handler The handler to enqueue when the system
251 * has closed the file at path.
252 * @param error An errno condition if control is relinquished
253 * because channel creation or opening of the
254 * specified file failed, zero otherwise.
255 * @result The newly created dispatch I/O channel or NULL if an error
258 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
259 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED
260 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
262 dispatch_io_create_with_path(dispatch_io_type_t type
,
263 const char *path
, int oflag
, mode_t mode
,
264 dispatch_queue_t queue
,
265 void (^cleanup_handler
)(int error
));
268 * @function dispatch_io_create_with_io
269 * Create a new dispatch I/O channel from an existing dispatch I/O channel.
270 * The new channel inherits the file descriptor or path name associated with
271 * the existing channel, but not its channel type or policies.
273 * If the existing channel is associated with a file descriptor, control by the
274 * system over that file descriptor is extended until the new channel is also
275 * closed, an error occurs on the file descriptor, or all references to both
276 * channels are released. At that time the specified cleanup handler will be
277 * enqueued and control over the file descriptor relinquished.
279 * While a file descriptor is under the control of a dispatch I/O channel, file
280 * descriptor flags such as O_NONBLOCK will be modified by the system on behalf
281 * of the application. It is an error for the application to modify a file
282 * descriptor directly while it is under the control of a dispatch I/O channel,
283 * but it may create additional channels associated with that file descriptor.
285 * @param type The desired type of I/O channel (DISPATCH_IO_STREAM
286 * or DISPATCH_IO_RANDOM).
287 * @param io The existing channel to create the new I/O channel from.
288 * @param queue The dispatch queue to which the handler should be submitted.
289 * @param cleanup_handler The handler to enqueue when the system
290 * relinquishes control over the file descriptor
291 * (resp. closes the file at path) associated with
292 * the existing channel.
293 * @param error An errno condition if control is relinquished
294 * because channel creation failed, zero otherwise.
295 * @result The newly created dispatch I/O channel or NULL if an error
298 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
299 DISPATCH_EXPORT DISPATCH_NONNULL2 DISPATCH_MALLOC DISPATCH_RETURNS_RETAINED
300 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
302 dispatch_io_create_with_io(dispatch_io_type_t type
,
304 dispatch_queue_t queue
,
305 void (^cleanup_handler
)(int error
));
308 * @function dispatch_io_read
309 * Schedule a read operation for asynchronous execution on the specified I/O
310 * channel. The I/O handler is enqueued one or more times depending on the
311 * general load of the system and the policy specified on the I/O channel.
313 * Any data read from the channel is described by the dispatch data object
314 * passed to the I/O handler. This object will be automatically released by the
315 * system when the I/O handler returns. It is the responsibility of the
316 * application to retain, concatenate or copy the data object if it is needed
317 * after the I/O handler returns.
319 * Dispatch I/O handlers are not reentrant. The system will ensure that no new
320 * I/O handler instance is invoked until the previously enqueued handler block
323 * An invocation of the I/O handler with the done flag set indicates that the
324 * read operation is complete and that the handler will not be enqueued again.
326 * If an unrecoverable error occurs on the I/O channel's underlying file
327 * descriptor, the I/O handler will be enqueued with the done flag set, the
328 * appropriate error code and a NULL data object.
330 * An invocation of the I/O handler with the done flag set, an error code of
331 * zero and an empty data object indicates that EOF was reached.
333 * @param channel The dispatch I/O channel from which to read the data.
334 * @param offset The offset relative to the channel position from which
335 * to start reading (only for DISPATCH_IO_RANDOM).
336 * @param length The length of data to read from the I/O channel, or
337 * SIZE_MAX to indicate that data should be read until EOF
339 * @param queue The dispatch queue to which the I/O handler should be
341 * @param io_handler The I/O handler to enqueue when data is ready to be
343 * @param done A flag indicating whether the operation is complete.
344 * @param data An object with the data most recently read from the
345 * I/O channel as part of this read operation, or NULL.
346 * @param error An errno condition for the read operation or zero if
347 * the read was successful.
349 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
350 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL4 DISPATCH_NONNULL5
353 dispatch_io_read(dispatch_io_t channel
,
356 dispatch_queue_t queue
,
357 dispatch_io_handler_t io_handler
);
360 * @function dispatch_io_write
361 * Schedule a write operation for asynchronous execution on the specified I/O
362 * channel. The I/O handler is enqueued one or more times depending on the
363 * general load of the system and the policy specified on the I/O channel.
365 * Any data remaining to be written to the I/O channel is described by the
366 * dispatch data object passed to the I/O handler. This object will be
367 * automatically released by the system when the I/O handler returns. It is the
368 * responsibility of the application to retain, concatenate or copy the data
369 * object if it is needed after the I/O handler returns.
371 * Dispatch I/O handlers are not reentrant. The system will ensure that no new
372 * I/O handler instance is invoked until the previously enqueued handler block
375 * An invocation of the I/O handler with the done flag set indicates that the
376 * write operation is complete and that the handler will not be enqueued again.
378 * If an unrecoverable error occurs on the I/O channel's underlying file
379 * descriptor, the I/O handler will be enqueued with the done flag set, the
380 * appropriate error code and an object containing the data that could not be
383 * An invocation of the I/O handler with the done flag set and an error code of
384 * zero indicates that the data was fully written to the channel.
386 * @param channel The dispatch I/O channel on which to write the data.
387 * @param offset The offset relative to the channel position from which
388 * to start writing (only for DISPATCH_IO_RANDOM).
389 * @param data The data to write to the I/O channel. The data object
390 * will be retained by the system until the write operation
392 * @param queue The dispatch queue to which the I/O handler should be
394 * @param io_handler The I/O handler to enqueue when data has been delivered.
395 * @param done A flag indicating whether the operation is complete.
396 * @param data An object of the data remaining to be
397 * written to the I/O channel as part of this write
398 * operation, or NULL.
399 * @param error An errno condition for the write operation or zero
400 * if the write was successful.
402 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
403 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_NONNULL4
404 DISPATCH_NONNULL5 DISPATCH_NOTHROW
406 dispatch_io_write(dispatch_io_t channel
,
408 dispatch_data_t data
,
409 dispatch_queue_t queue
,
410 dispatch_io_handler_t io_handler
);
413 * @typedef dispatch_io_close_flags_t
414 * The type of flags you can set on a dispatch_io_close() call
416 * @const DISPATCH_IO_STOP Stop outstanding operations on a channel when
417 * the channel is closed.
419 #define DISPATCH_IO_STOP 0x1
421 typedef unsigned long dispatch_io_close_flags_t
;
424 * @function dispatch_io_close
425 * Close the specified I/O channel to new read or write operations; scheduling
426 * operations on a closed channel results in their handler returning an error.
428 * If the DISPATCH_IO_STOP flag is provided, the system will make a best effort
429 * to interrupt any outstanding read and write operations on the I/O channel,
430 * otherwise those operations will run to completion normally.
431 * Partial results of read and write operations may be returned even after a
432 * channel is closed with the DISPATCH_IO_STOP flag.
433 * The final invocation of an I/O handler of an interrupted operation will be
434 * passed an ECANCELED error code, as will the I/O handler of an operation
435 * scheduled on a closed channel.
437 * @param channel The dispatch I/O channel to close.
438 * @param flags The flags for the close operation.
440 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
441 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
443 dispatch_io_close(dispatch_io_t channel
, dispatch_io_close_flags_t flags
);
446 * @function dispatch_io_barrier
447 * Schedule a barrier operation on the specified I/O channel; all previously
448 * scheduled operations on the channel will complete before the provided
449 * barrier block is enqueued onto the global queue determined by the channel's
450 * target queue, and no subsequently scheduled operations will start until the
451 * barrier block has returned.
453 * If multiple channels are associated with the same file descriptor, a barrier
454 * operation scheduled on any of these channels will act as a barrier across all
455 * channels in question, i.e. all previously scheduled operations on any of the
456 * channels will complete before the barrier block is enqueued, and no
457 * operations subsequently scheduled on any of the channels will start until the
458 * barrier block has returned.
460 * While the barrier block is running, it may safely operate on the channel's
461 * underlying file descriptor with fsync(2), lseek(2) etc. (but not close(2)).
463 * @param channel The dispatch I/O channel to close.
464 * @param barrier The flags for the close operation.
466 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
467 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
469 dispatch_io_barrier(dispatch_io_t channel
, dispatch_block_t barrier
);
472 * @function dispatch_io_get_descriptor
473 * Returns the file descriptor underlying a dispatch I/O channel.
475 * Will return -1 for a channel closed with dispatch_io_close() and for a
476 * channel associated with a path name that has not yet been open(2)ed.
478 * If called from a barrier block scheduled on a channel associated with a path
479 * name that has not yet been open(2)ed, this will trigger the channel open(2)
480 * operation and return the resulting file descriptor.
482 * @param channel The dispatch I/O channel to query.
483 * @result The file descriptor underlying the channel, or -1.
485 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
486 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_NOTHROW
488 dispatch_io_get_descriptor(dispatch_io_t channel
);
491 * @function dispatch_io_set_high_water
492 * Set a high water mark on the I/O channel for all operations.
494 * The system will make a best effort to enqueue I/O handlers with partial
495 * results as soon the number of bytes processed by an operation (i.e. read or
496 * written) reaches the high water mark.
498 * The size of data objects passed to I/O handlers for this channel will never
499 * exceed the specified high water mark.
501 * The default value for the high water mark is unlimited (i.e. SIZE_MAX).
503 * @param channel The dispatch I/O channel on which to set the policy.
504 * @param high_water The number of bytes to use as a high water mark.
506 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
507 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
509 dispatch_io_set_high_water(dispatch_io_t channel
, size_t high_water
);
512 * @function dispatch_io_set_low_water
513 * Set a low water mark on the I/O channel for all operations.
515 * The system will process (i.e. read or write) at least the low water mark
516 * number of bytes for an operation before enqueueing I/O handlers with partial
519 * The size of data objects passed to intermediate I/O handler invocations for
520 * this channel (i.e. excluding the final invocation) will never be smaller than
521 * the specified low water mark, except if the channel has an interval with the
522 * DISPATCH_IO_STRICT_INTERVAL flag set or if EOF or an error was encountered.
524 * I/O handlers should be prepared to receive amounts of data significantly
525 * larger than the low water mark in general. If an I/O handler requires
526 * intermediate results of fixed size, set both the low and and the high water
529 * The default value for the low water mark is unspecified, but must be assumed
530 * to be such that intermediate handler invocations may occur.
531 * If I/O handler invocations with partial results are not desired, set the
532 * low water mark to SIZE_MAX.
534 * @param channel The dispatch I/O channel on which to set the policy.
535 * @param low_water The number of bytes to use as a low water mark.
537 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
538 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
540 dispatch_io_set_low_water(dispatch_io_t channel
, size_t low_water
);
543 * @typedef dispatch_io_interval_flags_t
544 * Type of flags to set on dispatch_io_set_interval()
546 * @const DISPATCH_IO_STRICT_INTERVAL Enqueue I/O handlers at a channel's
547 * interval setting even if the amount of data ready to be delivered is inferior
548 * to the low water mark (or zero).
550 #define DISPATCH_IO_STRICT_INTERVAL 0x1
552 typedef unsigned long dispatch_io_interval_flags_t
;
555 * @function dispatch_io_set_interval
556 * Set a nanosecond interval at which I/O handlers are to be enqueued on the
557 * I/O channel for all operations.
559 * This allows an application to receive periodic feedback on the progress of
560 * read and write operations, e.g. for the purposes of displaying progress bars.
562 * If the amount of data ready to be delivered to an I/O handler at the interval
563 * is inferior to the channel low water mark, the handler will only be enqueued
564 * if the DISPATCH_IO_STRICT_INTERVAL flag is set.
566 * Note that the system may defer enqueueing interval I/O handlers by a small
567 * unspecified amount of leeway in order to align with other system activity for
568 * improved system performance or power consumption.
570 * @param channel The dispatch I/O channel on which to set the policy.
571 * @param interval The interval in nanoseconds at which delivery of the I/O
572 * handler is desired.
573 * @param flags Flags indicating desired data delivery behavior at
576 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_5_0
)
577 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
579 dispatch_io_set_interval(dispatch_io_t channel
,
581 dispatch_io_interval_flags_t flags
);
583 #endif /* __BLOCKS__ */
587 #endif /* __DISPATCH_IO__ */