cancellable.hpp

1
2
3
4
5
6
7
8
9
#pragma once


#include <felspar/coro/coroutine.hpp>

#include <vector>


namespace felspar::coro {

Cancellable coroutines

13
14
15
16
17
18
19
20
21
22
23
24
    class cancellable {
        std::vector<coroutine_handle<>> continuations = {};
        bool signalled = false;

        void remove(coroutine_handle<> h) { std::erase(continuations, h); }

      public:
        cancellable() {}
        cancellable(cancellable const &) = delete;
        cancellable(cancellable &&) = delete;
        cancellable &operator=(cancellable const &) = delete;
        cancellable &operator=(cancellable &&) = delete;

Used externally to cancel the controlled coroutine

27
28
29
30
31
32
33
34
35
        void cancel() {
            signalled = true;
            while (continuations.size()) {
                auto h = continuations.back();
                continuations.pop_back();
                h.resume();
            }
        }
        bool cancelled() const noexcept { return signalled; }

Wrap an awaitable so that an early resumption can be signalled

38
39
40
41
42
43
44
45
46
47
48
49
50
        template<typename A>
        auto signal_or(A coro_awaitable) {
            struct awaitable {
                A a;
                cancellable &b;
                coroutine_handle<> continuation = {};

                ~awaitable() { b.remove(continuation); }

                bool await_ready() const noexcept {
                    return b.signalled or a.await_ready();
                }
                auto await_suspend(coroutine_handle<> h) noexcept {

h is the coroutine making use of the cancellable

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
                    continuation = h;
                    b.continuations.push_back(h);
                    return a.await_suspend(h);
                }
                auto await_resume()
                        -> decltype(std::declval<A>().await_resume()) {
                    b.remove(continuation);
                    if (b.signalled) {
                        a.continuation = {};
                        return {};
                    } else {
                        return a.await_resume();
                    }
                }
            };
            return awaitable{std::move(coro_awaitable), *this};
        }

This can be directly awaited until signalled

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        auto operator co_await() {
            struct awaitable {
                cancellable &b;
                coroutine_handle<> continuation = {};

                ~awaitable() { b.remove(continuation); }

                bool await_ready() const noexcept { return b.signalled; }
                void await_suspend(coroutine_handle<> h) noexcept {
                    continuation = h;
                    b.continuations.push_back(h);
                }
                void await_resume() noexcept { b.remove(continuation); }
            };
            return awaitable{*this};
        }
    };


}