3 * Copyright 2007 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
27 #include <gr_top_block.h>
28 #include <gr_top_block_impl.h>
29 #include <gr_flat_flowgraph.h>
30 #include <gr_scheduler_thread.h>
31 #include <gr_local_sighandler.h>
37 #define GR_TOP_BLOCK_IMPL_DEBUG 0
39 static gr_top_block_impl *s_impl = 0;
42 * Make a vector of gr_block from a vector of gr_basic_block
44 * Pass-by-value to avoid problem with possible asynchronous modification
46 static gr_block_vector_t
47 make_gr_block_vector(gr_basic_block_vector_t blocks)
49 gr_block_vector_t result;
50 for (gr_basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) {
51 result.push_back(make_gr_block_sptr(*p));
57 // FIXME: This prevents using more than one gr_top_block instance
60 runtime_sigint_handler(int signum)
62 if (GR_TOP_BLOCK_IMPL_DEBUG){
63 char *msg = "SIGINT received, calling stop()\n";
64 ::write(1, msg, strlen(msg)); // write is OK to call from signal handler
71 // ----------------------------------------------------------------
73 gr_top_block_impl::gr_top_block_impl(gr_top_block *owner)
80 throw std::logic_error("gr_top_block_impl: multiple simultaneous gr_top_blocks not allowed");
85 gr_top_block_impl::~gr_top_block_impl()
87 s_impl = 0; // don't call delete we don't own these
92 gr_top_block_impl::start()
94 if (GR_TOP_BLOCK_IMPL_DEBUG)
95 std::cout << "start: entered " << this << std::endl;
98 throw std::runtime_error("top block already running or wait() not called after previous stop()");
100 // Create new flat flow graph by flattening hierarchy
101 d_ffg = d_owner->flatten();
103 // Validate new simple flow graph and wire it up
105 d_ffg->setup_connections();
107 // Execute scheduler threads
112 gr_top_block_impl::start_threads()
114 if (GR_TOP_BLOCK_IMPL_DEBUG)
115 std::cout << "start_threads: entered" << std::endl;
117 d_graphs = d_ffg->partition();
118 for (std::vector<gr_basic_block_vector_t>::iterator p = d_graphs.begin();
119 p != d_graphs.end(); p++) {
120 gr_scheduler_thread *thread = new gr_scheduler_thread(make_gr_block_vector(*p));
121 d_threads.push_back(thread);
122 if (GR_TOP_BLOCK_IMPL_DEBUG)
123 std::cout << "start_threads: starting " << thread << std::endl;
131 * N.B. as currently implemented, it is possible that this may be
132 * invoked by the SIGINT handler which is fragile as hell...
135 gr_top_block_impl::stop()
137 if (GR_TOP_BLOCK_IMPL_DEBUG){
138 char *msg = "stop: entered\n";
139 ::write(1, msg, strlen(msg));
142 for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
149 gr_top_block_impl::wait()
151 if (GR_TOP_BLOCK_IMPL_DEBUG)
152 std::cout << "wait: entered" << std::endl;
154 void *dummy_status; // don't ever dereference this
155 gr_local_sighandler sigint(SIGINT, runtime_sigint_handler);
157 for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
158 if (GR_TOP_BLOCK_IMPL_DEBUG)
159 std::cout << "wait: joining thread " << (*p) << std::endl;
160 (*p)->join(&dummy_status); // pthreads will self-delete, so pointer is now dead
161 (*p) = 0; // FIXME: switch to stl::list and actually remove from container
162 if (GR_TOP_BLOCK_IMPL_DEBUG)
163 std::cout << "wait: join returned" << std::endl;
170 // N.B. lock() and unlock() cannot be called from a flow graph thread or
171 // deadlock will occur when reconfiguration happens
173 gr_top_block_impl::lock()
175 omni_mutex_lock lock(d_reconf);
177 if (GR_TOP_BLOCK_IMPL_DEBUG)
178 std::cout << "runtime: locked, count = " << d_lock_count << std::endl;
182 gr_top_block_impl::unlock()
184 omni_mutex_lock lock(d_reconf);
185 if (d_lock_count <= 0)
186 throw std::runtime_error("unpaired unlock() call");
189 if (GR_TOP_BLOCK_IMPL_DEBUG)
190 std::cout << "unlock: unlocked, count = " << d_lock_count << std::endl;
192 if (d_lock_count == 0) {
193 if (GR_TOP_BLOCK_IMPL_DEBUG)
194 std::cout << "unlock: restarting flowgraph" << std::endl;
200 gr_top_block_impl::restart()
202 if (GR_TOP_BLOCK_IMPL_DEBUG)
203 std::cout << "restart: entered" << std::endl;
206 throw std::runtime_error("top block is not running");
208 // Stop scheduler threads and wait for completion
211 if (GR_TOP_BLOCK_IMPL_DEBUG)
212 std::cout << "restart: threads stopped" << std::endl;
214 // Create new simple flow graph
215 gr_flat_flowgraph_sptr new_ffg = d_owner->flatten();
216 new_ffg->validate(); // check consistency, sanity, etc
217 new_ffg->merge_connections(d_ffg); // reuse buffers, etc
219 if (GR_TOP_BLOCK_IMPL_DEBUG)
220 std::cout << "restart: replacing old flow graph with new" << std::endl;