Merged r7478:7608 from michaelld/t186 into trunk. Adds ability to compile GNU Radio...
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_top_block_impl.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
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)
10  * any later version.
11  *
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.
16  *
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
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>
32
33 #include <stdexcept>
34 #include <iostream>
35 #include <string.h>
36
37 #define GR_TOP_BLOCK_IMPL_DEBUG 0
38
39 static gr_top_block_impl *s_impl = 0;
40
41 /*!
42  * Make a vector of gr_block from a vector of gr_basic_block
43  *
44  * Pass-by-value to avoid problem with possible asynchronous modification
45  */
46 static gr_block_vector_t
47 make_gr_block_vector(gr_basic_block_vector_t blocks)
48 {
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));
52   }
53
54   return result;
55 }
56
57 // FIXME: This prevents using more than one gr_top_block instance
58
59 static void 
60 runtime_sigint_handler(int signum)
61 {
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
65   }
66
67   if (s_impl)
68     s_impl->stop();
69 }
70
71 // ----------------------------------------------------------------
72
73 gr_top_block_impl::gr_top_block_impl(gr_top_block *owner) 
74   : d_running(false),
75     d_ffg(),
76     d_owner(owner),
77     d_lock_count(0)
78 {
79   if (s_impl)
80     throw std::logic_error("gr_top_block_impl: multiple simultaneous gr_top_blocks not allowed");
81
82   s_impl = this;
83 }
84
85 gr_top_block_impl::~gr_top_block_impl()
86 {
87   s_impl = 0; // don't call delete we don't own these
88   d_owner = 0;
89 }
90
91 void
92 gr_top_block_impl::start()
93 {
94   if (GR_TOP_BLOCK_IMPL_DEBUG)
95     std::cout << "start: entered " << this << std::endl;
96
97   if (d_running)
98     throw std::runtime_error("top block already running or wait() not called after previous stop()");
99
100   // Create new flat flow graph by flattening hierarchy
101   d_ffg = d_owner->flatten();
102
103   // Validate new simple flow graph and wire it up
104   d_ffg->validate();
105   d_ffg->setup_connections();
106
107   // Execute scheduler threads
108   start_threads();
109 }
110
111 void
112 gr_top_block_impl::start_threads()
113 {
114   if (GR_TOP_BLOCK_IMPL_DEBUG)
115     std::cout << "start_threads: entered" << std::endl;
116
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;
124     thread->start();
125   }
126
127   d_running = true;
128 }
129
130 /*
131  * N.B. as currently implemented, it is possible that this may be
132  * invoked by the SIGINT handler which is fragile as hell...
133  */
134 void
135 gr_top_block_impl::stop()
136 {
137   if (GR_TOP_BLOCK_IMPL_DEBUG){
138     char *msg = "stop: entered\n";
139     ::write(1, msg, strlen(msg));
140   }
141
142   for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
143     if (*p)
144       (*p)->stop();
145   }
146 }
147
148 void
149 gr_top_block_impl::wait()
150 {
151   if (GR_TOP_BLOCK_IMPL_DEBUG)
152     std::cout << "wait: entered" << std::endl;
153
154   void *dummy_status; // don't ever dereference this
155   gr_local_sighandler sigint(SIGINT, runtime_sigint_handler);
156
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;
164   }
165
166   d_threads.clear();
167   d_running = false;
168 }
169
170 // N.B. lock() and unlock() cannot be called from a flow graph thread or
171 // deadlock will occur when reconfiguration happens
172 void
173 gr_top_block_impl::lock()
174 {
175   omni_mutex_lock lock(d_reconf);
176   d_lock_count++;
177   if (GR_TOP_BLOCK_IMPL_DEBUG)
178     std::cout << "runtime: locked, count = " << d_lock_count <<  std::endl;
179 }
180
181 void
182 gr_top_block_impl::unlock()
183 {
184   omni_mutex_lock lock(d_reconf);
185   if (d_lock_count <= 0)
186     throw std::runtime_error("unpaired unlock() call");
187
188   d_lock_count--;
189   if (GR_TOP_BLOCK_IMPL_DEBUG)
190     std::cout << "unlock: unlocked, count = " << d_lock_count << std::endl;
191
192   if (d_lock_count == 0) {
193     if (GR_TOP_BLOCK_IMPL_DEBUG)
194       std::cout << "unlock: restarting flowgraph" << std::endl;
195     restart();
196   }
197 }
198
199 void
200 gr_top_block_impl::restart()
201 {
202   if (GR_TOP_BLOCK_IMPL_DEBUG)
203     std::cout << "restart: entered" << std::endl;
204
205   if (!d_running)
206     throw std::runtime_error("top block is not running");
207
208   // Stop scheduler threads and wait for completion
209   stop();
210   wait();
211   if (GR_TOP_BLOCK_IMPL_DEBUG)
212     std::cout << "restart: threads stopped" << std::endl;
213
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
218
219   if (GR_TOP_BLOCK_IMPL_DEBUG)
220     std::cout << "restart: replacing old flow graph with new" << std::endl;
221   d_ffg = new_ffg;
222
223   start_threads();
224 }