/* Concurrency module with custom scheduler support. * * This file is part of OctaSTD. See COPYING.md for futher information. */ #ifndef OSTD_CONCURRENCY_HH #define OSTD_CONCURRENCY_HH #include #include #include #include "ostd/coroutine.hh" #include "ostd/channel.hh" namespace ostd { struct thread_scheduler { template using channel_type = channel; ~thread_scheduler() { join_all(); } template auto start(F &&func, A &&...args) -> std::result_of_t { return func(std::forward(args)...); } template void spawn(F &&func, A &&...args) { std::lock_guard l{p_lock}; p_threads.emplace_front(); auto it = p_threads.begin(); *it = std::thread{ [this, it](auto func, auto ...args) { func(std::move(args)...); this->remove_thread(it); }, std::forward(func), std::forward(args)... }; } void yield() { std::this_thread::yield(); } template channel make_channel() { return channel{}; } private: void remove_thread(typename std::list::iterator it) { std::lock_guard l{p_lock}; std::thread t{std::exchange(p_dead, std::move(*it))}; if (t.joinable()) { t.join(); } p_threads.erase(it); } void join_all() { /* wait for all threads to finish */ std::lock_guard l{p_lock}; if (p_dead.joinable()) { p_dead.join(); } for (auto &t: p_threads) { t.join(); } } std::list p_threads; std::thread p_dead; std::mutex p_lock; }; template struct basic_simple_coroutine_scheduler { private: /* simple one just for channels */ struct coro_cond { coro_cond() = delete; coro_cond(coro_cond const &) = delete; coro_cond(coro_cond &&) = delete; coro_cond &operator=(coro_cond const &) = delete; coro_cond &operator=(coro_cond &&) = delete; coro_cond(basic_simple_coroutine_scheduler &s): p_sched(s) {} template void wait(L &l) noexcept { l.unlock(); while (!p_notified) { p_sched.yield(); } p_notified = false; l.lock(); } void notify_one() noexcept { p_notified = true; p_sched.yield(); } void notify_all() noexcept { p_notified = true; p_sched.yield(); } private: basic_simple_coroutine_scheduler &p_sched; bool p_notified = false; }; public: template using channel_type = channel; basic_simple_coroutine_scheduler( size_t ss = TR::default_size(), size_t cs = basic_stack_pool::DEFAULT_CHUNK_SIZE ): p_stacks(ss, cs), p_dispatcher([this](auto yield_main) { this->dispatch(yield_main); }, p_stacks.get_allocator()), p_coros() {} template auto start(F &&func, A &&...args) -> std::result_of_t { using R = std::result_of_t; if constexpr(std::is_same_v) { func(std::forward(args)...); finish(); } else { auto ret = func(std::forward(args)...); finish(); return ret; } } template void spawn(F &&func, A &&...args) { if constexpr(sizeof...(A) == 0) { p_coros.emplace_back([lfunc = std::forward(func)](auto) { lfunc(); }, p_stacks.get_allocator()); } else { p_coros.emplace_back([lfunc = std::bind( std::forward(func), std::forward(args)... )](auto) mutable { lfunc(); }, p_stacks.get_allocator()); } } void yield() { auto ctx = coroutine_context::current(); if (!ctx) { /* yield from main means go to dispatcher and call first task */ p_idx = p_coros.begin(); p_dispatcher(); return; } coro *c = dynamic_cast(ctx); if (c) { typename coro::yield_type{*c}(); return; } throw std::runtime_error{"attempt to yield outside coroutine"}; } template channel make_channel() { return channel{[this]() { return coro_cond{*this}; }}; } private: struct coro: coroutine { using coroutine::coroutine; }; void dispatch(typename coro::yield_type &yield_main) { while (!p_coros.empty()) { if (p_idx == p_coros.end()) { /* we're at the end; it's time to return to main and * continue there (potential yield from main results * in continuing from this point with the first task) */ yield_main(); continue; } (*p_idx)(); if (!*p_idx) { p_idx = p_coros.erase(p_idx); } else { ++p_idx; } } } void finish() { /* main has finished, but there might be either suspended or never * started tasks in the queue; dispatch until there are none left */ while (!p_coros.empty()) { p_idx = p_coros.begin(); p_dispatcher(); } } basic_stack_pool p_stacks; coro p_dispatcher; std::list p_coros; typename std::list::iterator p_idx = p_coros.end(); }; using simple_coroutine_scheduler = basic_simple_coroutine_scheduler; using protected_simple_coroutine_scheduler = basic_simple_coroutine_scheduler; template inline void spawn(S &sched, F &&func, A &&...args) { sched.spawn(std::forward(func), std::forward(args)...); } template inline void yield(S &sched) { sched.yield(); } template inline auto make_channel(S &sched) -> typename S::template channel_type { return sched.template make_channel(); } } /* namespace ostd */ #endif