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>
36 #define GR_TOP_BLOCK_IMPL_DEBUG 0
38 static gr_top_block_impl *s_impl = 0;
40 // Make a vector of gr_block from a vector of gr_basic_block
43 make_gr_block_vector(gr_basic_block_vector_t &blocks)
45 gr_block_vector_t result;
46 for (gr_basic_block_viter_t p = blocks.begin(); p != blocks.end(); p++) {
47 result.push_back(make_gr_block_sptr(*p));
53 // FIXME: This prevents using more than one gr_top_block instance
55 runtime_sigint_handler(int signum)
57 if (GR_TOP_BLOCK_IMPL_DEBUG)
58 std::cout << "SIGINT received, calling stop()" << std::endl;
64 gr_top_block_impl::gr_top_block_impl(gr_top_block *owner)
66 d_ffg(gr_make_flat_flowgraph()),
73 gr_top_block_impl::~gr_top_block_impl()
75 s_impl = 0; // don't call delete we don't own these
80 gr_top_block_impl::start()
82 if (GR_TOP_BLOCK_IMPL_DEBUG)
83 std::cout << "start: entered" << std::endl;
86 throw std::runtime_error("already running");
88 // Create new flat flow graph by flattening hierarchy
90 d_owner->flatten(d_ffg);
92 // Validate new simple flow graph and wire it up
94 d_ffg->setup_connections();
96 // Execute scheduler threads
101 gr_top_block_impl::start_threads()
103 if (GR_TOP_BLOCK_IMPL_DEBUG)
104 std::cout << "start_threads: entered" << std::endl;
106 d_graphs = d_ffg->partition();
107 for (std::vector<gr_basic_block_vector_t>::iterator p = d_graphs.begin();
108 p != d_graphs.end(); p++) {
109 gr_scheduler_thread *thread = new gr_scheduler_thread(make_gr_block_vector(*p));
110 d_threads.push_back(thread);
111 if (GR_TOP_BLOCK_IMPL_DEBUG)
112 std::cout << "start_threads: starting " << thread << std::endl;
120 gr_top_block_impl::stop()
122 if (GR_TOP_BLOCK_IMPL_DEBUG)
123 std::cout << "stop: entered" << std::endl;
125 for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
126 if (GR_TOP_BLOCK_IMPL_DEBUG)
127 std::cout << "stop: stopping thread " << (*p) << std::endl;
135 gr_top_block_impl::wait()
137 if (GR_TOP_BLOCK_IMPL_DEBUG)
138 std::cout << "wait: entered" << std::endl;
140 void *dummy_status; // don't ever dereference this
141 gr_local_sighandler sigint(SIGINT, runtime_sigint_handler);
143 for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
144 if (GR_TOP_BLOCK_IMPL_DEBUG)
145 std::cout << "wait: joining thread " << (*p) << std::endl;
146 (*p)->join(&dummy_status); // pthreads will self-delete, so pointer is now dead
147 (*p) = 0; // FIXME: switch to stl::list and actually remove from container
148 if (GR_TOP_BLOCK_IMPL_DEBUG)
149 std::cout << "wait: join returned" << std::endl;
155 // N.B. lock() and unlock() cannot be called from a flow graph thread or
156 // deadlock will occur when reconfiguration happens
158 gr_top_block_impl::lock()
160 omni_mutex_lock lock(d_reconf);
162 if (GR_TOP_BLOCK_IMPL_DEBUG)
163 std::cout << "runtime: locked, count = " << d_lock_count << std::endl;
167 gr_top_block_impl::unlock()
169 omni_mutex_lock lock(d_reconf);
170 if (d_lock_count == 0)
171 throw std::runtime_error("unpaired unlock() call");
174 if (GR_TOP_BLOCK_IMPL_DEBUG)
175 std::cout << "unlock: unlocked, count = " << d_lock_count << std::endl;
177 if (d_lock_count == 0) {
178 if (GR_TOP_BLOCK_IMPL_DEBUG)
179 std::cout << "unlock: restarting flowgraph" << std::endl;
185 gr_top_block_impl::restart()
187 if (GR_TOP_BLOCK_IMPL_DEBUG)
188 std::cout << "restart: entered" << std::endl;
191 throw std::runtime_error("not running");
193 // Stop scheduler threads and wait for completion
196 if (GR_TOP_BLOCK_IMPL_DEBUG)
197 std::cout << "restart: threads stopped" << std::endl;
199 // Create new simple flow graph
200 gr_flat_flowgraph_sptr new_ffg = gr_make_flat_flowgraph();
201 d_owner->flatten(new_ffg);
203 new_ffg->merge_connections(d_ffg);
205 if (GR_TOP_BLOCK_IMPL_DEBUG)
206 std::cout << "restart: replacing old flow graph with new" << std::endl;