ssize_t submit_and_wait()

in io/aio-wrapper.cpp [83:135]


        ssize_t submit_and_wait(uint64_t timedout = -1)
        {
            auto ctx = libaio_ctx;
            io_set_eventfd(this, ctx->evfd);
            this->data = CURRENT;
            auto piocb = (iocb*)this;

            while(true)
            {
                int ret = io_submit(ctx->aio_ctx, 1, &piocb);
                if (ret == 1) break;
                if (ret < 0)
                {
                    auto e = -ret;
                    switch(e)
                    {
                        case EAGAIN:
                            ctx->cond.wait_no_lock();
                        case EINTR:
                            continue;
                        case EBADF:
                        case EFAULT:
                        case EINVAL:
                        default:
                            thread_usleep(1000*10);     // sleep 10ms whenever error occurs
                            LOG_ERRNO_RETURN(e, ret, "failed to io_submit()");
                    }
                }
            }

            int ret = thread_usleep(timedout);
            if (ret == 0)  // timedout
            {
                cancel();
                LOG_WARN("libaio timedout fd=`, offset=`, nbytes=`", aio_fildes, u.c.offset, u.c.nbytes);
                errno = ETIMEDOUT;
                return -1;
            }

            auto e = errno;
            if (e != EOK)  // interrupted by a user thread
            {
                cancel();
                LOG_ERROR_RETURN(e, -1, "libaio interrupted");
            }

            if (this->ioret < 0) {
                e = -this->ioret;
                LOG_ERROR_RETURN(e, -1, "libaio result error");
            }

            return this->ioret;
        }