1 .\" Copyright (c) 2010 Apple Inc. All rights reserved.
8 .Nd submit read and write operations to dispatch I/O channels
10 .Fd #include <dispatch/dispatch.h>
13 .Fa "dispatch_io_t channel"
16 .Fa "dispatch_queue_t queue"
17 .Fa "void (^handler)(bool done, dispatch_data_t data, int error)"
21 .Fa "dispatch_io_t channel"
23 .Fa "dispatch_data_t dispatch"
24 .Fa "dispatch_queue_t queue"
25 .Fa "void (^handler)(bool done, dispatch_data_t data, int error)"
28 The dispatch I/O framework is an API for asynchronous read and write I/O
29 operations. It is an application of the ideas and idioms present in the
31 framework to device I/O. Dispatch I/O enables an application to more easily
32 avoid blocking I/O operations and allows it to more directly express its I/O
33 requirements than by using the raw POSIX file API. Dispatch I/O will make a
34 best effort to optimize how and when asynchronous I/O operations are performed
35 based on the capabilities of the targeted device.
37 This page provides details on how to read from and write to dispatch I/O
38 channels. Creation and configuration of these channels is covered in the
39 .Xr dispatch_io_create 3
40 page. The dispatch I/O framework also provides the convenience functions
44 for uses that do not require the full functionality provided by I/O channels.
51 functions are used to perform asynchronous read and write operations on
52 dispatch I/O channels. They can be thought of as asynchronous versions of the
56 functions in the standard C library.
60 function schedules an I/O read operation on the specified dispatch I/O
62 As results from the read operation become available, the provided
64 block will be submitted to the specified
66 The block will be passed a dispatch data object representing the data that has
67 been read since the handler's previous invocation.
71 parameter indicates where the read operation should begin. For a channel of
72 .Dv DISPATCH_IO_RANDOM
73 type it is interpreted relative to the position of the file pointer when the
74 channel was created, for a channel of
75 .Dv DISPATCH_IO_STREAM
76 type it is ignored and the read operation will begin at the current file
81 parameter indicates the number of bytes that should be read from the I/O
84 to keep reading until EOF is encountered (for a channel created from a
85 disk-based file this happens when reading past the end of the physical file).
89 function schedules an I/O write operation on the specified dispatch I/O
91 As the write operation progresses, the provided
93 block will be submitted to the specified
95 The block will be passed a dispatch data object representing the data that
96 remains to be written as part of this I/O operation.
100 parameter indicates where the write operation should begin. It is interpreted
101 as for read operations above.
105 parameter specifies the location and amount of data to be written, encapsulated
106 as a dispatch data object. The object is retained by the system until the write
107 operation is complete.
108 .Sh I/O HANDLER BLOCKS
109 Dispatch I/O handler blocks submitted to a channel via the
112 .Fn dispatch_io_write
113 functions will be executed one or more times depending on system load and the
114 channel's configuration settings (see
115 .Xr dispatch_io_create 3
116 for details). The handler block need not be reentrant safe,
117 no new I/O handler instance is submitted until the previously enqueued handler
122 object passed to an I/O handler block will be released by the system when the
123 block returns, if access to the memory buffer it represents is needed outside
124 of the handler, the handler block must retain the data object or create a new
125 (e.g.\& concatenated) data object from it (see
126 .Xr dispatch_data_create 3
129 Once an I/O handler block is invoked with the
131 flag set, the associated I/O operation is complete and that handler block will
132 not be run again. If an unrecoverable error occurs while performing the I/O
133 operation, the handler block will be submitted with the
135 flag set and the appriate POSIX error code in the
137 parameter. An invocation of a handler block with the
144 .Vt dispatch_data_empty
145 indicates that the I/O operation has encountered EOF.
148 .Xr dispatch_data_create 3 ,
149 .Xr dispatch_io_create 3 ,
150 .Xr dispatch_read 3 ,