-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathevent_loop.cpp
228 lines (179 loc) · 5.5 KB
/
event_loop.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "codebuilder.hpp"
#include "../ext/libriscv/binaries/barebones/libc/include/event_loop.hpp"
TEST_CASE("Simple event loop", "[EventLoop]")
{
const auto program = build_and_load(R"M(
#include <api.h>
#include <include/event_loop.hpp>
static std::array<Events<>, 2> events;
PUBLIC(void event_loop())
{
api::print("Entering event loop...\n");
while (true)
{
for (auto& ev : events)
ev.consume_work();
halt();
}
}
PUBLIC(bool add_work(void(*callback)(void*), void* arg))
{
EXPECT(callback != nullptr);
for (auto& ev : events)
if (ev.add([callback, arg] {
callback(arg);
}))
{
return true;
}
return false;
}
struct WorkObject {
int work_done = 0;
};
PUBLIC(void some_work(WorkObject& obj))
{
obj.work_done += 1;
}
int main() {
})M");
// Load the program and run through main()
Script script {program, "MyScript", "/tmp/myscript"};
// Get the address of 'event_loop' and 'add_work'
const auto event_loop = script.address_of("event_loop");
const auto add_work = script.address_of("add_work");
REQUIRE(event_loop != 0x0);
REQUIRE(add_work != 0x0);
// Allocate 16 objects on the heap inside the script program
struct WorkObject {
int work_done = 0;
};
auto shared_objs = script.guest_alloc<WorkObject>(16);
// Start the event loop
script.call(event_loop);
REQUIRE(shared_objs.at(0).work_done == 0);
// Add some work with the first guest object as argument
// NOTE: Using preempt() in order to not break the event loop!
script.preempt(add_work, script.address_of("some_work"), shared_objs.address(0));
// The work is still not completed
REQUIRE(shared_objs.at(0).work_done == 0);
// Let the event loop run for a bit
script.resume(1000);
// The work is now completed
REQUIRE(shared_objs.at(0).work_done == 1);
// Let the event loop run for a bit
script.resume(1000);
// The work is not *more* done
REQUIRE(shared_objs.at(0).work_done == 1);
for (size_t i = 0; i < 10; i++) {
// Add some work with the first guest object as argument
// NOTE: Using preempt() in order to not break the event loop!
script.preempt(add_work, script.address_of("some_work"), shared_objs.address(0));
}
// The work is *still* not *more* done
REQUIRE(shared_objs.at(0).work_done == 1);
// Let the event loop run for a bit
script.resume(400);
// More work has been done, but not all of it
REQUIRE(shared_objs.at(0).work_done >= 2);
// Let the event loop run until it's done
script.resume(100'000);
// All of the work has been done
REQUIRE(shared_objs.at(0).work_done == 11);
}
TEST_CASE("Advanced event loop", "[EventLoop]")
{
const auto program = build_and_load(R"M(
#include <api.h>
#include <include/event_loop.hpp>
PUBLIC(void event_loop(SharedEvents<>& ev0, SharedEvents<>& ev1))
{
api::print("Entering event loop...\n");
while (true)
{
try {
while (ev0.has_work() || ev1.has_work()) {
ev0.consume_work();
ev1.consume_work();
}
halt();
} catch (const std::exception& e) {
api::print("Event loop exception: ", e.what(), "\n");
} catch (...) {
//api::print("Event loop unknown exception\n");
}
}
}
struct WorkObject {
int work_done = 0;
};
PUBLIC(void some_work(WorkObject& obj))
{
obj.work_done += 1;
}
PUBLIC(void some_erroneous_work(WorkObject& obj))
{
obj.work_done += 1;
throw "";
}
int main() {
})M");
// Load the program and run through main()
Script script {program, "MyScript", "/tmp/myscript"};
// Allocate 16 objects on the heap inside the script program
struct WorkObject {
int work_done = 0;
};
auto shared_objs = script.guest_alloc<WorkObject>(16);
auto shared_evs = script.guest_alloc<SharedEvents<>>(2);
// Start the event loop
script.call("event_loop", shared_evs.address(0), shared_evs.address(1));
REQUIRE(shared_objs.at(0).work_done == 0);
// Create a function that picks a free event loop
auto select_free_ev = [&shared_evs] () -> SharedEvents<>& {
if (!shared_evs.at(0).in_use)
return shared_evs.at(0);
return shared_evs.at(1);
};
// Add some work with the first guest object as argument
auto& ev = select_free_ev();
ev.host_add(script.address_of("some_work"), shared_objs.address(0));
// The work is still not completed
REQUIRE(shared_objs.at(0).work_done == 0);
// Let the event loop run for a bit
script.resume(1000);
// The work is now completed
REQUIRE(shared_objs.at(0).work_done == 1);
const auto some_work = script.address_of("some_work");
shared_objs.at(0).work_done = 0;
int expected_work = 0;
// Run event loop 10k times, and keep adding work
// Remember how much work was actually added
for (int i = 0; i < 10000; i++) {
auto& ev = select_free_ev();
if (ev.host_add(some_work, shared_objs.address(0)))
expected_work += 1;
script.resume(100);
}
// Finish all work
script.resume(10'000'000ull);
REQUIRE(!shared_evs.at(0).has_work());
REQUIRE(!shared_evs.at(1).has_work());
REQUIRE(expected_work == shared_objs.at(0).work_done);
const auto some_erroneous_work = script.address_of("some_erroneous_work");
REQUIRE(some_erroneous_work != 0x0);
shared_objs.at(0).work_done = 0;
expected_work = 0;
// Run event loop 10k times, and keep adding problematic work
for (int i = 0; i < 10000; i++) {
auto& ev = select_free_ev();
if (ev.host_add(some_erroneous_work, shared_objs.address(0)))
expected_work += 1;
script.resume(30'000ull);
}
// Finish all work
script.resume(50'000'000ull);
REQUIRE(!shared_evs.at(0).has_work());
REQUIRE(!shared_evs.at(1).has_work());
REQUIRE(expected_work == shared_objs.at(0).work_done);
}