Trait nix::sys::aio::Aio

source ·
pub trait Aio {
    type Output;

    fn aio_return(self: Pin<&mut Self>) -> Result<Self::Output>;
    fn cancel(self: Pin<&mut Self>) -> Result<AioCancelStat>;
    fn error(self: Pin<&mut Self>) -> Result<()>;
    fn fd(&self) -> RawFd;
    fn in_progress(&self) -> bool;
    fn priority(&self) -> i32;
    fn set_sigev_notify(&mut self, sev: SigevNotify);
    fn sigevent(&self) -> SigEvent;
    fn submit(self: Pin<&mut Self>) -> Result<()>;
}
Available on crate feature aio only.
Expand description

Methods common to all AIO operations

Required Associated Types§

The return type of Aio::aio_return.

Required Methods§

Retrieve return status of an asynchronous operation.

Should only be called once for each operation, after Aio::error indicates that it has completed. The result is the same as for the synchronous read(2), write(2), of fsync(2) functions.

References

aio_return

Cancels an outstanding AIO request.

The operating system is not required to implement cancellation for all file and device types. Even if it does, there is no guarantee that the operation has not already completed. So the caller must check the result and handle operations that were not canceled or that have already completed.

Examples

Cancel an outstanding aio operation. Note that we must still call aio_return to free resources, even though we don’t care about the result.

let wbuf = b"CDEF";
let mut f = tempfile().unwrap();
let mut aiocb = Box::pin(AioWrite::new(f.as_raw_fd(),
    2,   //offset
    &wbuf[..],
    0,   //priority
    SigevNotify::SigevNone));
aiocb.as_mut().submit().unwrap();
let cs = aiocb.as_mut().cancel().unwrap();
if cs == AioCancelStat::AioNotCanceled {
    while (aiocb.as_mut().error() == Err(Errno::EINPROGRESS)) {
        thread::sleep(time::Duration::from_millis(10));
    }
}
// Must call `aio_return`, but ignore the result
let _ = aiocb.as_mut().aio_return();
References

aio_cancel

Retrieve error status of an asynchronous operation.

If the request has not yet completed, returns EINPROGRESS. Otherwise, returns Ok or any other error.

Examples

Issue an aio operation and use error to poll for completion. Polling is an alternative to aio_suspend, used by most of the other examples.

const WBUF: &[u8] = b"abcdef123456";
let mut f = tempfile().unwrap();
let mut aiocb = Box::pin(AioWrite::new(f.as_raw_fd(),
    2,   //offset
    WBUF,
    0,   //priority
    SigevNotify::SigevNone));
aiocb.as_mut().submit().unwrap();
while (aiocb.as_mut().error() == Err(Errno::EINPROGRESS)) {
    thread::sleep(time::Duration::from_millis(10));
}
assert_eq!(aiocb.as_mut().aio_return().unwrap(), WBUF.len());
References

aio_error

Returns the underlying file descriptor associated with the operation.

Does this operation currently have any in-kernel state?

Dropping an operation that does have in-kernel state constitutes a resource leak.

Examples
let f = tempfile().unwrap();
let mut aiof = Box::pin(AioFsync::new(f.as_raw_fd(), AioFsyncMode::O_SYNC,
    0, SigevNone));
assert!(!aiof.as_mut().in_progress());
aiof.as_mut().submit().expect("aio_fsync failed early");
assert!(aiof.as_mut().in_progress());
while (aiof.as_mut().error() == Err(Errno::EINPROGRESS)) {
    thread::sleep(time::Duration::from_millis(10));
}
aiof.as_mut().aio_return().expect("aio_fsync failed late");
assert!(!aiof.as_mut().in_progress());

Returns the priority of the AioCb

Update the notification settings for an existing AIO operation that has not yet been submitted.

Returns the SigEvent that will be used for notification.

Actually start the I/O operation.

After calling this method and until Aio::aio_return returns Ok, the structure may not be moved in memory.

Implementors§