GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
buffer_reader.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,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_RUNTIME_BUFFER_READER_H
12 #define INCLUDED_GR_RUNTIME_BUFFER_READER_H
13 
14 #include <gnuradio/api.h>
15 #include <gnuradio/buffer.h>
16 #include <gnuradio/logger.h>
17 #include <gnuradio/runtime_types.h>
18 #include <gnuradio/tags.h>
19 #include <gnuradio/thread/thread.h>
20 #include <memory>
21 
22 namespace gr {
23 
24 class buffer_reader_sm;
25 
26 /*!
27  * \brief Create a new gr::buffer_reader and attach it to buffer \p buf
28  * \param buf is the buffer the \p gr::buffer_reader reads from.
29  * \param nzero_preload -- number of zero items to "preload" into buffer.
30  * \param link is the block that reads from the buffer using this gr::buffer_reader.
31  * \param delay Optional setting to declare the buffer's sample delay.
32  */
33 GR_RUNTIME_API buffer_reader_sptr buffer_add_reader(buffer_sptr buf,
34  int nzero_preload,
35  block_sptr link = block_sptr(),
36  int delay = 0);
37 
38 //! returns # of buffers currently allocated
40 
41 
42 // ---------------------------------------------------------------------------
43 
44 /*!
45  * \brief How we keep track of the readers of a gr::buffer.
46  * \ingroup internal
47  */
49 {
50 public:
51 #ifdef BUFFER_DEBUG
52  gr::logger_ptr d_logger;
53  gr::logger_ptr d_debug_logger;
54 #endif
55 
56  virtual ~buffer_reader();
57 
58  /*!
59  * Declares the sample delay for this reader.
60  *
61  * See gr::block::declare_sample_delay for details.
62  *
63  * \param delay The new sample delay
64  */
65  void declare_sample_delay(unsigned delay);
66 
67  /*!
68  * Gets the sample delay for this reader.
69  *
70  * See gr::block::sample_delay for details.
71  */
72  unsigned sample_delay() const;
73 
74  /*!
75  * \brief Return number of items available for reading.
76  */
77  virtual int items_available() const;
78 
79  /*!
80  * \brief Return buffer this reader reads from.
81  */
82  buffer_sptr buffer() const { return d_buffer; }
83 
84  /*!
85  * \brief Return maximum number of items that could ever be available for reading.
86  * This is used as a sanity check in the scheduler to avoid looping forever.
87  */
88  int max_possible_items_available() const { return d_buffer->bufsize() - 1; }
89 
90  /*!
91  * \brief return pointer to read buffer.
92  *
93  * The return value points to items_available() number of items
94  */
95  const void* read_pointer();
96 
97  /*
98  * \brief tell buffer we read \p items from it
99  */
100  void update_read_pointer(int nitems);
101 
102  void set_done(bool done) { d_buffer->set_done(done); }
103  bool done() const { return d_buffer->done(); }
104 
105  gr::thread::mutex* mutex() { return d_buffer->mutex(); }
106 
107  uint64_t nitems_read() const { return d_abs_read_offset; }
108 
110  {
111  d_read_index = 0;
112  d_abs_read_offset = 0;
113  }
114 
115  size_t get_sizeof_item() { return d_buffer->get_sizeof_item(); }
116 
117  /*!
118  * \brief Return the block that reads via this reader.
119  *
120  */
121  block_sptr link() const { return block_sptr(d_link); }
122 
123  /*!
124  * \brief Given a [start,end), returns a vector all tags in the range.
125  *
126  * Get a vector of tags in given range. Range of counts is from start to end-1.
127  *
128  * Tags are tuples of:
129  * (item count, source id, key, value)
130  *
131  * \param v a vector reference to return tags into
132  * \param abs_start a uint64 count of the start of the range of interest
133  * \param abs_end a uint64 count of the end of the range of interest
134  * \param id the unique ID of the block to make sure already deleted tags
135  * are not returned
136  */
137  void get_tags_in_range(std::vector<tag_t>& v,
138  uint64_t abs_start,
139  uint64_t abs_end,
140  long id);
141 
142  /*!
143  * \brief Returns true when the current thread is ready to call the callback,
144  * false otherwise. Delegate calls to buffer class's input_blkd_cb_ready().
145  * Note if input_blocked_callback is overridden then this function should
146  * also be overridden.
147  */
148  virtual bool input_blkd_cb_ready(int items_required) const { return false; }
149 
150  /*!
151  * \brief Callback function that the scheduler will call when it determines
152  * that the input is blocked. Delegate calls to buffer class's
153  * input_blocked_callback(). Override this function if needed.
154  */
155  virtual bool input_blocked_callback(int items_required, int items_avail)
156  {
157  return false;
158  }
159 
160  // -------------------------------------------------------------------------
161  unsigned int get_read_index() const { return d_read_index; }
162  uint64_t get_abs_read_offset() const { return d_abs_read_offset; }
163 
164 protected:
165  friend class buffer;
166  friend class buffer_double_mapped;
167  friend class buffer_single_mapped;
168  friend class buffer_reader_sm;
169 
170  friend GR_RUNTIME_API buffer_reader_sptr buffer_add_reader(buffer_sptr buf,
171  int nzero_preload,
172  block_sptr link,
173  int delay);
174 
175  buffer_sptr d_buffer;
176  unsigned int d_read_index; // in items [0,d->buffer.d_bufsize) ** see NB
177  uint64_t d_abs_read_offset; // num items seen since the start ** see NB
178  std::weak_ptr<block> d_link; // block that reads via this buffer reader
179  unsigned d_attr_delay; // sample delay attribute for tag propagation
180  // ** NB: buffer::d_mutex protects d_read_index and d_abs_read_offset
181 
182  //! constructor is private. Use gr::buffer::add_reader to create instances
183  buffer_reader(buffer_sptr buffer, unsigned int read_index, block_sptr link);
184 };
185 
186 //! returns # of buffer_readers currently allocated
188 
189 } /* namespace gr */
190 
191 #endif /* INCLUDED_GR_RUNTIME_BUFFER_READER_H */
Single writer, multiple reader fifo.
Definition: buffer_double_mapped.h:29
Definition: buffer_reader_sm.h:22
How we keep track of the readers of a gr::buffer.
Definition: buffer_reader.h:49
uint64_t nitems_read() const
Definition: buffer_reader.h:107
virtual ~buffer_reader()
const void * read_pointer()
return pointer to read buffer.
unsigned sample_delay() const
unsigned int get_read_index() const
Definition: buffer_reader.h:161
uint64_t d_abs_read_offset
Definition: buffer_reader.h:177
std::weak_ptr< block > d_link
Definition: buffer_reader.h:178
block_sptr link() const
Return the block that reads via this reader.
Definition: buffer_reader.h:121
buffer_sptr d_buffer
Definition: buffer_reader.h:175
size_t get_sizeof_item()
Definition: buffer_reader.h:115
void set_done(bool done)
Definition: buffer_reader.h:102
virtual int items_available() const
Return number of items available for reading.
void declare_sample_delay(unsigned delay)
bool done() const
Definition: buffer_reader.h:103
buffer_sptr buffer() const
Return buffer this reader reads from.
Definition: buffer_reader.h:82
uint64_t get_abs_read_offset() const
Definition: buffer_reader.h:162
void get_tags_in_range(std::vector< tag_t > &v, uint64_t abs_start, uint64_t abs_end, long id)
Given a [start,end), returns a vector all tags in the range.
unsigned d_attr_delay
Definition: buffer_reader.h:179
unsigned int d_read_index
Definition: buffer_reader.h:176
void update_read_pointer(int nitems)
gr::thread::mutex * mutex()
Definition: buffer_reader.h:105
buffer_reader(buffer_sptr buffer, unsigned int read_index, block_sptr link)
constructor is private. Use gr::buffer::add_reader to create instances
friend GR_RUNTIME_API buffer_reader_sptr buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link, int delay)
Create a new gr::buffer_reader and attach it to buffer buf.
void reset_nitem_counter()
Definition: buffer_reader.h:109
virtual bool input_blkd_cb_ready(int items_required) const
Returns true when the current thread is ready to call the callback, false otherwise....
Definition: buffer_reader.h:148
int max_possible_items_available() const
Return maximum number of items that could ever be available for reading. This is used as a sanity che...
Definition: buffer_reader.h:88
virtual bool input_blocked_callback(int items_required, int items_avail)
Callback function that the scheduler will call when it determines that the input is blocked....
Definition: buffer_reader.h:155
A single mapped buffer where wrapping conditions are handled explicitly via input/output_blocked_call...
Definition: buffer_single_mapped.h:30
Single writer, multiple reader fifo.
Definition: buffer.h:67
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
boost::mutex mutex
Definition: thread.h:37
GNU Radio logging wrapper.
Definition: basic_block.h:29
std::shared_ptr< logger > logger_ptr
Definition: logger.h:250
GR_RUNTIME_API long buffer_reader_ncurrently_allocated()
returns # of buffer_readers currently allocated
GR_RUNTIME_API buffer_reader_sptr buffer_add_reader(buffer_sptr buf, int nzero_preload, block_sptr link=block_sptr(), int delay=0)
Create a new gr::buffer_reader and attach it to buffer buf.
GR_RUNTIME_API long buffer_ncurrently_allocated()
returns # of buffers currently allocated
Definition: cc_common.h:35