summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/lib/gr_top_block_impl.cc
blob: b6d427ce270bab4268be556fe5d7a11fa0bf3b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* -*- c++ -*- */
/*
 * Copyright 2007,2008 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gr_top_block.h>
#include <gr_top_block_impl.h>
#include <gr_flat_flowgraph.h>
#include <gr_scheduler_sts.h>
#include <gr_scheduler_tpb.h>
#include <gr_prefs.h>

#include <stdexcept>
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define GR_TOP_BLOCK_IMPL_DEBUG 0


typedef gr_scheduler_sptr (*scheduler_maker)(gr_flat_flowgraph_sptr ffg,
					     int max_noutput_items);

static struct scheduler_table {
  const char 	       *name;
  scheduler_maker	f;
} scheduler_table[] = {
  { "TPB",	gr_scheduler_tpb::make },	// first entry is default
  { "STS",	gr_scheduler_sts::make }
};

static gr_scheduler_sptr
make_scheduler(gr_flat_flowgraph_sptr ffg, int max_noutput_items)
{
  static scheduler_maker  factory = 0;

  if (factory == 0){
    char *v = getenv("GR_SCHEDULER");
    if (!v)
      factory = scheduler_table[0].f;	// use default
    else {
      for (size_t i = 0; i < sizeof(scheduler_table)/sizeof(scheduler_table[0]); i++){
	if (strcmp(v, scheduler_table[i].name) == 0){
	  factory = scheduler_table[i].f;
	  break;
	}
      }
      if (factory == 0){
	std::cerr << "warning: Invalid GR_SCHEDULER environment variable value \""
		  << v << "\".  Using \"" << scheduler_table[0].name << "\"\n";
	factory = scheduler_table[0].f;
      }
    }
  }
  return factory(ffg, max_noutput_items);
}


gr_top_block_impl::gr_top_block_impl(gr_top_block *owner)
  : d_owner(owner), d_ffg(),
    d_state(IDLE), d_lock_count(0)
{
}

gr_top_block_impl::~gr_top_block_impl()
{
  d_owner = 0;
}

void
gr_top_block_impl::start(int max_noutput_items)
{
  gruel::scoped_lock	l(d_mutex);

  d_max_noutput_items = max_noutput_items;

  if (d_state != IDLE)
    throw std::runtime_error("top_block::start: top block already running or wait() not called after previous stop()");

  if (d_lock_count > 0)
    throw std::runtime_error("top_block::start: can't start with flow graph locked");

  // Create new flat flow graph by flattening hierarchy
  d_ffg = d_owner->flatten();

  // Validate new simple flow graph and wire it up
  d_ffg->validate();
  d_ffg->setup_connections();

  // Only export perf. counters if ControlPort config param is enabled
  // and if the PerfCounter option 'export' is turned on.
  gr_prefs *p = gr_prefs::singleton();
  if(p->get_bool("ControlPort", "on", false) && p->get_bool("PerfCounters", "export", false))
    d_ffg->enable_pc_rpc();

  d_scheduler = make_scheduler(d_ffg, d_max_noutput_items);
  d_state = RUNNING;
}

void
gr_top_block_impl::stop()
{
  if (d_scheduler)
    d_scheduler->stop();
}


void
gr_top_block_impl::wait()
{
  if (d_scheduler)
    d_scheduler->wait();

  d_state = IDLE;
}

// N.B. lock() and unlock() cannot be called from a flow graph thread or
// deadlock will occur when reconfiguration happens
void
gr_top_block_impl::lock()
{
  gruel::scoped_lock lock(d_mutex);
  d_lock_count++;
}

void
gr_top_block_impl::unlock()
{
  gruel::scoped_lock lock(d_mutex);

  if (d_lock_count <= 0){
    d_lock_count = 0;		// fix it, then complain
    throw std::runtime_error("unpaired unlock() call");
  }

  d_lock_count--;
  if (d_lock_count > 0 || d_state == IDLE) // nothing to do
    return;

  restart();
}

/*
 * restart is called with d_mutex held
 */
void
gr_top_block_impl::restart()
{
  stop();		     // Stop scheduler and wait for completion
  wait();

  // Create new simple flow graph
  gr_flat_flowgraph_sptr new_ffg = d_owner->flatten();
  new_ffg->validate();		       // check consistency, sanity, etc
  new_ffg->merge_connections(d_ffg);   // reuse buffers, etc
  d_ffg = new_ffg;

  // Create a new scheduler to execute it
  d_scheduler = make_scheduler(d_ffg, d_max_noutput_items);
  d_state = RUNNING;
}

std::string
gr_top_block_impl::edge_list()
{
  if(d_ffg)
    return d_ffg->edge_list();
  else
    return "";
}

void
gr_top_block_impl::dump()
{
  if (d_ffg)
    d_ffg->dump();
}

int
gr_top_block_impl::max_noutput_items()
{
  return d_max_noutput_items;
}

void
gr_top_block_impl::set_max_noutput_items(int nmax)
{
  d_max_noutput_items = nmax;
}