From 10b7f835854f46ac390fdb6954d312a6b0ba30e9 Mon Sep 17 00:00:00 2001
From: Andrej Rode <mail@andrejro.de>
Date: Thu, 13 Apr 2017 23:38:12 -0700
Subject: runtime: add barrier to synchronize block startup

---
 gnuradio-runtime/lib/scheduler_tpb.cc | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

(limited to 'gnuradio-runtime/lib/scheduler_tpb.cc')

diff --git a/gnuradio-runtime/lib/scheduler_tpb.cc b/gnuradio-runtime/lib/scheduler_tpb.cc
index 0319a43a88..fe593670f8 100644
--- a/gnuradio-runtime/lib/scheduler_tpb.cc
+++ b/gnuradio-runtime/lib/scheduler_tpb.cc
@@ -26,6 +26,7 @@
 #include "scheduler_tpb.h"
 #include "tpb_thread_body.h"
 #include <gnuradio/thread/thread_body_wrapper.h>
+#include <boost/make_shared.hpp>
 #include <sstream>
 
 namespace gr {
@@ -34,14 +35,15 @@ namespace gr {
   {
     block_sptr d_block;
     int d_max_noutput_items;
+    thread::barrier_sptr d_start_sync;
 
   public:
-    tpb_container(block_sptr block, int max_noutput_items)
-      : d_block(block), d_max_noutput_items(max_noutput_items) {}
+    tpb_container(block_sptr block, int max_noutput_items, thread::barrier_sptr start_sync)
+      : d_block(block), d_max_noutput_items(max_noutput_items), d_start_sync(start_sync) {}
 
     void operator()()
     {
-      tpb_thread_body body(d_block, d_max_noutput_items);
+      tpb_thread_body body(d_block, d_start_sync, d_max_noutput_items);
     }
   };
 
@@ -71,6 +73,9 @@ namespace gr {
       blocks[i]->detail()->set_done(false);
     }
 
+    thread::barrier_sptr start_sync =
+      boost::make_shared<thread::barrier>(blocks.size()+1);
+
     // Fire off a thead for each block
 
     for(size_t i = 0; i < blocks.size(); i++) {
@@ -84,12 +89,11 @@ namespace gr {
       else {
         block_max_noutput_items = max_noutput_items;
       }
-
-      d_threads.create_thread(
-	    gr::thread::thread_body_wrapper<tpb_container>
-            (tpb_container(blocks[i], block_max_noutput_items),
-             name.str()));
+      d_threads.create_thread(thread::thread_body_wrapper<tpb_container>(
+          tpb_container(blocks[i], block_max_noutput_items, start_sync),
+          name.str()));
     }
+    start_sync->wait();
   }
 
   scheduler_tpb::~scheduler_tpb()
-- 
cgit v1.2.3