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