p The fields are: l -enum -offset indent t The .Va aio_offset specifies the implicit file offset at which the .Tn I/O operations are performed. This cannot be expected to be the actual read/write offset of the file descriptor. t The .Va aio_buf member is a pointer to the buffer to which data is going to be written or to which the read operation stores data. t The .Va aio_nbytes specifies the length of .Va aio_buf . t The .Va aio_fildes specifies the used file descriptor. t The .Va aio_lio_opcode is used by the .Fn lio_listio function to initialize a list of .Tn I/O requests with a single call. t The .Va aio_reqprio member can be used to lower the scheduling priority of an .Nm operation. This is only available if .Dv _POSIX_PRIORITIZED_IO and .Dv _POSIX_PRIORITY_SCHEDULING are defined, and the associated file descriptor supports it. t The .Va aio_sigevent member is used to specify how the calling process is notified once an .Nm operation completes. .El
p The members .Va aio_buf , .Va aio_fildes , and .Va aio_nbytes are conceptually similar to the parameters .Fa buf , .Fa fildes , and .Fa nbytes used in the standard .Xr read 2 and .Xr write 2 functions. For example, the caller can read .Va aio_nbytes from a file associated with the file descriptor .Va aio_fildes into the buffer .Va aio_buf . All appropriate fields should be initialized by the caller before .Fn aio_read or .Fn aio_write is called. .Ss File Offsets Asynchronous .Tn I/O operations are not strictly sequential; operations are carried out in arbitrary order and more than one operation for one file descriptor can be started. Each operation must specify an offset, but the actual file offset is never updated as a result of an .Nm operation. .Ss Errors and Completion Asynchronous .Tn I/O operations are said to be complete when: l -bullet -offset 2n t An error is detected. t The .Tn I/O transfer is performed successfully. t The operation is canceled. .El
p If an error condition is detected that prevents an operation from being started, the request is not enqueued. In this case the read and write functions, .Fn aio_read and .Fn aio_write , return immediately, setting the global .Va errno to indicate the cause of the error.
p After an operation has been successfully enqueued, .Fn aio_error and .Fn aio_return must be used to determine the status of the operation and to determine any error conditions. This includes the conditions reported by the standard .Xr read 2 and .Xr write 2 . The request remains enqueued and consumes process and system resources until .Fn aio_return is called. .Ss Waiting for Completion The .Nm interface supports both polling and notification models. The first can be implemented by simply repeatedly calling the .Fn aio_error function to test the status of an operation. Once the operation has completed, .Fn aio_return is used to free the .Va aiocb structure for re-use.
p The notification model is implemented by using the .Va aio_sigevent member of the Asynchronous I/O Control Block. The structure .Em sigevent is defined in n signal.h . The relevant fields for .Nm are .Va sigev_notify , .Va sigev_signo , and the function pointer .Va sigev_notify_function . The .Va sigev_notify member determines the type of the action: l -enum -offset indent t If it is .Dv SIGEV_NONE , no notification is sent. t If it is .Dv SIGEV_SIGNAL , the signal determined by .Va sigev_signo is sent when the operation completes. t If it is .Dv SIGEV_THREAD , a thread is created which starts executing the function specified by .Va sigev_notify_function . .El
p The .Fn aio_suspend function can be used to wait for the completion of one or more operations. It is possible to set a timeout so that the process can continue the execution and take recovery actions if the .Nm operations do not complete as expected. .Ss Cancellation and Synchronization The .Fn aio_cancel function can be used to request cancellation of an asynchronous .Tn I/O operation. Note however that not all of them can be canceled. The same .Va aiocb used to start the operation may be used as a handle for identification. It is also possible to request cancellation of all operations pending for a file.
p Comparable to .Xr fsync 2 , the .Fn aio_fsync function can be used to synchronize the contents of permanent storage when multiple asynchronous .Tn I/O operations are outstanding for the file or device. The synchronization operation includes only those requests that have already been successfully enqueued. .Sh FUNCTIONS The following functions comprise the .Tn API of the .Nm interface: l -column -offset indent "aio_suspend " "XXX" t Sy Function Ta Sy Description t Xr aio_cancel 3 Ta cancel an outstanding asynchronous I/O operation t Xr aio_error 3 Ta retrieve error status of asynchronous I/O operation t Xr aio_fsync 3 Ta asynchronous data synchronization of file t Xr aio_read 3 Ta asynchronous read from a file t Xr aio_return 3 Ta get return status of asynchronous I/O operation t Xr aio_suspend 3 Ta suspend until operations or timeout complete t Xr aio_write 3 Ta asynchronous write to a file t Xr lio_listio 3 Ta list directed I/O .El .Sh COMPATIBILITY Unfortunately, the .Tn POSIX asynchronous .Tn I/O implementations vary slightly. Some implementations provide a slightly different .Tn API with possible extensions. For instance, the .Fx implementation uses a function .Dq Fn aio_waitcomplete to wait for the next completion of an .Nm aio request. .Sh STANDARDS The .Nm interface is expected to conform to the .St -p1003.1-2001 standard. .Sh HISTORY The .Nm interface first appeared in .Nx 5.0 . .Sh CAVEATS When an asynchronous read operation is outstanding, undefined behavior may follow if the contents of .Va aiocb are altered, or if memory associated with the structure, or the .Va aio_buf buffer, is deallocated.