Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

dispatch_read(3) [osx man page]

dispatch_read(3)					   BSD Library Functions Manual 					  dispatch_read(3)

NAME
dispatch_read, dispatch_write -- asynchronously read from and write to file descriptors SYNOPSIS
#include <dispatch/dispatch.h> void dispatch_read(int fd, size_t length, dispatch_queue_t queue, void (^handler)(dispatch_data_t data, int error)); void dispatch_write(int fd, dispatch_data_t data, dispatch_queue_t queue, void (^handler)(dispatch_data_t data, int error))); DESCRIPTION
The dispatch_read() and dispatch_write() functions asynchronously read from and write to POSIX file descriptors. They can be thought of as asynchronous, callback-based versions of the fread() and fwrite() functions provided by the standard C library. They are convenience func- tions based on the dispatch_io_read(3) and dispatch_io_write(3) functions, intended for simple one-shot read or write requests. Multiple request on the same file desciptor are better handled with the full underlying dispatch I/O channel functions. BEHAVIOR
The dispatch_read() function schedules an asynchronous read operation on the file descriptor fd. Once the file descriptor is readable, the system will read as much data as is currently available, up to the specified length, starting at the current file pointer position. The given handler block will be submitted to queue when the operation completes or an error occurs. The block will be passed a dispatch data object with the result of the read operation. If an error occurred while reading from the file descriptor, the error parameter to the block will be set to the appropriate POSIX error code and data will contain any data that could be read successfully. If the file pointer position is at end-of-file, emtpy data and zero error will be passed to the handler block. The dispatch_write() function schedules an asynchronous write operation on the file descriptor fd. The system will attempt to write the entire contents of the provided data object to fd at the current file pointer position. The given handler block will be submitted to queue when the operation completes or an error occurs. If the write operation completed successfully, the error parameter to the block will be set to zero, otherwise it will be set to the appropriate POSIX error code and the data parameter will contain any data that could not be written. CAVEATS
The data object passed to a handler block is released by the system when the block returns. If data is needed outside of the handler block, it must concatenate, copy, or retain it. Once an asynchronous read or write operation has been submitted on a file descriptor fd, the system takes control of that file descriptor until the handler block is executed. During this time the application must not manipulate fd directly, in particular it is only safe to close fd from the handler block (or after it has returned). If multiple asynchronous read or write operations are submitted to the same file descriptor, they will be performed in order, but their han- dlers will only be submitted once all operations have completed and control over the file descriptor has been relinquished. For details on this and on the interaction with dispatch I/O channels created from the same file descriptor, see FILEDESCRIPTOR OWNERSHIP in dispatch_io_create(3). SEE ALSO
dispatch(3), dispatch_data_create(3), dispatch_io_create(3), dispatch_io_read(3), fread(3) Darwin December 1, 2010 Darwin

Check Out this Related Man Page

dispatch_io_create(3)					   BSD Library Functions Manual 				     dispatch_io_create(3)

NAME
dispatch_io_create, dispatch_io_create_with_path, dispatch_io_close, dispatch_io_set_high_water, dispatch_io_set_low_water, dispatch_io_set_interval, dispatch_io_barrier -- open, close and configure dispatch I/O channels SYNOPSIS
#include <dispatch/dispatch.h> dispatch_io_t dispatch_io_create(dispatch_io_type_t type, int fd, dispatch_queue_t queue, void (^cleanup_handler)(int error)); dispatch_io_t dispatch_io_create_with_path(dispatch_io_type_t type, const char *path, int oflag, mode_t mode, dispatch_queue_t queue, void (^cleanup_handler)(int error)); void dispatch_io_close(dispatch_io_t channel, dispatch_io_close_flags_t flags); void dispatch_io_set_high_water(dispatch_io_t channel, size_t high_water); void dispatch_io_set_low_water(dispatch_io_t channel, size_t low_water); void dispatch_io_set_interval(dispatch_io_t channel, uint64_t interval, dispatch_io_interval_flags_t flags); void dispatch_io_barrier(dispatch_io_t channel, void (^barrier)(void)); DESCRIPTION
The dispatch I/O framework is an API for asynchronous read and write I/O operations. It is an application of the ideas and idioms present in the dispatch(3) framework to device I/O. Dispatch I/O enables an application to more easily avoid blocking I/O operations and allows it to more directly express its I/O requirements than by using the raw POSIX file API. Dispatch I/O will make a best effort to optimize how and when asynchronous I/O operations are performed based on the capabilities of the targeted device. This page provides details on how to create and configure dispatch I/O channels. Reading from and writing to these channels is covered in the dispatch_io_read(3) page. The dispatch I/O framework also provides the convenience functions dispatch_read(3) and dispatch_write(3) for uses that do not require the full functionality provided by I/O channels. FUNDAMENTALS
A dispatch I/O channel represents the asynchronous I/O policy applied to a file descriptor and encapsulates it for the purposes of ownership tracking while I/O operations are ongoing. CHANNEL TYPES
Dispatch I/O channels can have one of the following types: DISPATCH_IO_STREAM channels that represent a stream of bytes and do not support reads and writes at arbitrary offsets, such as pipes or sockets. Channels of this type perform read and write operations sequentially at the current file pointer posi- tion and ignore any offset specified. Depending on the underlying file descriptor, read operations may be performed simultaneously with write operations. DISPATCH_IO_RANDOM channels that represent random access files on disk. Only supported for seekable file descriptors and paths. Chan- nels of this type may perform submitted read and write operations concurrently at the specified offset (interpreted relative to the position of the file pointer when the channel was created). CHANNEL OPENING AND CLOSING
The dispatch_io_create() and dispatch_io_create_with_path() functions create a dispatch I/O channel of provided type from a file descriptor fd or an absolute pathname, respectively. They can be thought of as analogous to the fdopen(3) POSIX function and the fopen(3) function in the standard C library. For a channel created from a pathname, the provided path, oflag and mode parameters will be passed to open(2) when the first I/O operation on the channel is ready to execute. The provided cleanup_handler block will be submitted to the specified queue when all I/O operations on the channel have completed and it is closed or reaches the end of its lifecycle. If an error occurs during channel creation, the cleanup_handler block will be submitted immedi- ately and passed an error parameter with the POSIX error encountered. If an invalid type or a non-absolute path argument is specified, these functions will return NULL and the cleanup_handler will not be invoked. After successfully creating a dispatch I/O channel from a file descriptor, the application must take care not to modify that file descriptor until the associated cleanup_handler is invoked, see FILEDESCRIPTOR OWNERSHIP for details. The dispatch_io_close() function closes a dispatch I/O channel to new submissions of I/O operations. If DISPATCH_IO_STOP is passed in the flags parameter, the system will in addition not perform the I/O operations already submitted to the channel that are still pending and will make a best effort to interrupt any ongoing operations. Handlers for operations so affected will be passed the ECANCELED error code, along with any partial results. CHANNEL CONFIGURATION
Dispatch I/O channels have high-water mark, low-water mark and interval configuration settings that determine if and when partial results from I/O operations are delivered via their associated I/O handlers. The dispatch_io_set_high_water() and dispatch_io_set_low_water() functions configure the water mark settings of a channel. The system will read or write at least the number of bytes specified by low_water before submitting an I/O handler with partial results, and will make a best effort to submit an I/O handler as soon as the number of bytes read or written reaches high_water. The dispatch_io_set_interval() function configures the time interval at which I/O handlers are submitted (measured in nanoseconds). If DISPATCH_IO_STRICT_INTERVAL is passed in the flags parameter, the interval will be strictly observed even if there is an insufficient amount of data to deliver; otherwise delivery will be skipped for intervals where the amount of available data is inferior to the channel's low- water mark. Note that the system may defer enqueueing interval I/O handlers by a small unspecified amount of leeway in order to align with other system activity for improved system performance or power consumption. DATA DELIVERY
The size of data objects passed to I/O handlers for a channel will never be larger than the high-water mark set on the channel; it will also never be smaller than the low-water mark, except in the following cases: - the final handler invocation for an I/O operation - EOF was encountered - the channel has an interval with the DISPATCH_IO_STRICT_INTERVAL flag set Bear in mind that dispatch I/O channels will typically deliver amounts of data significantly higher than the low-water mark. The default value for the low-water mark is unspecified, but must be assumed to allow intermediate handler invocations. The default value for the high- water mark is unlimited (i.e. SIZE_MAX). Channels that require intermediate results of fixed size should have both the low-water and the high-water mark set to that size. Channels that do not wish to receive any intermediate results should have the low-water mark set to SIZE_MAX. FILEDESCRIPTOR OWNERSHIP
When an application creates a dispatch I/O channel from a file descriptor with the dispatch_io_create() function, the system takes control of that file descriptor until the channel is closed, an error occurs on the file descriptor or all references to the channel are released. At that time the channel's cleanup handler will be enqueued and control over the file descriptor relinquished, making it safe for the applica- tion to close(2) the file descriptor. While a file descriptor is under the control of a dispatch I/O channel, file descriptor flags such as O_NONBLOCK will be modified by the system on behalf of the application. It is an error for the application to modify a file descriptor directly while it is under the control of a dispatch I/O channel, but it may create further I/O channels from that file descriptor or use the dispatch_read(3) and dispatch_write(3) convenience functions with that file descriptor. If multiple I/O channels have been created from the same file descriptor, all the associated cleanup handlers will be submitted together once the last channel has been closed resp. all refer- ences to those channels have been released. If convenience functions have also been used on that file descriptor, submission of their han- dlers will be tied to the submission of the channel cleanup handlers as well. BARRIER OPERATIONS
The dispatch_io_barrier() function schedules a barrier operation on an I/O channel. The specified barrier block will be run once, after all current I/O operations (such as read(2) or write(2)) on the underlying file descriptor have finished. No new I/O operations will start until the barrier block finishes. The barrier block may operate on the underlying file descriptor with functions like fsync(2) or lseek(2). As discussed in the FILEDESCRIPTOR OWNERSHIP section, the barrier block must not close(2) the file descriptor, and if it changes any flags on the file descriptor, it must restore them before finishing. There is no synchronization between a barrier block and any dispatch_io_read(3) or dispatch_io_write(3) handler blocks; they may be running at the same time. The barrier block itself is responsible for any required synchronization. MEMORY MODEL
Dispatch I/O channel objects are retained and released via calls to dispatch_retain() and dispatch_release(). SEE ALSO
dispatch(3), dispatch_io_read(3), dispatch_object(3), dispatch_read(3), fopen(3), open(2) Darwin December 1, 2010 Darwin
Man Page