uring.warden.cpp

1
2
3
4
5
6
#include "uring.hpp"

#include <felspar/exceptions/runtime_error.hpp>

#include <cerrno>
#include <span>

felspar::io::uring_warden

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
felspar::io::uring_warden::uring_warden(unsigned entries, unsigned flags)
: ring{std::make_unique<impl>()} {
    if (auto const ret = ::io_uring_queue_init(entries, &ring->uring, flags);
        ret < 0) {
        throw felspar::stdexcept::system_error{
                -ret, std::system_category(), "uring_queue_init"};
    }
}
felspar::io::uring_warden::~uring_warden() {
    if (ring) { ::io_uring_queue_exit(&ring->uring); }
}


void felspar::io::uring_warden::run_until(std::coroutine_handle<> coro) {
    coro.resume();
    while (not coro.done()) {
        ::io_uring_submit(&ring->uring);

        ::io_uring_cqe *cqe = {};
        auto const ret = ::io_uring_wait_cqe(&ring->uring, &cqe);
        if (ret == -EINTR) {

A signal interrupted the wait. io_uring_enter is not restarted by SA_RESTART, so retry the loop ourselves rather than treating the interruption as fatal — any completions a handler queued (such as a self-pipe write) are then picked up on the next wait.

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
            continue;
        } else if (ret < 0) {
            throw felspar::stdexcept::system_error{
                    -ret, std::system_category(), "uring_wait_cqe"};
        }
        ring->execute(cqe);
        while (::io_uring_peek_cqe(&ring->uring, &cqe) == 0) {
            ring->execute(cqe);
        }
        resumer.resume_all();
    }
}


void felspar::io::uring_warden::run_batch() {
    ::io_uring_submit(&ring->uring);
    ::io_uring_cqe *cqe = {};
    while (::io_uring_peek_cqe(&ring->uring, &cqe) == 0) { ring->execute(cqe); }
    resumer.resume_all();
}


void felspar::io::uring_warden::do_async_resume(
        std::span<std::coroutine_handle<> const> const handles) {

Wake only when the queue was empty — a prior wake covers the rest

64
65
66
67
68
69
70
71
72
    bool const was_empty = not resumer.has_requests();
    if (resumer.queue(handles) and was_empty) { wake_event_loop(); }
}


void felspar::io::uring_warden::wake_event_loop() {
    auto *const sqe = ring->next_sqe();
    ::io_uring_prep_nop(sqe);
    ::io_uring_sqe_set_data(sqe, nullptr);

Submit immediately so that a wait_cqe already blocked in the kernel is woken by the NOP's completion — queuing the SQE alone wouldn't reach the kernel until the next submit, which won't happen while the loop is parked.

78
79
    ::io_uring_submit(&ring->uring);
}

felspar::io::uring_warden::impl

85
86
87
88
89
90
91
92
93
94
95
96
97
::io_uring_sqe *felspar::io::uring_warden::impl::next_sqe() {
    ::io_uring_sqe *sqe = ::io_uring_get_sqe(&uring);
    if (not sqe) {
        throw felspar::stdexcept::runtime_error{
                "No more SQEs are available in the ring"};
    }
    return sqe;
}


void felspar::io::uring_warden::impl::execute(::io_uring_cqe *cqe) {
    auto d = reinterpret_cast<delivery *>(::io_uring_cqe_get_data(cqe));
    if (not d) {

A null user data marks the wake-up NOP posted by wake_event_loop. It carries no delivery; consuming it is all that's needed — its only job was to make the loop's wait return so the async resume queue gets drained.

104
105
106
107
108
        ::io_uring_cqe_seen(&uring, cqe);
        return;
    }
    int result = cqe->res;
    ::io_uring_cqe_seen(&uring, cqe);

If the owning IOP has already been destructed (for example its coroutine was cancelled while this op was still in flight) the completion is only being kept alive to drain its outstanding CQEs. There is no coroutine left to resume, so calling deliver would resume a destroyed handle and read freed memory — we just count the CQE down and let the bookkeeping below delete the completion once the last one arrives.

117
118
119
120
    if (d->iop_exists) { d->deliver(result); }
    if (d->is_outstanding) { std::erase(outstanding, d); }
    if (--d->iop_count == 0 and not d->iop_exists) { delete d; }
}