3 * Copyright 2006,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_runtime.h>
28 #include <gr_runtime_impl.h>
29 #include <gr_flat_flowgraph.h>
30 #include <gr_hier_block2.h>
31 #include <gr_hier_block2_detail.h>
32 #include <gr_local_sighandler.h>
41 #define GR_RUNTIME_IMPL_DEBUG 0
43 static gr_runtime_impl *s_runtime = 0;
45 // Make a vector of gr_block from a vector of gr_basic_block
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_runtime instance
60 runtime_sigint_handler(int signum)
62 if (GR_RUNTIME_IMPL_DEBUG)
63 std::cout << "SIGINT received, calling stop() on all threads" << std::endl;
69 gr_runtime_impl::gr_runtime_impl(gr_hier_block2_sptr top_block, gr_runtime *owner)
71 d_top_block(top_block),
72 d_ffg(gr_make_flat_flowgraph()),
76 top_block->set_runtime(d_owner);
79 gr_runtime_impl::~gr_runtime_impl()
81 s_runtime = 0; // don't call delete we don't own these
86 gr_runtime_impl::start()
88 if (GR_RUNTIME_IMPL_DEBUG)
89 std::cout << "start: entered" << std::endl;
92 throw std::runtime_error("already running");
94 // Create new simple flow graph by flattening hierarchical block
96 d_top_block->d_detail->flatten(d_ffg);
98 // Validate new simple flow graph and wire it up
100 d_ffg->setup_connections();
102 // Execute scheduler threads
107 gr_runtime_impl::start_threads()
109 if (GR_RUNTIME_IMPL_DEBUG)
110 std::cout << "start_threads: entered" << std::endl;
112 d_graphs = d_ffg->partition();
113 for (std::vector<gr_basic_block_vector_t>::iterator p = d_graphs.begin();
114 p != d_graphs.end(); p++) {
115 gr_scheduler_thread *thread = new gr_scheduler_thread(make_gr_block_vector(*p));
116 d_threads.push_back(thread);
117 if (GR_RUNTIME_IMPL_DEBUG)
118 std::cout << "start_threads: starting " << thread << std::endl;
126 gr_runtime_impl::stop()
128 if (GR_RUNTIME_IMPL_DEBUG)
129 std::cout << "stop: entered" << std::endl;
131 for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
132 if (GR_RUNTIME_IMPL_DEBUG)
133 std::cout << "stop: stopping thread " << (*p) << std::endl;
141 gr_runtime_impl::wait()
143 if (GR_RUNTIME_IMPL_DEBUG)
144 std::cout << "wait: entered" << std::endl;
146 void *dummy_status; // don't ever dereference this
147 gr_local_sighandler sigint(SIGINT, runtime_sigint_handler);
149 for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
150 if (GR_RUNTIME_IMPL_DEBUG)
151 std::cout << "wait: joining thread " << (*p) << std::endl;
152 (*p)->join(&dummy_status); // pthreads will self-delete, so pointer is now dead
153 (*p) = 0; // FIXME: switch to stl::list and actually remove from container
154 if (GR_RUNTIME_IMPL_DEBUG)
155 std::cout << "wait: join returned" << std::endl;
162 // N.B. lock() and unlock() cannot be called from a flow graph thread or
163 // deadlock will occur when reconfiguration happens
165 gr_runtime_impl::lock()
167 omni_mutex_lock lock(d_reconf);
169 if (GR_RUNTIME_IMPL_DEBUG)
170 std::cout << "runtime: locked, count = " << d_lock_count << std::endl;
174 gr_runtime_impl::unlock()
176 omni_mutex_lock lock(d_reconf);
177 if (d_lock_count == 0)
178 throw std::runtime_error("unpaired unlock() call");
181 if (GR_RUNTIME_IMPL_DEBUG)
182 std::cout << "runtime: unlocked, count = " << d_lock_count << std::endl;
184 if (d_lock_count == 0)
189 gr_runtime_impl::restart()
191 if (GR_RUNTIME_IMPL_DEBUG)
192 std::cout << "restart: entered" << std::endl;
195 throw std::runtime_error("not running");
197 // Stop scheduler threads and wait for completion
200 if (GR_RUNTIME_IMPL_DEBUG)
201 std::cout << "restart: threads stopped" << std::endl;
203 // Create new simple flow graph
204 gr_flat_flowgraph_sptr new_ffg = gr_make_flat_flowgraph();
205 d_top_block->d_detail->flatten(new_ffg);
207 new_ffg->merge_connections(d_ffg);
209 if (GR_RUNTIME_IMPL_DEBUG)
210 std::cout << "restart: replacing old flow graph with new" << std::endl;
216 gr_scheduler_thread::gr_scheduler_thread(gr_block_vector_t graph) :
217 omni_thread(NULL, PRIORITY_NORMAL),
218 d_sts(gr_make_single_threaded_scheduler(graph))
222 gr_scheduler_thread::~gr_scheduler_thread()
226 void gr_scheduler_thread::start()
232 gr_scheduler_thread::run_undetached(void *arg)
234 // First code to run in new thread context
236 // Mask off SIGINT in this thread to gaurantee mainline thread gets signal
237 #ifdef HAVE_SIGPROCMASK
240 sigemptyset(&new_set);
241 sigaddset(&new_set, SIGINT);
242 sigprocmask(SIG_BLOCK, &new_set, &old_set);
244 // Run the single-threaded scheduler
250 gr_scheduler_thread::stop()