From c123e981789319e52c360f109c22acab77a7020f Mon Sep 17 00:00:00 2001 From: q66 Date: Sun, 19 Mar 2017 18:12:08 +0100 Subject: [PATCH] some universal funcs for working with schedulers --- examples/concurrency.cc | 6 +++--- ostd/concurrency.hh | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/examples/concurrency.cc b/examples/concurrency.cc index f659bb1..500bdf0 100644 --- a/examples/concurrency.cc +++ b/examples/concurrency.cc @@ -7,12 +7,12 @@ int main() { auto foo = [](auto &sched) { auto arr = ostd::iter({ 150, 38, 76, 25, 67, 18, -15, 215, 25, -10 }); - auto c = sched.template make_channel(); + auto c = make_channel(sched); auto f = [](auto c, auto half) { c.put(foldl(half, 0)); }; - sched.spawn(f, c, arr.slice(0, arr.size() / 2)); - sched.spawn(f, c, arr.slice(arr.size() / 2, arr.size())); + spawn(sched, f, c, arr.slice(0, arr.size() / 2)); + spawn(sched, f, c, arr.slice(arr.size() / 2, arr.size())); int a, b; c.get(a), c.get(b); diff --git a/ostd/concurrency.hh b/ostd/concurrency.hh index 6269a2f..620410a 100644 --- a/ostd/concurrency.hh +++ b/ostd/concurrency.hh @@ -16,6 +16,9 @@ namespace ostd { struct thread_scheduler { + template + using channel_type = channel; + ~thread_scheduler() { join_all(); } @@ -118,6 +121,9 @@ private: }; public: + template + using channel_type = channel; + template auto start(F &&func, A &&...args) -> std::result_of_t { using R = std::result_of_t; @@ -188,6 +194,21 @@ private: typename std::list::iterator p_idx = p_coros.end(); }; +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