]> git.gag.com Git - debian/gnuradio/blobdiff - gnuradio-core/src/lib/runtime/gr_top_block_impl.cc
merged refactoring of gr_top_block_impl into abstract base class
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_top_block_impl.cc
index 359665a7727268e0d5401bfcbb431c3d875e4aad..857e9fa2dfacf5c2acd766d46083861208379a49 100644 (file)
@@ -1,6 +1,6 @@
 /* -*- c++ -*- */
 /*
- * Copyright 2007 Free Software Foundation, Inc.
+ * Copyright 2007,2008 Free Software Foundation, Inc.
  *
  * This file is part of GNU Radio
  *
 
 #define GR_TOP_BLOCK_IMPL_DEBUG 0
 
-static gr_top_block_impl *s_impl = 0;
-
-/*!
- * Make a vector of gr_block from a vector of gr_basic_block
- *
- * Pass-by-value to avoid problem with possible asynchronous modification
- */
-static gr_block_vector_t
-make_gr_block_vector(gr_basic_block_vector_t blocks)
-{
-  gr_block_vector_t result;
-  for (gr_basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) {
-    result.push_back(make_gr_block_sptr(*p));
-  }
-
-  return result;
-}
-
-// FIXME: This prevents using more than one gr_top_block instance
-
-static void 
-runtime_sigint_handler(int signum)
-{
-  if (GR_TOP_BLOCK_IMPL_DEBUG){
-    char *msg = "SIGINT received, calling stop()\n";
-    ::write(1, msg, strlen(msg));      // write is OK to call from signal handler
-  }
-
-  if (s_impl)
-    s_impl->stop();
-}
-
-// ----------------------------------------------------------------
-
 gr_top_block_impl::gr_top_block_impl(gr_top_block *owner) 
-  : d_running(false),
+  : d_owner(owner),
+    d_running(false),
     d_ffg(),
-    d_owner(owner),
     d_lock_count(0)
 {
-  if (s_impl)
-    throw std::logic_error("gr_top_block_impl: multiple simultaneous gr_top_blocks not allowed");
-
-  s_impl = this;
 }
 
 gr_top_block_impl::~gr_top_block_impl()
 {
-  s_impl = 0; // don't call delete we don't own these
   d_owner = 0;
 }
 
@@ -109,64 +70,6 @@ gr_top_block_impl::start()
   start_threads();
 }
 
-void
-gr_top_block_impl::start_threads()
-{
-  if (GR_TOP_BLOCK_IMPL_DEBUG)
-    std::cout << "start_threads: entered" << std::endl;
-
-  d_graphs = d_ffg->partition();
-  for (std::vector<gr_basic_block_vector_t>::iterator p = d_graphs.begin();
-       p != d_graphs.end(); p++) {
-    gr_scheduler_thread *thread = new gr_scheduler_thread(make_gr_block_vector(*p));
-    d_threads.push_back(thread);
-    if (GR_TOP_BLOCK_IMPL_DEBUG)
-      std::cout << "start_threads: starting " << thread << std::endl;
-    thread->start();
-  }
-
-  d_running = true;
-}
-
-/*
- * N.B. as currently implemented, it is possible that this may be
- * invoked by the SIGINT handler which is fragile as hell...
- */
-void
-gr_top_block_impl::stop()
-{
-  if (GR_TOP_BLOCK_IMPL_DEBUG){
-    char *msg = "stop: entered\n";
-    ::write(1, msg, strlen(msg));
-  }
-
-  for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
-    if (*p)
-      (*p)->stop();
-  }
-}
-
-void
-gr_top_block_impl::wait()
-{
-  if (GR_TOP_BLOCK_IMPL_DEBUG)
-    std::cout << "wait: entered" << std::endl;
-
-  void *dummy_status; // don't ever dereference this
-  gr_local_sighandler sigint(SIGINT, runtime_sigint_handler);
-
-  for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
-    if (GR_TOP_BLOCK_IMPL_DEBUG)
-      std::cout << "wait: joining thread " << (*p) << std::endl;
-    (*p)->join(&dummy_status); // pthreads will self-delete, so pointer is now dead
-    (*p) = 0; // FIXME: switch to stl::list and actually remove from container
-    if (GR_TOP_BLOCK_IMPL_DEBUG)
-      std::cout << "wait: join returned" << std::endl;
-  }
-
-  d_threads.clear();
-  d_running = false;
-}
 
 // N.B. lock() and unlock() cannot be called from a flow graph thread or
 // deadlock will occur when reconfiguration happens
@@ -238,3 +141,14 @@ gr_top_block_impl::dump()
   if (d_ffg)
     d_ffg->dump();
 }
+
+gr_block_vector_t
+gr_top_block_impl::make_gr_block_vector(gr_basic_block_vector_t blocks)
+{
+  gr_block_vector_t result;
+  for (gr_basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) {
+    result.push_back(make_gr_block_sptr(*p));
+  }
+
+  return result;
+}