uring.hpp

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


#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

16
        bool iop_exists = true;

True if the completion is now in the outstanding completions list

18
        bool is_outstanding = false;

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

21
22
23
24
25
26
27
28
29
30
31
32
33
        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

36
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
82
83
84
85
        ::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<io::deadline> tout,
                std::source_location const loc)
        : io::completion<R>{loc}, self{w}, deadline{tout} {}

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

        std::optional<io::deadline> deadline = {};
        __kernel_timespec kts;

        ::io_uring_sqe *setup_submission(std::coroutine_handle<> h) {
            io::completion<R>::handle = h;
            return self->ring->next_sqe();
        }
        std::coroutine_handle<> setup_timeout(::io_uring_sqe *sqe) {
            ::io_uring_sqe_set_data(sqe, this);
            if (deadline) {
                sqe->flags |= IOSQE_IO_LINK;
                auto tsqe = self->ring->next_sqe();
                auto const remaining =
                        std::chrono::duration_cast<std::chrono::nanoseconds>(
                                *deadline - std::chrono::steady_clock::now());
                kts.tv_sec = 0;
                kts.tv_nsec = remaining > std::chrono::nanoseconds::zero()
                        ? remaining.count()
                        : 0;
                ::io_uring_prep_link_timeout(tsqe, &kts, 0);
                ::io_uring_sqe_set_data(tsqe, this);
                iop_count = 2;
            }
            return std::noop_coroutine();
        }

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

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

 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
                    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

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
145
146
147
148
149
150
151
152
153
                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<io::deadline> tout,
                std::source_location const loc)
        : io::completion<void>{loc}, self{w}, deadline{tout} {}

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

        std::optional<io::deadline> deadline = {};
        __kernel_timespec kts;

        ::io_uring_sqe *setup_submission(std::coroutine_handle<> h) {
            io::completion<void>::handle = h;
            return self->ring->next_sqe();
        }
        std::coroutine_handle<> setup_timeout(::io_uring_sqe *sqe) {
            ::io_uring_sqe_set_data(sqe, this);
            if (deadline) {
                sqe->flags |= IOSQE_IO_LINK;
                auto tsqe = self->ring->next_sqe();
                auto const remaining =
                        std::chrono::duration_cast<std::chrono::nanoseconds>(
                                *deadline - std::chrono::steady_clock::now());
                kts.tv_sec = 0;
                kts.tv_nsec = remaining > std::chrono::nanoseconds::zero()
                        ? remaining.count()
                        : 0;
                ::io_uring_prep_link_timeout(tsqe, &kts, 0);
                ::io_uring_sqe_set_data(tsqe, this);
                iop_count = 2;
            }
            return std::noop_coroutine();
        }

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

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

156
157
158
159
160
161
162
163
164
165
166
167
                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

169
170
171
172
173
174
175
176
177
                is_outstanding = true;
                self->ring->outstanding.push_back(this);
                return false;
            }
        }
    };


}