123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- #ifndef __COROUTINE_HPP__
- #define __COROUTINE_HPP__
- #include <vector>
- #include "corotypes.hpp"
- #include "mpcio.hpp"
- static inline void send_or_yield(MPCTIO &tio) { tio.send(); }
- static inline void send_or_yield(yield_t &yield) { yield(); }
- static inline int getset_communication_nthreads(MPCTIO &tio, int nthreads = 0) {
- return tio.comm_nthreads(nthreads);
- }
- static inline int getset_communication_nthreads(yield_t &yield, int nthreads = 0) {
- return 0;
- }
- template <typename T>
- inline void run_coroutines(T &mpctio_or_yield, std::vector<coro_t> &coroutines) {
-
-
-
-
- int saved_communication_nthreads = 0;
- if (coroutines.size() > 1) {
- saved_communication_nthreads =
- getset_communication_nthreads(mpctio_or_yield, 1);
- }
-
- bool finished = false;
- while(!finished) {
-
-
-
-
- send_or_yield(mpctio_or_yield);
- finished = true;
- for (auto &c : coroutines) {
-
-
- if (c) {
- finished = false;
-
- c();
- }
- }
- }
- if (saved_communication_nthreads > 0) {
- getset_communication_nthreads(mpctio_or_yield,
- saved_communication_nthreads);
- }
- }
- template <typename T>
- inline void run_coroutines(T &mpctio_or_yield, const coro_lambda_t &l1)
- {
- std::vector<coro_t> coroutines;
- coroutines.emplace_back(l1);
- run_coroutines(mpctio_or_yield, coroutines);
- }
- template <typename T>
- inline void run_coroutines(T &mpctio_or_yield, const coro_lambda_t &l1,
- const coro_lambda_t &l2)
- {
- std::vector<coro_t> coroutines;
- coroutines.emplace_back(l1);
- coroutines.emplace_back(l2);
- run_coroutines(mpctio_or_yield, coroutines);
- }
- template <typename T>
- inline void run_coroutines(T &mpctio_or_yield, const coro_lambda_t &l1,
- const coro_lambda_t &l2, const coro_lambda_t &l3)
- {
- std::vector<coro_t> coroutines;
- coroutines.emplace_back(l1);
- coroutines.emplace_back(l2);
- coroutines.emplace_back(l3);
- run_coroutines(mpctio_or_yield, coroutines);
- }
- template <typename T>
- inline void run_coroutines(T &mpctio_or_yield, const coro_lambda_t &l1,
- const coro_lambda_t &l2, const coro_lambda_t &l3,
- const coro_lambda_t &l4)
- {
- std::vector<coro_t> coroutines;
- coroutines.emplace_back(l1);
- coroutines.emplace_back(l2);
- coroutines.emplace_back(l3);
- coroutines.emplace_back(l4);
- run_coroutines(mpctio_or_yield, coroutines);
- }
- #endif
|