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:
		
							parent
							
								
									8d1570f44a
								
							
						
					
					
						commit
						daf9a3cc07
					
				
					 8 changed files with 134 additions and 16 deletions
				
			
		|  | @ -1 +1 @@ | ||||||
| set makeprg=meson\ compile\ -C\ . | set makeprg=meson\ compile\ -j\ 4\ -C\ . | ||||||
|  |  | ||||||
							
								
								
									
										2
									
								
								Makefile
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								Makefile
									
										
									
									
									
								
							|  | @ -3,7 +3,7 @@ | ||||||
| all: build test | all: build test | ||||||
| 
 | 
 | ||||||
| build: | build: | ||||||
| 	meson compile -C builddir | 	meson compile -j 4 -C builddir | ||||||
| 
 | 
 | ||||||
| test: | test: | ||||||
| 	meson test -C builddir --suite turings_tarpit | 	meson test -C builddir --suite turings_tarpit | ||||||
|  |  | ||||||
|  | @ -56,6 +56,8 @@ void Builder::run_build() { | ||||||
|   while(fgets(buffer, BUF_MAX, build_out) != nullptr) { |   while(fgets(buffer, BUF_MAX, build_out) != nullptr) { | ||||||
|     string line(buffer);  // yeah, that's probably a problem
 |     string line(buffer);  // yeah, that's probably a problem
 | ||||||
| 
 | 
 | ||||||
|  |     cerr << buffer; | ||||||
|  | 
 | ||||||
|     smatch err; |     smatch err; | ||||||
|     bool match = regex_match(line, err, err_re); |     bool match = regex_match(line, err, err_re); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										125
									
								
								corotest.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										125
									
								
								corotest.cpp
									
										
									
									
									
										Normal 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; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | @ -19,7 +19,7 @@ int GameEngine::determine_damage(string &type) { | ||||||
|     return damage_types.at(type); |     return damage_types.at(type); | ||||||
|   } catch(std::out_of_range &err) { |   } catch(std::out_of_range &err) { | ||||||
|     print(ERROR, "BAD DAMAGE TYPE {}\n", type); |     print(ERROR, "BAD DAMAGE TYPE {}\n", type); | ||||||
|     return 1000; |     return 0; | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										15
									
								
								meson.build
									
										
									
									
									
								
							
							
						
						
									
										15
									
								
								meson.build
									
										
									
									
									
								
							|  | @ -15,9 +15,6 @@ libgit2package_dep = libgit2_proj.dependency('libgit2package') | ||||||
| 
 | 
 | ||||||
| efsw_dep = dependency('efsw') | efsw_dep = dependency('efsw') | ||||||
| fmt = dependency('fmt') | fmt = dependency('fmt') | ||||||
| ftxui_screen = dependency('ftxui-screen') |  | ||||||
| ftxui_dom = dependency('ftxui-dom') |  | ||||||
| ftxui_component = dependency('ftxui-component') |  | ||||||
| catch2 = dependency('catch2-with-main') | catch2 = dependency('catch2-with-main') | ||||||
| sfml = dependency('sfml') | sfml = dependency('sfml') | ||||||
| json = dependency('nlohmann_json') | json = dependency('nlohmann_json') | ||||||
|  | @ -25,7 +22,6 @@ imgui = dependency('imgui-sfml') | ||||||
| 
 | 
 | ||||||
| dependencies = [ | dependencies = [ | ||||||
|   fmt, libgit2package_dep, efsw_dep, |   fmt, libgit2package_dep, efsw_dep, | ||||||
|   ftxui_screen, ftxui_dom, ftxui_component, |  | ||||||
|   sfml, imgui, json |   sfml, imgui, json | ||||||
| ] | ] | ||||||
| 
 | 
 | ||||||
|  | @ -36,18 +32,11 @@ executable('escape_turings_tarpit', | ||||||
|     'builder.cpp', |     'builder.cpp', | ||||||
|     'sfmlgui.cpp', |     'sfmlgui.cpp', | ||||||
|     'escape_turings_tarpit.cpp'], |     'escape_turings_tarpit.cpp'], | ||||||
|   win_subsystem: 'windows', |  | ||||||
|   dependencies: dependencies) |   dependencies: dependencies) | ||||||
| 
 | 
 | ||||||
| executable('regtest', 'regtest.cpp', | executable('regtest', 'regtest.cpp', | ||||||
|   dependencies: [fmt]) |   dependencies: [fmt]) | ||||||
| 
 | 
 | ||||||
| executable('ftxtest', 'ftxtest.cpp', |  | ||||||
|   dependencies: dependencies) |  | ||||||
| 
 |  | ||||||
| executable('ftx_thread_test', 'ftx_thread_test.cpp', |  | ||||||
|   dependencies: dependencies) |  | ||||||
| 
 |  | ||||||
| executable('audiotest', 'audiotest.cpp', | executable('audiotest', 'audiotest.cpp', | ||||||
|   dependencies: dependencies) |   dependencies: dependencies) | ||||||
| 
 | 
 | ||||||
|  | @ -57,6 +46,10 @@ executable('jsontest', 'jsontest.cpp', | ||||||
| executable('threadtest', 'threadtest.cpp', | executable('threadtest', 'threadtest.cpp', | ||||||
|   dependencies: dependencies) |   dependencies: dependencies) | ||||||
| 
 | 
 | ||||||
|  | executable('corotest', 'corotest.cpp', | ||||||
|  |   dependencies: dependencies, | ||||||
|  |   cpp_args: '-fcoroutines') | ||||||
|  | 
 | ||||||
| runtests = executable('runtests', [ | runtests = executable('runtests', [ | ||||||
|   'game_engine.cpp', |   'game_engine.cpp', | ||||||
|   'tests/game_engine.cpp', |   'tests/game_engine.cpp', | ||||||
|  |  | ||||||
|  | @ -7,7 +7,6 @@ mkdir builddir | ||||||
| meson wrap install fmt | meson wrap install fmt | ||||||
| meson wrap install sqlite3 | meson wrap install sqlite3 | ||||||
| meson wrap install sqlitecpp | meson wrap install sqlitecpp | ||||||
| meson wrap install ftxui |  | ||||||
| meson wrap install catch2 | meson wrap install catch2 | ||||||
| meson wrap install libpng | meson wrap install libpng | ||||||
| meson wrap install vorbis | meson wrap install vorbis | ||||||
|  |  | ||||||
|  | @ -10,7 +10,6 @@ cp *.wrap subprojects | ||||||
| meson wrap install fmt | meson wrap install fmt | ||||||
| meson wrap install sqlite3 | meson wrap install sqlite3 | ||||||
| meson wrap install sqlitecpp | meson wrap install sqlitecpp | ||||||
| meson wrap install ftxui |  | ||||||
| meson wrap install catch2 | meson wrap install catch2 | ||||||
| meson wrap install libpng | meson wrap install libpng | ||||||
| meson wrap install vorbis | meson wrap install vorbis | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Zed A. Shaw
						Zed A. Shaw