Coroutines are mostly working but they're kind of junk anyway. Feel free to tinker with this if you like.

This commit is contained in:
Zed A. Shaw 2024-09-05 18:48:15 -04:00
parent 8d1570f44a
commit daf9a3cc07
8 changed files with 134 additions and 16 deletions

View file

@ -1 +1 @@
set makeprg=meson\ compile\ -C\ .
set makeprg=meson\ compile\ -j\ 4\ -C\ .

View file

@ -3,7 +3,7 @@
all: build test
build:
meson compile -C builddir
meson compile -j 4 -C builddir
test:
meson test -C builddir --suite turings_tarpit

View file

@ -56,6 +56,8 @@ void Builder::run_build() {
while(fgets(buffer, BUF_MAX, build_out) != nullptr) {
string line(buffer); // yeah, that's probably a problem
cerr << buffer;
smatch err;
bool match = regex_match(line, err, err_re);

125
corotest.cpp Normal file
View file

@ -0,0 +1,125 @@
#include <concepts>
#include <coroutine>
#include <exception>
#include <iostream>
#include <vector>
#include <assert.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
template<typename T>
struct Task {
struct promise_type;
using handle_type = std::coroutine_handle<promise_type>;
struct promise_type {
T value_;
std::exception_ptr exception_;
Task get_return_object() {
return Task(handle_type::from_promise(*this));
}
std::suspend_always initial_suspend() {
return {};
}
std::suspend_always final_suspend() noexcept {
return {};
}
void unhandled_exception() {
exception_ = std::current_exception();
}
template<std::convertible_to<T> From> // C++20 concept
void return_value(From &&from) {
value_ = std::forward<From>(from);
}
template<std::convertible_to<T> From> // C++20 concept
std::suspend_always yield_value(From &&from) {
value_ = std::forward<From>(from);
return {};
}
void return_void() {}
};
handle_type h_;
Task() {
}
Task(handle_type h) : h_(h) {
}
Task(const Task &t) : h_(t.h_) {
}
void destroy() {
h_.destroy();
}
T operator()() {
assert(!h_.done());
call();
return std::move(h_.promise().value_);
}
bool done() {
return h_.done();
}
private:
void call() {
h_();
if (h_.promise().exception_)
std::rethrow_exception(h_.promise().exception_);
}
};
#define pass() co_await std::suspend_always{}
Task<unsigned> task_test()
{
pass();
for (unsigned i = 0; i < 3; ++i)
co_yield i;
co_return 1000;
}
int main()
{
const int task_count = 4;
vector<Task<unsigned>> tasks;
for(int i = 0; i < task_count; i++) {
auto t = task_test();
tasks.push_back(std::move(t));
}
int done_count = 0;
while(done_count < task_count) {
for(int i = 0; i < task_count; i++) {
Task<unsigned> &t = tasks[i];
if(t.done()) {
t.destroy();
done_count++;
} else {
auto res = t();
cout << "T# " << i << " result "
<< res << endl;
}
}
}
}

View file

@ -19,7 +19,7 @@ int GameEngine::determine_damage(string &type) {
return damage_types.at(type);
} catch(std::out_of_range &err) {
print(ERROR, "BAD DAMAGE TYPE {}\n", type);
return 1000;
return 0;
}
}

View file

@ -15,9 +15,6 @@ libgit2package_dep = libgit2_proj.dependency('libgit2package')
efsw_dep = dependency('efsw')
fmt = dependency('fmt')
ftxui_screen = dependency('ftxui-screen')
ftxui_dom = dependency('ftxui-dom')
ftxui_component = dependency('ftxui-component')
catch2 = dependency('catch2-with-main')
sfml = dependency('sfml')
json = dependency('nlohmann_json')
@ -25,7 +22,6 @@ imgui = dependency('imgui-sfml')
dependencies = [
fmt, libgit2package_dep, efsw_dep,
ftxui_screen, ftxui_dom, ftxui_component,
sfml, imgui, json
]
@ -36,18 +32,11 @@ executable('escape_turings_tarpit',
'builder.cpp',
'sfmlgui.cpp',
'escape_turings_tarpit.cpp'],
win_subsystem: 'windows',
dependencies: dependencies)
executable('regtest', 'regtest.cpp',
dependencies: [fmt])
executable('ftxtest', 'ftxtest.cpp',
dependencies: dependencies)
executable('ftx_thread_test', 'ftx_thread_test.cpp',
dependencies: dependencies)
executable('audiotest', 'audiotest.cpp',
dependencies: dependencies)
@ -57,6 +46,10 @@ executable('jsontest', 'jsontest.cpp',
executable('threadtest', 'threadtest.cpp',
dependencies: dependencies)
executable('corotest', 'corotest.cpp',
dependencies: dependencies,
cpp_args: '-fcoroutines')
runtests = executable('runtests', [
'game_engine.cpp',
'tests/game_engine.cpp',

View file

@ -7,7 +7,6 @@ mkdir builddir
meson wrap install fmt
meson wrap install sqlite3
meson wrap install sqlitecpp
meson wrap install ftxui
meson wrap install catch2
meson wrap install libpng
meson wrap install vorbis

View file

@ -10,7 +10,6 @@ cp *.wrap subprojects
meson wrap install fmt
meson wrap install sqlite3
meson wrap install sqlitecpp
meson wrap install ftxui
meson wrap install catch2
meson wrap install libpng
meson wrap install vorbis