Merged r6171:6186 from jcorgan/fg into trunk.
[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
36 #define GR_TOP_BLOCK_IMPL_DEBUG 0
37
38 static gr_top_block_impl *s_impl = 0;
39
40 // Make a vector of gr_block from a vector of gr_basic_block
41 static
42 gr_block_vector_t
43 make_gr_block_vector(gr_basic_block_vector_t &blocks)
44 {
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));
48   }
49
50   return result;
51 }
52
53 // FIXME: This prevents using more than one gr_top_block instance
54 static void 
55 runtime_sigint_handler(int signum)
56 {
57   if (GR_TOP_BLOCK_IMPL_DEBUG)
58     std::cout << "SIGINT received, calling stop()" << std::endl;
59
60   if (s_impl)
61     s_impl->stop();
62 }
63
64 gr_top_block_impl::gr_top_block_impl(gr_top_block *owner) 
65   : d_running(false),
66     d_ffg(gr_make_flat_flowgraph()),
67     d_owner(owner),
68     d_lock_count(0)
69 {
70   s_impl = this;
71 }
72
73 gr_top_block_impl::~gr_top_block_impl()
74 {
75   s_impl = 0; // don't call delete we don't own these
76   d_owner = 0;
77 }
78
79 void
80 gr_top_block_impl::start()
81 {
82   if (GR_TOP_BLOCK_IMPL_DEBUG)
83     std::cout << "start: entered" << std::endl;
84
85   if (d_running)
86     throw std::runtime_error("already running");
87
88   // Create new flat flow graph by flattening hierarchy
89   d_ffg->clear();
90   d_owner->flatten(d_ffg);
91
92   // Validate new simple flow graph and wire it up
93   d_ffg->validate();
94   d_ffg->setup_connections();
95
96   // Execute scheduler threads
97   start_threads();
98 }
99
100 void
101 gr_top_block_impl::start_threads()
102 {
103   if (GR_TOP_BLOCK_IMPL_DEBUG)
104     std::cout << "start_threads: entered" << std::endl;
105
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;
113     thread->start();
114   }
115
116   d_running = true;
117 }
118
119 void
120 gr_top_block_impl::stop()
121 {
122   if (GR_TOP_BLOCK_IMPL_DEBUG)
123     std::cout << "stop: entered" << std::endl;
124
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;
128     (*p)->stop();
129   }
130
131   d_running = false;
132 }
133
134 void
135 gr_top_block_impl::wait()
136 {
137   if (GR_TOP_BLOCK_IMPL_DEBUG)
138     std::cout << "wait: entered" << std::endl;
139
140   void *dummy_status; // don't ever dereference this
141   gr_local_sighandler sigint(SIGINT, runtime_sigint_handler);
142
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;
150   }
151
152   d_threads.clear();
153 }
154
155 // N.B. lock() and unlock() cannot be called from a flow graph thread or
156 // deadlock will occur when reconfiguration happens
157 void
158 gr_top_block_impl::lock()
159 {
160   omni_mutex_lock lock(d_reconf);
161   d_lock_count++;
162   if (GR_TOP_BLOCK_IMPL_DEBUG)
163     std::cout << "runtime: locked, count = " << d_lock_count <<  std::endl;
164 }
165
166 void
167 gr_top_block_impl::unlock()
168 {
169   omni_mutex_lock lock(d_reconf);
170   if (d_lock_count == 0)
171     throw std::runtime_error("unpaired unlock() call");
172
173   d_lock_count--;
174   if (GR_TOP_BLOCK_IMPL_DEBUG)
175     std::cout << "unlock: unlocked, count = " << d_lock_count << std::endl;
176
177   if (d_lock_count == 0) {
178     if (GR_TOP_BLOCK_IMPL_DEBUG)
179       std::cout << "unlock: restarting flowgraph" << std::endl;
180     restart();
181   }
182 }
183
184 void
185 gr_top_block_impl::restart()
186 {
187   if (GR_TOP_BLOCK_IMPL_DEBUG)
188     std::cout << "restart: entered" << std::endl;
189
190   if (!d_running)
191     throw std::runtime_error("not running");
192
193   // Stop scheduler threads and wait for completion
194   stop();
195   wait();
196   if (GR_TOP_BLOCK_IMPL_DEBUG)
197     std::cout << "restart: threads stopped" << std::endl;
198
199   // Create new simple flow graph 
200   gr_flat_flowgraph_sptr new_ffg = gr_make_flat_flowgraph();
201   d_owner->flatten(new_ffg);
202   new_ffg->validate();
203   new_ffg->merge_connections(d_ffg);
204
205   if (GR_TOP_BLOCK_IMPL_DEBUG)
206     std::cout << "restart: replacing old flow graph with new" << std::endl;
207   d_ffg = new_ffg;
208
209   start_threads();
210 }