uring.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#pragma once


#include <felspar/exceptions.hpp>
#include <felspar/io/warden.uring.hpp>

#include <liburing.h>

#include <vector>


namespace felspar::io {


    struct uring_warden::delivery {

True so long as the IOP instance still exists

17
        bool iop_exists = true;

True if the completion is now in the outstanding completions list

19
        bool is_outstanding = false;

The number of IOPs that have been submitted to the queue and not returned

22
23
24
25
26
27
28
29
30
31
32
33
34
        std::size_t iop_count = 1;

        virtual ~delivery() = default;
        virtual void deliver(int result) = 0;
    };


    struct uring_warden::impl {
        ~impl() {
            for (auto *d : outstanding) { delete d; }
        }

        ::io_uring uring;

Fetch another SQE from the uring

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
        ::io_uring_sqe *next_sqe();

        void execute(::io_uring_cqe *);

        std::vector<delivery *> outstanding;
    };


    template<typename R>
    struct uring_warden::completion :
    public delivery,
            public io::completion<R> {
        completion(
                uring_warden *w,
                std::optional<std::chrono::nanoseconds> tout,
                felspar::source_location const &loc)
        : io::completion<R>{loc}, self{w}, timeout{tout} {}

        uring_warden *self = nullptr;
        warden *ward() override { return self; }

        std::optional<std::chrono::nanoseconds> timeout = {};
        __kernel_timespec kts;

        ::io_uring_sqe *setup_submission(felspar::coro::coroutine_handle<> h) {
            io::completion<R>::handle = h;
            return self->ring->next_sqe();
        }
        felspar::coro::coroutine_handle<> setup_timeout(::io_uring_sqe *sqe) {
            ::io_uring_sqe_set_data(sqe, this);
            if (timeout) {
                sqe->flags |= IOSQE_IO_LINK;
                auto tsqe = self->ring->next_sqe();
                kts.tv_sec = 0;
                kts.tv_nsec = timeout->count();
                ::io_uring_prep_link_timeout(tsqe, &kts, 0);
                ::io_uring_sqe_set_data(tsqe, this);
                iop_count = 2;
            }
            return felspar::coro::noop_coroutine();
        }

        void deliver(int result) override {
            if (result < 0) {
                if (timeout and result == -ECANCELED) {

This is the cancelled IOP so we ignore it as we've timed out

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
                    return;
                } else {
                    io::completion<R>::result = {
                            {-result, std::system_category()}, "uring IOP"};
                }
            } else {
                io::completion<R>::result = result;
            }
            io::completion<R>::handle.resume();
        }
        bool delete_due_to_iop_destructed() override {
            iop_exists = false;
            if (iop_count == 0) {
                return true;
            } else {

Cancel IOP

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
                is_outstanding = true;
                self->ring->outstanding.push_back(this);
                return false;
            }
        }
    };
    template<>
    struct uring_warden::completion<void> :
    public delivery,
            public io::completion<void> {
        completion(
                uring_warden *w,
                std::optional<std::chrono::nanoseconds> tout,
                felspar::source_location const &loc)
        : io::completion<void>{loc}, self{w}, timeout{tout} {}

        uring_warden *self;
        warden *ward() override { return self; }

        std::optional<std::chrono::nanoseconds> timeout = {};
        __kernel_timespec kts;

        ::io_uring_sqe *setup_submission(felspar::coro::coroutine_handle<> h) {
            io::completion<void>::handle = h;
            return self->ring->next_sqe();
        }
        felspar::coro::coroutine_handle<> setup_timeout(::io_uring_sqe *sqe) {
            ::io_uring_sqe_set_data(sqe, this);
            if (timeout) {
                sqe->flags |= IOSQE_IO_LINK;
                auto tsqe = self->ring->next_sqe();
                kts.tv_sec = 0;
                kts.tv_nsec = timeout->count();
                ::io_uring_prep_link_timeout(tsqe, &kts, 0);
                ::io_uring_sqe_set_data(tsqe, this);
                iop_count = 2;
            }
            return felspar::coro::noop_coroutine();
        }

        void deliver(int result) override {
            if (result == -ETIME) {
                io::completion<void>::result = {
                        {ETIME, std::system_category()}, "uring IOP timeout"};
            } else if (timeout and result == -ECANCELED) {

This is the cancelled IOP so we ignore it as we've timed out

147
148
149
150
151
152
153
154
155
156
157
158
                return;
            } else if (result < 0) {
                io::completion<void>::result = {
                        {-result, std::system_category()}, "uring IOP"};
            }
            io::completion<void>::handle.resume();
        }
        bool delete_due_to_iop_destructed() override {
            iop_exists = false;
            if (iop_count == 0) {
                return true;
            } else {

Cancel IOP

160
161
162
163
164
165
166
167
168
                is_outstanding = true;
                self->ring->outstanding.push_back(this);
                return false;
            }
        }
    };


}