]> git.saurik.com Git - apple/libdispatch.git/blame - man/dispatch_io_read.3
libdispatch-913.30.4.tar.gz
[apple/libdispatch.git] / man / dispatch_io_read.3
CommitLineData
e85f4437
A
1.\" Copyright (c) 2010 Apple Inc. All rights reserved.
2.Dd December 1, 2010
3.Dt dispatch_io_read 3
4.Os Darwin
5.Sh NAME
6.Nm dispatch_io_read ,
7.Nm dispatch_io_write
8.Nd submit read and write operations to dispatch I/O channels
9.Sh SYNOPSIS
10.Fd #include <dispatch/dispatch.h>
11.Ft void
12.Fo dispatch_io_read
13.Fa "dispatch_io_t channel"
14.Fa "off_t offset"
15.Fa "size_t length"
16.Fa "dispatch_queue_t queue"
17.Fa "void (^handler)(bool done, dispatch_data_t data, int error)"
18.Fc
19.Ft void
20.Fo dispatch_io_write
21.Fa "dispatch_io_t channel"
22.Fa "off_t offset"
23.Fa "dispatch_data_t dispatch"
24.Fa "dispatch_queue_t queue"
25.Fa "void (^handler)(bool done, dispatch_data_t data, int error)"
26.Fc
27.Sh DESCRIPTION
28The dispatch I/O framework is an API for asynchronous read and write I/O
29operations. It is an application of the ideas and idioms present in the
30.Xr dispatch 3
31framework to device I/O. Dispatch I/O enables an application to more easily
32avoid blocking I/O operations and allows it to more directly express its I/O
33requirements than by using the raw POSIX file API. Dispatch I/O will make a
34best effort to optimize how and when asynchronous I/O operations are performed
35based on the capabilities of the targeted device.
36.Pp
37This page provides details on how to read from and write to dispatch I/O
38channels. Creation and configuration of these channels is covered in the
39.Xr dispatch_io_create 3
40page. The dispatch I/O framework also provides the convenience functions
41.Xr dispatch_read 3
42and
43.Xr dispatch_write 3
44for uses that do not require the full functionality provided by I/O channels.
45.Pp
46.Sh FUNDAMENTALS
47The
48.Fn dispatch_io_read
49and
50.Fn dispatch_io_write
51functions are used to perform asynchronous read and write operations on
52dispatch I/O channels. They can be thought of as asynchronous versions of the
53.Xr fread 3
54and
55.Xr fwrite 3
56functions in the standard C library.
57.Sh READ OPERATIONS
58The
59.Fn dispatch_io_read
60function schedules an I/O read operation on the specified dispatch I/O
61.Va channel .
62As results from the read operation become available, the provided
63.Va handler
64block will be submitted to the specified
65.Va queue .
66The block will be passed a dispatch data object representing the data that has
67been read since the handler's previous invocation.
68.Pp
69The
70.Va offset
71parameter indicates where the read operation should begin. For a channel of
72.Dv DISPATCH_IO_RANDOM
73type it is interpreted relative to the position of the file pointer when the
74channel was created, for a channel of
75.Dv DISPATCH_IO_STREAM
76type it is ignored and the read operation will begin at the current file
77pointer position.
78.Pp
79The
80.Va length
81parameter indicates the number of bytes that should be read from the I/O
82channel. Pass
83.Dv SIZE_MAX
84to keep reading until EOF is encountered (for a channel created from a
85disk-based file this happens when reading past the end of the physical file).
86.Sh WRITE OPERATIONS
87The
88.Fn dispatch_io_write
89function schedules an I/O write operation on the specified dispatch I/O
90.Va channel .
91As the write operation progresses, the provided
92.Va handler
93block will be submitted to the specified
94.Va queue .
95The block will be passed a dispatch data object representing the data that
96remains to be written as part of this I/O operation.
97.Pp
98The
99.Va offset
100parameter indicates where the write operation should begin. It is interpreted
101as for read operations above.
102.Pp
103The
104.Va data
105parameter specifies the location and amount of data to be written, encapsulated
106as a dispatch data object. The object is retained by the system until the write
107operation is complete.
108.Sh I/O HANDLER BLOCKS
109Dispatch I/O handler blocks submitted to a channel via the
110.Fn dispatch_io_read
111or
112.Fn dispatch_io_write
113functions will be executed one or more times depending on system load and the
114channel's configuration settings (see
115.Xr dispatch_io_create 3
116for details). The handler block need not be reentrant safe,
117no new I/O handler instance is submitted until the previously enqueued handler
118block has returned.
119.Pp
120The dispatch
121.Va data
122object passed to an I/O handler block will be released by the system when the
123block returns, if access to the memory buffer it represents is needed outside
124of 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
127for details).
128.Pp
129Once an I/O handler block is invoked with the
130.Va done
131flag set, the associated I/O operation is complete and that handler block will
132not be run again. If an unrecoverable error occurs while performing the I/O
133operation, the handler block will be submitted with the
134.Va done
135flag set and the appriate POSIX error code in the
136.Va error
137parameter. An invocation of a handler block with the
138.Va done
139flag set, zero
140.Va error
141and
142.Va data
143set to
144.Vt dispatch_data_empty
145indicates that the I/O operation has encountered EOF.
146.Sh SEE ALSO
147.Xr dispatch 3 ,
148.Xr dispatch_data_create 3 ,
149.Xr dispatch_io_create 3 ,
150.Xr dispatch_read 3 ,
151.Xr fread 3