GNU Radio 3.7.2 C++ API
basic_block.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2008,2009,2011,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 #ifndef INCLUDED_GR_BASIC_BLOCK_H
24 #define INCLUDED_GR_BASIC_BLOCK_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/sptr_magic.h>
28 #include <gnuradio/msg_accepter.h>
29 #include <gnuradio/runtime_types.h>
30 #include <gnuradio/io_signature.h>
31 #include <gnuradio/thread/thread.h>
32 #include <boost/enable_shared_from_this.hpp>
33 #include <boost/function.hpp>
34 #include <boost/foreach.hpp>
35 #include <boost/thread/condition_variable.hpp>
36 #include <iostream>
37 #include <string>
38 #include <deque>
39 #include <map>
40 
41 #ifdef GR_CTRLPORT
43 #endif
44 
45 namespace gr {
46 
47  /*!
48  * \brief The abstract base class for all signal processing blocks.
49  * \ingroup internal
50  *
51  * Basic blocks are the bare abstraction of an entity that has a
52  * name, a set of inputs and outputs, and a message queue. These
53  * are never instantiated directly; rather, this is the abstract
54  * parent class of both gr_hier_block, which is a recursive
55  * container, and block, which implements actual signal
56  * processing functions.
57  */
59  public boost::enable_shared_from_this<basic_block>
60  {
61  typedef boost::function<void(pmt::pmt_t)> msg_handler_t;
62 
63  private:
64  //msg_handler_t d_msg_handler;
65  typedef std::map<pmt::pmt_t , msg_handler_t, pmt::comperator> d_msg_handlers_t;
66  d_msg_handlers_t d_msg_handlers;
67 
68  typedef std::deque<pmt::pmt_t> msg_queue_t;
69  typedef std::map<pmt::pmt_t, msg_queue_t, pmt::comperator> msg_queue_map_t;
70  typedef std::map<pmt::pmt_t, msg_queue_t, pmt::comperator>::iterator msg_queue_map_itr;
71  std::map<pmt::pmt_t, boost::shared_ptr<boost::condition_variable>, pmt::comperator> msg_queue_ready;
72 
73  gr::thread::mutex mutex; //< protects all vars
74 
75  protected:
76  friend class flowgraph;
77  friend class flat_flowgraph; // TODO: will be redundant
78  friend class tpb_thread_body;
79 
80  enum vcolor { WHITE, GREY, BLACK };
81 
82  std::string d_name;
87  std::string d_symbol_name;
88  std::string d_symbol_alias;
90  bool d_rpc_set;
91 
92  msg_queue_map_t msg_queue;
93  std::vector<boost::any> d_rpc_vars; // container for all RPC variables
94 
95  basic_block(void) {} // allows pure virtual interface sub-classes
96 
97  //! Protected constructor prevents instantiation by non-derived classes
98  basic_block(const std::string &name,
99  gr::io_signature::sptr input_signature,
100  gr::io_signature::sptr output_signature);
101 
102  //! may only be called during constructor
104  d_input_signature = iosig;
105  }
106 
107  //! may only be called during constructor
109  d_output_signature = iosig;
110  }
111 
112  /*!
113  * \brief Allow the flowgraph to set for sorting and partitioning
114  */
115  void set_color(vcolor color) { d_color = color; }
116  vcolor color() const { return d_color; }
117 
118  /*!
119  * \brief Tests if there is a handler attached to port \p which_port
120  */
121  virtual bool has_msg_handler(pmt::pmt_t which_port) {
122  return (d_msg_handlers.find(which_port) != d_msg_handlers.end());
123  }
124 
125  /*
126  * This function is called by the runtime system to dispatch messages.
127  *
128  * The thread-safety guarantees mentioned in set_msg_handler are
129  * implemented by the callers of this method.
130  */
131  virtual void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
132  {
133  // AA Update this
134  if(has_msg_handler(which_port)) { // Is there a handler?
135  d_msg_handlers[which_port](msg); // Yes, invoke it.
136  }
137  }
138 
139  // Message passing interface
141 
142  public:
143  pmt::pmt_t message_subscribers(pmt::pmt_t port);
144  virtual ~basic_block();
145  long unique_id() const { return d_unique_id; }
146  long symbolic_id() const { return d_symbolic_id; }
147  std::string name() const { return d_name; }
148  std::string symbol_name() const { return d_symbol_name; }
149  gr::io_signature::sptr input_signature() const { return d_input_signature; }
150  gr::io_signature::sptr output_signature() const { return d_output_signature; }
151  basic_block_sptr to_basic_block(); // Needed for Python type coercion
152  bool alias_set() { return !d_symbol_alias.empty(); }
153  std::string alias(){ return alias_set()?d_symbol_alias:symbol_name(); }
154  pmt::pmt_t alias_pmt(){ return pmt::intern(alias()); }
155  void set_block_alias(std::string name);
156 
157  // ** Message passing interface **
158  void message_port_register_in(pmt::pmt_t port_id);
159  void message_port_register_out(pmt::pmt_t port_id);
160  void message_port_pub(pmt::pmt_t port_id, pmt::pmt_t msg);
161  void message_port_sub(pmt::pmt_t port_id, pmt::pmt_t target);
162  void message_port_unsub(pmt::pmt_t port_id, pmt::pmt_t target);
163 
164  virtual bool message_port_is_hier(pmt::pmt_t port_id) { (void) port_id; std::cout << "is_hier\n"; return false; }
165  virtual bool message_port_is_hier_in(pmt::pmt_t port_id) { (void) port_id; std::cout << "is_hier_in\n"; return false; }
166  virtual bool message_port_is_hier_out(pmt::pmt_t port_id) { (void) port_id; std::cout << "is_hier_out\n"; return false; }
167 
168  /*!
169  * \brief Get input message port names.
170  *
171  * Returns the available input message ports for a block. The
172  * return object is a PMT vector that is filled with PMT symbols.
173  */
174  pmt::pmt_t message_ports_in();
175 
176  /*!
177  * \brief Get output message port names.
178  *
179  * Returns the available output message ports for a block. The
180  * return object is a PMT vector that is filled with PMT symbols.
181  */
182  pmt::pmt_t message_ports_out();
183 
184  /*!
185  * Accept msg, place in queue, arrange for thread to be awakened if it's not already.
186  */
187  void _post(pmt::pmt_t which_port, pmt::pmt_t msg);
188 
189  //! is the queue empty?
190  bool empty_p(pmt::pmt_t which_port) {
191  if(msg_queue.find(which_port) == msg_queue.end())
192  throw std::runtime_error("port does not exist!");
193  return msg_queue[which_port].empty();
194  }
195  bool empty_p() {
196  bool rv = true;
197  BOOST_FOREACH(msg_queue_map_t::value_type &i, msg_queue) {
198  rv &= msg_queue[i.first].empty();
199  }
200  return rv;
201  }
202 
203  //! are all msg ports with handlers empty?
204  bool empty_handled_p(pmt::pmt_t which_port){
205  return (empty_p(which_port) || !has_msg_handler(which_port));
206  }
207  bool empty_handled_p() {
208  bool rv = true;
209  BOOST_FOREACH(msg_queue_map_t::value_type &i, msg_queue) {
210  rv &= empty_handled_p(i.first);
211  }
212  return rv;
213  }
214 
215  //! How many messages in the queue?
216  size_t nmsgs(pmt::pmt_t which_port) {
217  if(msg_queue.find(which_port) == msg_queue.end())
218  throw std::runtime_error("port does not exist!");
219  return msg_queue[which_port].size();
220  }
221 
222  //| Acquires and release the mutex
223  void insert_tail( pmt::pmt_t which_port, pmt::pmt_t msg);
224  /*!
225  * \returns returns pmt at head of queue or pmt::pmt_t() if empty.
226  */
227  pmt::pmt_t delete_head_nowait( pmt::pmt_t which_port);
228 
229  /*!
230  * \returns returns pmt at head of queue or pmt::pmt_t() if empty.
231  */
232  pmt::pmt_t delete_head_blocking( pmt::pmt_t which_port);
233 
234  msg_queue_t::iterator get_iterator(pmt::pmt_t which_port) {
235  return msg_queue[which_port].begin();
236  }
237 
238  void erase_msg(pmt::pmt_t which_port, msg_queue_t::iterator it) {
239  msg_queue[which_port].erase(it);
240  }
241 
242  virtual bool has_msg_port(pmt::pmt_t which_port) {
243  if(msg_queue.find(which_port) != msg_queue.end()) {
244  return true;
245  }
246  if(pmt::dict_has_key(d_message_subscribers, which_port)) {
247  return true;
248  }
249  return false;
250  }
251 
252 #ifdef GR_CTRLPORT
253  /*!
254  * \brief Add an RPC variable (get or set).
255  *
256  * Using controlport, we create new getters/setters and need to
257  * store them. Each block has a vector to do this, and these never
258  * need to be accessed again once they are registered with the RPC
259  * backend. This function takes a
260  * boost::shared_sptr<rpcbasic_base> so that when the block is
261  * deleted, all RPC registered variables are cleaned up.
262  *
263  * \param s an rpcbasic_sptr of the new RPC variable register to store.
264  */
265  void add_rpc_variable(rpcbasic_sptr s)
266  {
267  d_rpc_vars.push_back(s);
268  }
269 #endif /* GR_CTRLPORT */
270 
271  /*!
272  * \brief Set up the RPC registered variables.
273  *
274  * This must be overloaded by a block that wants to use
275  * controlport. This is where rpcbasic_register_{get,set} pointers
276  * are created, which then get wrapped as shared pointers
277  * (rpcbasic_sptr(...)) and stored using add_rpc_variable.
278  */
279  virtual void setup_rpc() {};
280 
281  /*!
282  * \brief Ask if this block has been registered to the RPC.
283  *
284  * We can only register a block once, so we use this to protect us
285  * from calling it multiple times.
286  */
287  bool is_rpc_set() { return d_rpc_set; }
288 
289  /*!
290  * \brief When the block is registered with the RPC, set this.
291  */
292  void rpc_set() { d_rpc_set = true; }
293 
294  /*!
295  * \brief Confirm that ninputs and noutputs is an acceptable combination.
296  *
297  * \param ninputs number of input streams connected
298  * \param noutputs number of output streams connected
299  *
300  * \returns true if this is a valid configuration for this block.
301  *
302  * This function is called by the runtime system whenever the
303  * topology changes. Most classes do not need to override this.
304  * This check is in addition to the constraints specified by the
305  * input and output gr::io_signatures.
306  */
307  virtual bool check_topology(int ninputs, int noutputs) {
308  (void)ninputs;
309  (void)noutputs;
310  return true;
311  }
312 
313  /*!
314  * \brief Set the callback that is fired when messages are available.
315  *
316  * \p msg_handler can be any kind of function pointer or function object
317  * that has the signature:
318  * <pre>
319  * void msg_handler(pmt::pmt msg);
320  * </pre>
321  *
322  * (You may want to use boost::bind to massage your callable into
323  * the correct form. See gr::blocks::nop for an example that sets
324  * up a class method as the callback.)
325  *
326  * Blocks that desire to handle messages must call this method in
327  * their constructors to register the handler that will be invoked
328  * when messages are available.
329  *
330  * If the block inherits from block, the runtime system will
331  * ensure that msg_handler is called in a thread-safe manner, such
332  * that work and msg_handler will never be called concurrently.
333  * This allows msg_handler to update state variables without
334  * having to worry about thread-safety issues with work,
335  * general_work or another invocation of msg_handler.
336  *
337  * If the block inherits from hier_block2, the runtime system
338  * will ensure that no reentrant calls are made to msg_handler.
339  */
340  template <typename T> void set_msg_handler(pmt::pmt_t which_port, T msg_handler) {
341  if(msg_queue.find(which_port) == msg_queue.end()) {
342  throw std::runtime_error("attempt to set_msg_handler() on bad input message port!");
343  }
344  d_msg_handlers[which_port] = msg_handler_t(msg_handler);
345  }
346 
347  virtual void set_processor_affinity(const std::vector<int> &mask)
348  { throw std::runtime_error("set_processor_affinity not overloaded in child class."); }
349 
351  { throw std::runtime_error("unset_processor_affinity not overloaded in child class."); }
352 
353  virtual std::vector<int> processor_affinity()
354  { throw std::runtime_error("processor_affinity not overloaded in child class."); }
355  };
356 
358  {
359  return lhs->unique_id() < rhs->unique_id();
360  }
361 
362  typedef std::vector<basic_block_sptr> basic_block_vector_t;
363  typedef std::vector<basic_block_sptr>::iterator basic_block_viter_t;
364 
366 
367  inline std::ostream &operator << (std::ostream &os, basic_block_sptr basic_block)
368  {
369  os << basic_block->name() << "(" << basic_block->unique_id() << ")";
370  return os;
371  }
372 
373 } /* namespace gr */
374 
375 #endif /* INCLUDED_GR_BASIC_BLOCK_H */
std::string d_symbol_alias
Definition: basic_block.h:88
void set_input_signature(gr::io_signature::sptr iosig)
may only be called during constructor
Definition: basic_block.h:103
size_t nmsgs(pmt::pmt_t which_port)
How many messages in the queue?
Definition: basic_block.h:216
virtual void setup_rpc()
Set up the RPC registered variables.
Definition: basic_block.h:279
std::string alias()
Definition: basic_block.h:153
virtual void set_processor_affinity(const std::vector< int > &mask)
Definition: basic_block.h:347
bool empty_p()
Definition: basic_block.h:195
std::string d_symbol_name
Definition: basic_block.h:87
bool is_rpc_set()
Ask if this block has been registered to the RPC.
Definition: basic_block.h:287
void set_color(vcolor color)
Allow the flowgraph to set for sorting and partitioning.
Definition: basic_block.h:115
virtual bool message_port_is_hier(pmt::pmt_t port_id)
Definition: basic_block.h:164
msg_queue_map_t msg_queue
Definition: basic_block.h:92
void erase_msg(pmt::pmt_t which_port, msg_queue_t::iterator it)
Definition: basic_block.h:238
virtual bool has_msg_handler(pmt::pmt_t which_port)
Tests if there is a handler attached to port which_port.
Definition: basic_block.h:121
bool empty_handled_p(pmt::pmt_t which_port)
are all msg ports with handlers empty?
Definition: basic_block.h:204
virtual std::vector< int > processor_affinity()
Definition: basic_block.h:353
Class representing a directed, acyclic graph of basic blocks.
Definition: flowgraph.h:144
PMT_API pmt_t intern(const std::string &s)
Alias for pmt_string_to_symbol.
void rpc_set()
When the block is registered with the RPC, set this.
Definition: basic_block.h:292
std::string name() const
Definition: basic_block.h:147
virtual bool has_msg_port(pmt::pmt_t which_port)
Definition: basic_block.h:242
Accepts messages and inserts them into a message queue, then notifies subclass gr::basic_block there ...
Definition: msg_accepter.h:35
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
long unique_id() const
Definition: basic_block.h:145
thread-safe message queue
Definition: msg_queue.h:36
gr::io_signature::sptr d_input_signature
Definition: basic_block.h:83
gr::io_signature::sptr output_signature() const
Definition: basic_block.h:150
vcolor
Definition: basic_block.h:80
The abstract base class for all signal processing blocks.Basic blocks are the bare abstraction of an ...
Definition: basic_block.h:58
gr::io_signature::sptr input_signature() const
Definition: basic_block.h:149
std::string d_name
Definition: basic_block.h:82
vcolor color() const
Definition: basic_block.h:116
basic_block(void)
Definition: basic_block.h:95
virtual bool message_port_is_hier_in(pmt::pmt_t port_id)
Definition: basic_block.h:165
virtual bool message_port_is_hier_out(pmt::pmt_t port_id)
Definition: basic_block.h:166
bool empty_p(pmt::pmt_t which_port)
is the queue empty?
Definition: basic_block.h:190
bool empty_handled_p()
Definition: basic_block.h:207
virtual bool check_topology(int ninputs, int noutputs)
Confirm that ninputs and noutputs is an acceptable combination.
Definition: basic_block.h:307
VOLK_API void(kern.name) _manual($kern.arglist_full
Call into a specific implementation given by name.
virtual void unset_processor_affinity()
Definition: basic_block.h:350
std::vector< boost::any > d_rpc_vars
Definition: basic_block.h:93
std::vector< basic_block_sptr > basic_block_vector_t
Definition: basic_block.h:362
msg_queue_t::iterator get_iterator(pmt::pmt_t which_port)
Definition: basic_block.h:234
long d_symbolic_id
Definition: basic_block.h:86
pmt::pmt_t alias_pmt()
Definition: basic_block.h:154
std::vector< basic_block_sptr >::iterator basic_block_viter_t
Definition: basic_block.h:363
gr::io_signature::sptr d_output_signature
Definition: basic_block.h:84
std::ostream & operator<<(std::ostream &os, basic_block_sptr basic_block)
Definition: basic_block.h:367
VOLK_API $kern pname $kern name
A function pointer to the dispatcher implementation.
GR_RUNTIME_API long basic_block_ncurrently_allocated()
boost::mutex mutex
Definition: thread.h:46
PMT_API bool dict_has_key(const pmt_t &dict, const pmt_t &key)
Return true if key exists in dict.
Provide a comparator function object to allow pmt use in stl types.
Definition: pmt.h:876
long symbolic_id() const
Definition: basic_block.h:146
bool d_rpc_set
Definition: basic_block.h:90
boost::intrusive_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting). See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
Definition: pmt.h:56
virtual void dispatch_msg(pmt::pmt_t which_port, pmt::pmt_t msg)
Definition: basic_block.h:131
vcolor d_color
Definition: basic_block.h:89
Definition: basic_block.h:80
pmt::pmt_t d_message_subscribers
Definition: basic_block.h:140
void set_msg_handler(pmt::pmt_t which_port, T msg_handler)
Set the callback that is fired when messages are available.
Definition: basic_block.h:340
bool alias_set()
Definition: basic_block.h:152
long d_unique_id
Definition: basic_block.h:85
bool operator<(basic_block_sptr lhs, basic_block_sptr rhs)
Definition: basic_block.h:357
void set_output_signature(gr::io_signature::sptr iosig)
may only be called during constructor
Definition: basic_block.h:108
std::string symbol_name() const
Definition: basic_block.h:148
abstract class of message handlers
Definition: msg_handler.h:38