Skip to content

Instantly share code, notes, and snippets.

@vstakhov
Created May 3, 2012 16:54
io_submit sample
struct io_cbdata *cbdata;
struct iocb *iocb[1];
iocb[0] = alloca (sizeof (struct iocb));
memset (iocb[0], 0, sizeof (struct iocb));
iocb[0]->aio_fildes = fd;
iocb[0]->aio_lio_opcode = IO_CMD_PREAD;
iocb[0]->aio_reqprio = 0;
iocb[0]->aio_buf = (uint64_t)((uintptr_t)buf);
iocb[0]->aio_nbytes = len;
iocb[0]->aio_offset = offset;
iocb[0]->aio_flags |= (1 << 0) /* IOCB_FLAG_RESFD */;
iocb[0]->aio_resfd = event_fd;
iocb[0]->aio_data = (uint64_t)((uintptr_t)cbdata);
/* Iocb is copied to kernel internally, so it is safe to put it on stack */
if (io_submit (io_ctx, 1, iocb) == 1) {
return len;
}
else {
if (errno == EAGAIN || errno == ENOSYS) {
/* Fall back to sync read */
goto blocking;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment