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>
38 #define GR_TOP_BLOCK_IMPL_DEBUG 0
40 static gr_top_block_impl *s_impl = 0;
43 * Make a vector of gr_block from a vector of gr_basic_block
45 * Pass-by-value to avoid problem with possible asynchronous modification
47 static gr_block_vector_t
48 make_gr_block_vector(gr_basic_block_vector_t blocks)
50 gr_block_vector_t result;
51 for (gr_basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) {
52 result.push_back(make_gr_block_sptr(*p));
58 // FIXME: This prevents using more than one gr_top_block instance
61 runtime_sigint_handler(int signum)
63 if (GR_TOP_BLOCK_IMPL_DEBUG){
64 char *msg = "SIGINT received, calling stop()\n";
65 ::write(1, msg, strlen(msg)); // write is OK to call from signal handler
72 // ----------------------------------------------------------------
74 gr_top_block_impl::gr_top_block_impl(gr_top_block *owner)
81 throw std::logic_error("gr_top_block_impl: multiple simultaneous gr_top_blocks not allowed");
86 gr_top_block_impl::~gr_top_block_impl()
88 s_impl = 0; // don't call delete we don't own these
93 gr_top_block_impl::start()
95 if (GR_TOP_BLOCK_IMPL_DEBUG)
96 std::cout << "start: entered " << this << std::endl;
99 throw std::runtime_error("top block already running or wait() not called after previous stop()");
101 // Create new flat flow graph by flattening hierarchy
102 d_ffg = d_owner->flatten();
104 // Validate new simple flow graph and wire it up
106 d_ffg->setup_connections();
108 // Execute scheduler threads
113 gr_top_block_impl::start_threads()
115 if (GR_TOP_BLOCK_IMPL_DEBUG)
116 std::cout << "start_threads: entered" << std::endl;
118 d_graphs = d_ffg->partition();
119 for (std::vector<gr_basic_block_vector_t>::iterator p = d_graphs.begin();
120 p != d_graphs.end(); p++) {
121 gr_scheduler_thread *thread = new gr_scheduler_thread(make_gr_block_vector(*p));
122 d_threads.push_back(thread);
123 if (GR_TOP_BLOCK_IMPL_DEBUG)
124 std::cout << "start_threads: starting " << thread << std::endl;
132 * N.B. as currently implemented, it is possible that this may be
133 * invoked by the SIGINT handler which is fragile as hell...
136 gr_top_block_impl::stop()
138 if (GR_TOP_BLOCK_IMPL_DEBUG){
139 char *msg = "stop: entered\n";
140 ::write(1, msg, strlen(msg));
143 for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
150 gr_top_block_impl::wait()
152 if (GR_TOP_BLOCK_IMPL_DEBUG)
153 std::cout << "wait: entered" << std::endl;
155 void *dummy_status; // don't ever dereference this
156 gr_local_sighandler sigint(SIGINT, runtime_sigint_handler);
158 for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
159 if (GR_TOP_BLOCK_IMPL_DEBUG)
160 std::cout << "wait: joining thread " << (*p) << std::endl;
161 (*p)->join(&dummy_status); // pthreads will self-delete, so pointer is now dead
162 (*p) = 0; // FIXME: switch to stl::list and actually remove from container
163 if (GR_TOP_BLOCK_IMPL_DEBUG)
164 std::cout << "wait: join returned" << std::endl;
171 // N.B. lock() and unlock() cannot be called from a flow graph thread or
172 // deadlock will occur when reconfiguration happens
174 gr_top_block_impl::lock()
176 omni_mutex_lock lock(d_reconf);
178 if (GR_TOP_BLOCK_IMPL_DEBUG)
179 std::cout << "runtime: locked, count = " << d_lock_count << std::endl;
183 gr_top_block_impl::unlock()
185 omni_mutex_lock lock(d_reconf);
186 if (d_lock_count <= 0)
187 throw std::runtime_error("unpaired unlock() call");
190 if (GR_TOP_BLOCK_IMPL_DEBUG)
191 std::cout << "unlock: unlocked, count = " << d_lock_count << std::endl;
193 if (d_lock_count == 0) {
194 if (GR_TOP_BLOCK_IMPL_DEBUG)
195 std::cout << "unlock: restarting flowgraph" << std::endl;
201 gr_top_block_impl::restart()
203 if (GR_TOP_BLOCK_IMPL_DEBUG)
204 std::cout << "restart: entered" << std::endl;
207 throw std::runtime_error("top block is not running");
209 // Stop scheduler threads and wait for completion
212 if (GR_TOP_BLOCK_IMPL_DEBUG)
213 std::cout << "restart: threads stopped" << std::endl;
215 // Create new simple flow graph
216 gr_flat_flowgraph_sptr new_ffg = d_owner->flatten();
217 new_ffg->validate(); // check consistency, sanity, etc
219 if (GR_TOP_BLOCK_IMPL_DEBUG) {
220 std::cout << std::endl << "*** Existing flat flowgraph @" << d_ffg << ":" << std::endl;
223 new_ffg->merge_connections(d_ffg); // reuse buffers, etc
225 if (GR_TOP_BLOCK_IMPL_DEBUG) {
226 std::cout << std::endl << "*** New flat flowgraph after merge @" << new_ffg << ":" << std::endl;