소스 검색

Simplify the implementation of run_coroutines

Ian Goldberg 1 년 전
부모
커밋
fc10eb1250
2개의 변경된 파일12개의 추가작업 그리고 29개의 파일을 삭제
  1. 2 2
      Makefile
  2. 10 27
      coroutine.hpp

+ 2 - 2
Makefile

@@ -13,8 +13,8 @@ $(BIN): $(OBJS)
 oblivds.o: preproc.hpp mpcio.hpp types.hpp
 mpcio.o: mpcio.hpp types.hpp
 preproc.o: preproc.hpp mpcio.hpp types.hpp
-online.o: online.hpp mpcops.hpp
-mpcops.o: mpcops.hpp
+online.o: online.hpp mpcops.hpp coroutine.hpp
+mpcops.o: mpcops.hpp coroutine.hpp
 
 clean:
 	-rm -f $(BIN) $(OBJS) *.p[01].t*

+ 10 - 27
coroutine.hpp

@@ -7,33 +7,16 @@
 typedef boost::coroutines2::coroutine<void>::pull_type  coro_t;
 typedef boost::coroutines2::coroutine<void>::push_type  yield_t;
 
-// Use this version from the top level; i.e., not running in a coroutine
-// itself
-inline void run_coroutines(MPCTIO &tio, std::vector<coro_t> &coroutines) {
-    // Loop until all the coroutines are finished
-    bool finished = false;
-    while(!finished) {
-        // If this current function is not itself a coroutine (i.e.,
-        // this is the top-level function that launches all the
-        // coroutines), here's where to call send().  Otherwise, call
-        // yield() here to let other coroutines at this level run.
-        tio.send();
-        finished = true;
-        for (auto &c : coroutines) {
-            // This tests if coroutine c still has work to do (is not
-            // finished)
-            if (c) {
-                finished = false;
-                // Resume coroutine c from the point it yield()ed
-                c();
-            }
-        }
-    }
-}
+// The top-level coroutine runner will call run_coroutines with
+// a MPCTIO, and we should call its send() method.  Subcoroutines that
+// launch their own coroutines (and coroutine running) will call
+// run_coroutines with a yield_t instead, which we should just call, in
+// order to yield to the next higher level of coroutine runner.
+static inline void send_or_yield(MPCTIO &tio) { tio.send(); }
+static inline void send_or_yield(yield_t &yield) { yield(); }
 
-// Use this version if a coroutine needs to itself use multiple
-// subcoroutines.
-inline void run_coroutines(yield_t &yield, std::vector<coro_t> &coroutines) {
+template <typename T>
+inline void run_coroutines(T &mpctio_or_yield, std::vector<coro_t> &coroutines) {
     // Loop until all the coroutines are finished
     bool finished = false;
     while(!finished) {
@@ -41,7 +24,7 @@ inline void run_coroutines(yield_t &yield, std::vector<coro_t> &coroutines) {
         // this is the top-level function that launches all the
         // coroutines), here's where to call send().  Otherwise, call
         // yield() here to let other coroutines at this level run.
-        yield();
+        send_or_yield(mpctio_or_yield);
         finished = true;
         for (auto &c : coroutines) {
             // This tests if coroutine c still has work to do (is not