1 .\" Copyright (c) 2010 Apple Inc. All rights reserved.
3 .Dt dispatch_io_create 3
6 .Nm dispatch_io_create ,
7 .Nm dispatch_io_create_with_path ,
8 .Nm dispatch_io_close ,
9 .Nm dispatch_io_set_high_water ,
10 .Nm dispatch_io_set_low_water ,
11 .Nm dispatch_io_set_interval ,
12 .Nm dispatch_io_barrier
13 .Nd open, close and configure dispatch I/O channels
15 .Fd #include <dispatch/dispatch.h>
17 .Fo dispatch_io_create
18 .Fa "dispatch_io_type_t type"
20 .Fa "dispatch_queue_t queue"
21 .Fa "void (^cleanup_handler)(int error)"
24 .Fo dispatch_io_create_with_path
25 .Fa "dispatch_io_type_t type"
26 .Fa "const char *path"
29 .Fa "dispatch_queue_t queue"
30 .Fa "void (^cleanup_handler)(int error)"
34 .Fa "dispatch_io_t channel"
35 .Fa "dispatch_io_close_flags_t flags"
38 .Fo dispatch_io_set_high_water
39 .Fa "dispatch_io_t channel"
40 .Fa "size_t high_water"
43 .Fo dispatch_io_set_low_water
44 .Fa "dispatch_io_t channel"
45 .Fa "size_t low_water"
48 .Fo dispatch_io_set_interval
49 .Fa "dispatch_io_t channel"
50 .Fa "uint64_t interval"
51 .Fa "dispatch_io_interval_flags_t flags"
54 .Fo dispatch_io_barrier
55 .Fa "dispatch_io_t channel"
56 .Fa "void (^barrier)(void)"
59 The dispatch I/O framework is an API for asynchronous read and write I/O
60 operations. It is an application of the ideas and idioms present in the
62 framework to device I/O. Dispatch I/O enables an application to more easily
63 avoid blocking I/O operations and allows it to more directly express its I/O
64 requirements than by using the raw POSIX file API. Dispatch I/O will make a
65 best effort to optimize how and when asynchronous I/O operations are performed
66 based on the capabilities of the targeted device.
68 This page provides details on how to create and configure dispatch I/O
69 channels. Reading from and writing to these channels is covered in the
70 .Xr dispatch_io_read 3
71 page. The dispatch I/O framework also provides the convenience functions
75 for uses that do not require the full functionality provided by I/O channels.
77 A dispatch I/O channel represents the asynchronous I/O policy applied to a file
78 descriptor and encapsulates it for the purposes of ownership tracking while
79 I/O operations are ongoing.
81 Dispatch I/O channels can have one of the following types:
82 .Bl -tag -width DISPATCH_IO_STREAM -compact -offset indent
83 .It DISPATCH_IO_STREAM
84 channels that represent a stream of bytes and do not support reads and writes
85 at arbitrary offsets, such as pipes or sockets. Channels of this type perform
86 read and write operations sequentially at the current file pointer position and
87 ignore any offset specified. Depending on the underlying file descriptor, read
88 operations may be performed simultaneously with write operations.
89 .It DISPATCH_IO_RANDOM
90 channels that represent random access files on disk. Only supported for
91 seekable file descriptors and paths. Channels of this type may perform
92 submitted read and write operations concurrently at the specified offset
93 (interpreted relative to the position of the file pointer when the channel was
96 .Sh CHANNEL OPENING AND CLOSING
98 .Fn dispatch_io_create
100 .Fn dispatch_io_create_with_path
101 functions create a dispatch I/O channel of provided
103 from a file descriptor
105 or a pathname, respectively. They can be thought of as
108 POSIX function and the
110 function in the standard C library. For a channel created from a
111 pathname, the provided
116 parameters will be passed to
118 when the first I/O operation on the channel is ready to execute. The provided
120 block will be submitted to the specified
122 when all I/O operations on the channel have completed and is is closed or
123 reaches the end of its lifecycle. If an error occurs during channel creation,
126 block will be submitted immediately and passed an
128 parameter with the POSIX error encountered. After creating a dispatch I/O
129 channel from a file descriptor, the application must take care not to modify
130 that file descriptor until the associated
133 .Sx "FILEDESCRIPTOR OWNERSHIP"
137 .Fn dispatch_io_close
138 function closes a dispatch I/O channel to new submissions of I/O operations. If
142 parameter, the system will in addition not perform the I/O operations already
143 submitted to the channel that are still pending and will make a best effort to
144 interrupt any ongoing operations. Handlers for operations so affected will be
147 error code, along with any partial results.
148 .Sh CHANNEL CONFIGURATION
149 Dispatch I/O channels have high-water mark, low-water mark and interval
150 configuration settings that determine if and when partial results from I/O
151 operations are delivered via their associated I/O handlers.
154 .Fn dispatch_io_set_high_water
156 .Fn dispatch_io_set_low_water
157 functions configure the water mark settings of a
160 or write at least the number of bytes specified by
162 before submitting an I/O handler with partial results, and will make a best
163 effort to submit an I/O handler as soon as the number of bytes read or written
168 .Fn dispatch_io_set_interval
169 function configures the time
171 at which I/O handlers are submitted (measured in nanoseconds). If
172 .Dv DISPATCH_IO_STRICT_INTERVAL
175 parameter, the interval will be strictly observed even if there is an
176 insufficient amount of data to deliver; otherwise delivery will be skipped for
177 intervals where the amount of available data is inferior to the channel's
178 low-water mark. Note that the system may defer enqueueing interval I/O handlers
179 by a small unspecified amount of leeway in order to align with other system
180 activity for improved system performance or power consumption.
183 The size of data objects passed to I/O handlers for a channel will never be
184 larger than the high-water mark set on the channel; it will also never be
185 smaller than the low-water mark, except in the following cases:
186 .Bl -dash -offset indent -compact
188 the final handler invocation for an I/O operation
192 the channel has an interval with the
193 .Dv DISPATCH_IO_STRICT_INTERVAL
196 Bear in mind that dispatch I/O channels will typically deliver amounts of data
197 significantly higher than the low-water mark. The default value for the
198 low-water mark is unspecified, but must be assumed to allow intermediate
199 handler invocations. The default value for the high-water mark is
202 Channels that require intermediate results of fixed size should have both the
203 low-water and the high-water mark set to that size. Channels that do not wish
204 to receive any intermediate results should have the low-water mark set to
207 .Sh FILEDESCRIPTOR OWNERSHIP
208 When an application creates a dispatch I/O channel from a file descriptor with
210 .Fn dispatch_io_create
211 function, the system takes control of that file descriptor until the channel is
212 closed, an error occurs on the file descriptor or all references to the channel
213 are released. At that time the channel's cleanup handler will be enqueued and
214 control over the file descriptor relinquished, making it safe for the
217 the file descriptor. While a file descriptor is under the control of a dispatch
218 I/O channel, file descriptor flags such as
220 will be modified by the system on behalf of the application. It is an error for
221 the application to modify a file descriptor directly while it is under the
222 control of a dispatch I/O channel, but it may create further I/O channels
223 from that file descriptor or use the
227 convenience functions with that file descriptor. If multiple I/O channels have
228 been created from the same file descriptor, all the associated cleanup handlers
229 will be submitted together once the last channel has been closed resp.\& all
230 references to those channels have been released. If convenience functions have
231 also been used on that file descriptor, submission of their handlers will be
232 tied to the submission of the channel cleanup handlers as well.
234 .Sh BARRIER OPERATIONS
236 .Fn dispatch_io_barrier
237 function schedules a barrier operation on an I/O channel. The specified barrier
238 block will be run once, after all current I/O operations (such as
242 file descriptor have finished. No new I/O operations will start until the
243 barrier block finishes.
245 The barrier block may operate on the underlying file descriptor with functions
251 .Sx FILEDESCRIPTOR OWNERSHIP
252 section, the barrier block must not
254 the file descriptor, and if it changes any flags on the file descriptor, it
255 must restore them before finishing.
257 There is no synchronization between a barrier block and any
258 .Xr dispatch_io_read 3
260 .Xr dispatch_io_write 3
261 handler blocks; they may be running at the same time. The barrier block itself
262 is responsible for any required synchronization.
264 Dispatch I/O channel objects are retained and released via calls to
267 .Fn dispatch_release .
270 .Xr dispatch_io_read 3 ,
271 .Xr dispatch_object 3 ,
272 .Xr dispatch_read 3 ,