future.hpp

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


#include <felspar/coro/coroutine.hpp>
#include <felspar/exceptions.hpp>
#include <felspar/test/source.hpp>

#include <optional>
#include <vector>


namespace felspar::coro {

Asynchronous future

A non-thread safe asynchronous future. This type is not used to define a coroutine (like a task is), but is used to enable communication to coroutines from other parts of the code. Typically you will find this type as an instance in a data structure.

It is able to have multiple coroutines wait on the future, with them all being resumed when a value is pushed. If the value has already been set then any new coroutine will continue without suspending.

The value can be set and read from non-coroutines as well. The value can only be set once.

29
30
31
32
33
34
35
36
    template<typename T>
    class future {
        std::optional<T> m_value;
        std::vector<coroutine_handle<>> continuations;


      public:
        using value_type = T;

Query the future

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
        bool has_value() const noexcept { return m_value.has_value(); }
        explicit operator bool() const noexcept { return has_value(); }

        value_type &
                value(source_location const &loc = source_location::current()) {
            if (not m_value) {
                throw felspar::stdexcept::logic_error{
                        "Future does not contain a value", loc};
            } else {
                return *m_value;
            }
        }
        value_type const &value(
                source_location const &loc = source_location::current()) const {
            if (not m_value) {
                throw felspar::stdexcept::logic_error{
                        "Future does not contain a value", loc};
            } else {
                return *m_value;
            }
        }

Coroutine interface

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        auto operator co_await() {
            struct awaitable {
                explicit awaitable(coro::future<value_type> &f) : fut{f} {}
                awaitable(awaitable const &) = delete;
                // TODO We could be movable
                awaitable(awaitable &&) = delete;
                ~awaitable() {
                    if (mine) { std::erase(fut.continuations, mine); }
                }

                awaitable &operator=(awaitable const &) = delete;
                awaitable &operator=(awaitable &&) = delete;


                coro::future<value_type> &fut;
                coroutine_handle<> mine = {};


                bool await_ready() const noexcept { return fut.has_value(); }
                void await_suspend(coroutine_handle<> h) {
                    mine = h;
                    fut.continuations.push_back(h);
                }
                value_type &await_resume() {
                    mine = {};
                    return *fut.m_value;
                }
            };
            return awaitable{*this};
        }

Set the future's value

 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        void set_value(
                value_type t,
                source_location const &loc = source_location::current()) {
            if (m_value) {
                throw stdexcept::logic_error{
                        "The future already has a value set", loc};
            }
            m_value = std::move(t);
            for (auto h : continuations) { h.resume(); }
            continuations = {};
        }
    };

    template<>
    class future<void> {
        bool m_has_value = false;
        std::vector<coroutine_handle<>> continuations;


      public:
        using value_type = void;

Query the future

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
        bool has_value() const noexcept { return m_has_value; }
        explicit operator bool() const noexcept { return has_value(); }

        void value(source_location const &loc = source_location::current()) {
            if (not m_has_value) {
                throw felspar::stdexcept::logic_error{
                        "Future does not contain a value", loc};
            }
        }
        void value(
                source_location const &loc = source_location::current()) const {
            if (not m_has_value) {
                throw felspar::stdexcept::logic_error{
                        "Future does not contain a value", loc};
            }
        }

Coroutine interface

140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        auto operator co_await() {
            struct awaitable {
                explicit awaitable(coro::future<void> &f) : fut{f} {}
                awaitable(awaitable const &) = delete;
                // TODO We could be movable
                awaitable(awaitable &&) = delete;
                ~awaitable() {
                    if (mine) { std::erase(fut.continuations, mine); }
                }

                awaitable &operator=(awaitable const &) = delete;
                awaitable &operator=(awaitable &&) = delete;


                coro::future<void> &fut;
                coroutine_handle<> mine = {};


                bool await_ready() const noexcept { return fut.has_value(); }
                void await_suspend(coroutine_handle<> h) {
                    mine = h;
                    fut.continuations.push_back(h);
                }
                void await_resume() noexcept { mine = {}; }
            };
            return awaitable{*this};
        }

Set the future's value

170
171
172
173
174
175
176
177
178
179
180
181
182
        void set_value(source_location const &loc = source_location::current()) {
            if (m_has_value) {
                throw stdexcept::logic_error{
                        "The future already has a value set", loc};
            }
            m_has_value = true;
            for (auto h : continuations) { h.resume(); }
            continuations = {};
        }
    };


}