#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include int main(int argc, char **argv) { #define SIZE (256*1024*1024) char *buf; struct io_event io_event; struct iocb iocb; struct iocb *iocbs[] = { &iocb }; int rwfd, efd; int res; io_context_t io_ctx; efd = eventfd(0, 0); if (efd < 0) { perror("eventfd"); exit(1); } rwfd = open("rwfile", O_RDWR|O_DIRECT); assert(rwfd != -1); if (posix_memalign((void **)&buf, getpagesize(), SIZE) < 0) { perror("posix_memalign"); exit(1); } memset(buf, 0x42, SIZE); /* Write test. */ res = io_queue_init(1024, &io_ctx); assert(res == 0); io_prep_pwrite(&iocb, rwfd, buf, SIZE, 0); io_set_eventfd(&iocb, efd); res = io_submit(io_ctx, 1, iocbs); assert(res == 1); /* Now close the eventfd so that AIO has the last reference */ close(efd); /* Keep this process around so that the aio subsystem does not hold * the last reference on the rwfd, otherwise the really_put_req will * be called from process context */ res = io_getevents(io_ctx, 1, 1, &io_event, NULL); if (res != 1) { if (res < 0) { errno = -res; perror("io_getevents"); } else printf("io_getevents did not return 1 event after " "closing eventfd\n"); exit(1); } assert(io_event.res == SIZE); printf("eventfd write test [SUCCESS]\n"); return 0; }