summaryrefslogtreecommitdiff
path: root/async/scheduler.h
blob: 0196aa5ca6211b804bc17d5b5f230fe2a125c2eb (plain)
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
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
94
95
96
97
98
99
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#pragma once

#include <coroutine>

struct schedulable {
    schedulable* next = nullptr;

    std::coroutine_handle<> awaiter;

    schedulable() {}
    schedulable(std::coroutine_handle<> h) : awaiter(h) {}
};

struct scheduler_t {
    schedulable* first = nullptr;
    schedulable** last_next_p = &first;

    void run() {
        while(first) {
            auto sch = pop_next();
            sch.awaiter.resume();
        }
    }

    schedulable& pop_next() {
        critical_section lock;

        auto sch = first;
        first = first->next;

        if(!first) {
            last_next_p = &first;
        }

        return *sch;
    }

    void schedule(schedulable& sch) {
        critical_section lock;

        sch.next = nullptr;
        *last_next_p = &sch;
        last_next_p = &sch.next;
    }
};

scheduler_t scheduler;

struct yield : public schedulable {
    bool await_ready() {
        return false;
    }

    bool await_suspend(std::coroutine_handle<> h) {
        awaiter = h;
        scheduler.schedule(*this);
        return true;
    }

    void await_resume() {}
};

template <typename T = void>
struct future : public schedulable {
    std::optional<T> value;

    bool await_ready() {
        return bool(value);
    }

    bool await_suspend(std::coroutine_handle<> h) {
        if(value) {
            return false;
        } else {
            awaiter = h;
            return true;
        }
    }

    T await_resume() {
        return *value; // TODO: move?
    }

    bool done() {
        return bool(value);
    }

    void set(T v) {
        if(value) {
            return;
        }

        value = v;

        if(awaiter) {
            scheduler.schedule(*this);
        }
    }

    void reset() {
        value.reset();
        awaiter = nullptr;
    }
};

template <>
struct future<void> : public schedulable {
    bool ready;

    future() : ready(false) {}

    bool await_ready() {
        return ready;
    }

    bool await_suspend(std::coroutine_handle<> h) {
        if(ready) {
            return false;
        } else {
            awaiter = h;
            return true;
        }
    }

    void await_resume() {
        
    }

    bool done() {
        return ready;
    }

    void set() {
        if(ready) {
            return;
        }

        ready = true;

        if(awaiter) {
            scheduler.schedule(*this);
        }
    }

    void reset() {
        awaiter = nullptr;
        ready = false;
    }
};

struct task : public schedulable {
    struct promise_type {
        task get_return_object() {
            auto handle = std::coroutine_handle<promise_type>::from_promise(*this);
            return {handle};
        }
        std::suspend_always initial_suspend() { return {}; }
        std::suspend_never final_suspend() { return {}; }
        void return_void() {}
    };

    task(std::coroutine_handle<promise_type> h) : schedulable(h) {}
};