3 * Copyright 2007,2008 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_sts.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_STS_DEBUG 0
40 static gr_top_block_impl *s_impl = 0;
43 // FIXME: This prevents using more than one gr_top_block instance
46 runtime_sigint_handler(int signum)
48 if (GR_TOP_BLOCK_IMPL_STS_DEBUG){
49 char *msg = "SIGINT received, calling stop()\n";
50 ::write(1, msg, strlen(msg)); // write is OK to call from signal handler
57 // ----------------------------------------------------------------
59 gr_top_block_impl_sts::gr_top_block_impl_sts(gr_top_block *owner)
60 : gr_top_block_impl(owner)
63 throw std::logic_error("gr_top_block_impl_sts: multiple simultaneous gr_top_blocks not allowed");
68 gr_top_block_impl_sts::~gr_top_block_impl_sts()
70 s_impl = 0; // don't call delete we don't own these
74 gr_top_block_impl_sts::start_threads()
76 if (GR_TOP_BLOCK_IMPL_STS_DEBUG)
77 std::cout << "start_threads: entered" << std::endl;
79 d_graphs = d_ffg->partition();
80 for (std::vector<gr_basic_block_vector_t>::iterator p = d_graphs.begin();
81 p != d_graphs.end(); p++) {
82 gr_scheduler_thread *thread = new gr_scheduler_thread(make_gr_block_vector(*p));
83 d_threads.push_back(thread);
84 if (GR_TOP_BLOCK_IMPL_STS_DEBUG)
85 std::cout << "start_threads: starting " << thread << std::endl;
91 * N.B. as currently implemented, it is possible that this may be
92 * invoked by the SIGINT handler which is fragile as hell...
95 gr_top_block_impl_sts::stop()
97 if (GR_TOP_BLOCK_IMPL_STS_DEBUG){
98 char *msg = "stop: entered\n";
99 ::write(1, msg, strlen(msg));
102 for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
109 gr_top_block_impl_sts::wait()
111 if (GR_TOP_BLOCK_IMPL_STS_DEBUG)
112 std::cout << "wait: entered" << std::endl;
114 void *dummy_status; // don't ever dereference this
115 gr_local_sighandler sigint(SIGINT, runtime_sigint_handler);
117 for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
118 if (GR_TOP_BLOCK_IMPL_STS_DEBUG)
119 std::cout << "wait: joining thread " << (*p) << std::endl;
120 (*p)->join(&dummy_status); // omnithreads will self-delete, so pointer is now dead
121 (*p) = 0; // FIXME: switch to stl::list and actually remove from container
122 if (GR_TOP_BLOCK_IMPL_STS_DEBUG)
123 std::cout << "wait: join returned" << std::endl;