1 /* -*- c++ -*- */
2 /*
3 * Copyright 2007,2008,2013 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 "flat_flowgraph.h"
28 #include "scheduler_tpb.h"
29 #include "top_block_impl.h"
30 #include <gnuradio/prefs.h>
31 #include <gnuradio/top_block.h>
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <iostream>
37 #include <stdexcept>
38
39 namespace gr {
40
41 #define GR_TOP_BLOCK_IMPL_DEBUG 0
42
43 typedef scheduler_sptr (*scheduler_maker)(flat_flowgraph_sptr ffg, int max_noutput_items);
44
45 static struct scheduler_table {
46 const char* name;
47 scheduler_maker f;
48 } scheduler_table[] = {
49 { "TPB", scheduler_tpb::make } // first entry is default
50 };
51
make_scheduler(flat_flowgraph_sptr ffg,int max_noutput_items)52 static scheduler_sptr make_scheduler(flat_flowgraph_sptr ffg, int max_noutput_items)
53 {
54 static scheduler_maker factory = 0;
55
56 if (factory == 0) {
57 char* v = getenv("GR_SCHEDULER");
58 if (!v)
59 factory = scheduler_table[0].f; // use default
60 else {
61 for (size_t i = 0; i < sizeof(scheduler_table) / sizeof(scheduler_table[0]);
62 i++) {
63 if (strcmp(v, scheduler_table[i].name) == 0) {
64 factory = scheduler_table[i].f;
65 break;
66 }
67 }
68 if (factory == 0) {
69 std::cerr << "warning: Invalid GR_SCHEDULER environment variable value \""
70 << v << "\". Using \"" << scheduler_table[0].name << "\"\n";
71 factory = scheduler_table[0].f;
72 }
73 }
74 }
75 return factory(ffg, max_noutput_items);
76 }
77
top_block_impl(top_block * owner)78 top_block_impl::top_block_impl(top_block* owner)
79 : d_owner(owner), d_ffg(), d_state(IDLE), d_lock_count(0), d_retry_wait(false)
80 {
81 }
82
~top_block_impl()83 top_block_impl::~top_block_impl()
84 {
85 if (d_lock_count) {
86 std::cerr << "error: destroying locked block." << std::endl;
87 }
88 d_owner = 0;
89 }
90
start(int max_noutput_items)91 void top_block_impl::start(int max_noutput_items)
92 {
93 gr::thread::scoped_lock l(d_mutex);
94
95 d_max_noutput_items = max_noutput_items;
96
97 if (d_state != IDLE)
98 throw std::runtime_error("top_block::start: top block already running or wait() "
99 "not called after previous stop()");
100
101 if (d_lock_count > 0)
102 throw std::runtime_error("top_block::start: can't start with flow graph locked");
103
104 // Create new flat flow graph by flattening hierarchy
105 d_ffg = d_owner->flatten();
106
107 // Validate new simple flow graph and wire it up
108 d_ffg->validate();
109 d_ffg->setup_connections();
110
111 // Only export perf. counters if ControlPort config param is
112 // enabled and if the PerfCounter option 'export' is turned on.
113 prefs* p = prefs::singleton();
114 if (p->get_bool("ControlPort", "on", false) &&
115 p->get_bool("PerfCounters", "export", false))
116 d_ffg->enable_pc_rpc();
117
118 d_scheduler = make_scheduler(d_ffg, d_max_noutput_items);
119 d_state = RUNNING;
120 }
121
stop()122 void top_block_impl::stop()
123 {
124 gr::thread::scoped_lock lock(d_mutex);
125
126 if (d_scheduler)
127 d_scheduler->stop();
128
129 d_ffg.reset();
130
131 d_state = IDLE;
132 }
133
wait()134 void top_block_impl::wait()
135 {
136 do {
137 wait_for_jobs();
138 {
139 gr::thread::scoped_lock lock(d_mutex);
140 if (!d_lock_count) {
141 if (d_retry_wait) {
142 d_retry_wait = false;
143 continue;
144 }
145 d_state = IDLE;
146 break;
147 }
148 d_lock_cond.wait(lock);
149 }
150 } while (true);
151 }
152
wait_for_jobs()153 void top_block_impl::wait_for_jobs()
154 {
155 if (d_scheduler)
156 d_scheduler->wait();
157 }
158
159 // N.B. lock() and unlock() cannot be called from a flow graph
160 // thread or deadlock will occur when reconfiguration happens
lock()161 void top_block_impl::lock()
162 {
163 gr::thread::scoped_lock lock(d_mutex);
164 if (d_scheduler)
165 d_scheduler->stop();
166 d_lock_count++;
167 }
168
unlock()169 void top_block_impl::unlock()
170 {
171 gr::thread::scoped_lock lock(d_mutex);
172
173 if (d_lock_count <= 0) {
174 d_lock_count = 0; // fix it, then complain
175 throw std::runtime_error("unpaired unlock() call");
176 }
177
178 d_lock_count--;
179 if (d_lock_count > 0 || d_state == IDLE) // nothing to do
180 return;
181
182 restart();
183 d_lock_cond.notify_all();
184 }
185
186 /*
187 * restart is called with d_mutex held
188 */
restart()189 void top_block_impl::restart()
190 {
191 wait_for_jobs();
192
193 // Create new simple flow graph
194 flat_flowgraph_sptr new_ffg = d_owner->flatten();
195 new_ffg->validate(); // check consistency, sanity, etc
196 new_ffg->merge_connections(d_ffg); // reuse buffers, etc
197 d_ffg = new_ffg;
198
199 // Create a new scheduler to execute it
200 d_scheduler = make_scheduler(d_ffg, d_max_noutput_items);
201 d_retry_wait = true;
202 }
203
edge_list()204 std::string top_block_impl::edge_list()
205 {
206 if (d_ffg)
207 return d_ffg->edge_list();
208 else
209 return "";
210 }
211
msg_edge_list()212 std::string top_block_impl::msg_edge_list()
213 {
214 if (d_ffg)
215 return d_ffg->msg_edge_list();
216 else
217 return "";
218 }
219
dump()220 void top_block_impl::dump()
221 {
222 if (d_ffg)
223 d_ffg->dump();
224 }
225
max_noutput_items()226 int top_block_impl::max_noutput_items() { return d_max_noutput_items; }
227
set_max_noutput_items(int nmax)228 void top_block_impl::set_max_noutput_items(int nmax) { d_max_noutput_items = nmax; }
229
230 } /* namespace gr */
231