poll.hpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
#pragma once


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


namespace felspar::io {


    struct poll_warden::retrier {
        virtual felspar::coro::coroutine_handle<> try_or_resume() = 0;
        virtual felspar::coro::coroutine_handle<> iop_timedout() = 0;
    };


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

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

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

        void insert_timeout() {
            if (timeout) {
                auto const deadline =
                        std::chrono::steady_clock::now() + *timeout;
                self->timeouts.insert({deadline, this});
            }
        }
        void cancel_timeout() {
            if (timeout) {
                auto pos = std::find_if(
                        self->timeouts.begin(), self->timeouts.end(),
                        [this](auto const &s) { return s.second == this; });
                if (pos != self->timeouts.end()) { self->timeouts.erase(pos); }
            }
        }
        virtual void cancel_iop() = 0;

        felspar::coro::coroutine_handle<>
                await_suspend(felspar::coro::coroutine_handle<> h) override {
            io::completion<R>::handle = h;
            insert_timeout();
            return try_or_resume();
        }
        felspar::coro::coroutine_handle<> iop_timedout() override {
            cancel_iop();
            io::completion<R>::result = {timeout::error, "IOP timed out"};
            return io::completion<R>::handle;
        }
        felspar::coro::coroutine_handle<> cancel_timeout_then_resume() {
            cancel_timeout();
            return io::completion<R>::handle;
        }

        bool delete_due_to_iop_destructed() override {
            cancel_timeout();
            cancel_iop();
            return true;
        }
    };

These are used to smooth over some Windows/POSIX differences

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    inline bool would_block(auto const err) {
#ifdef FELSPAR_WINSOCK2
        return err == WSAEWOULDBLOCK;
#else
        return err == EAGAIN or err == EWOULDBLOCK;
#endif
    }
    inline bool bad_fd(auto const err) {
#ifdef FELSPAR_WINSOCK2
        return false;
#else
        return err == EBADF;
#endif
    }


}