~ubuntu-branches/ubuntu/trusty/gnuradio/trusty

« back to all changes in this revision

Viewing changes to gnuradio-core/src/lib/runtime/gr_buffer.h

  • Committer: Bazaar Package Importer
  • Author(s): Kamal Mostafa
  • Date: 2010-03-13 07:46:01 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100313074601-zjsa893a87bozyh7
Tags: 3.2.2.dfsg-1ubuntu1
* Fix build for Ubuntu lucid (LP: #260406)
  - add binary package dep for libusrp0, libusrp2-0: adduser
  - debian/rules clean: remove pre-built Qt moc files

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#ifndef INCLUDED_GR_BUFFER_H
24
24
#define INCLUDED_GR_BUFFER_H
25
25
 
26
 
#include <gr_runtime.h>
 
26
#include <gr_runtime_types.h>
 
27
#include <boost/weak_ptr.hpp>
 
28
#include <boost/thread.hpp>
27
29
 
28
30
class gr_vmcircbuf;
29
31
 
33
35
 * The total size of the buffer will be rounded up to a system
34
36
 * dependent boundary.  This is typically the system page size, but
35
37
 * under MS windows is 64KB.
 
38
 *
 
39
 * \param nitems is the minimum number of items the buffer will hold.
 
40
 * \param sizeof_item is the size of an item in bytes.
 
41
 * \param link is the block that writes to this buffer.
36
42
 */
37
 
gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item);
 
43
gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link=gr_block_sptr());
38
44
 
39
45
 
40
46
/*!
43
49
 */
44
50
class gr_buffer {
45
51
 public:
 
52
 
 
53
  typedef boost::unique_lock<boost::mutex>  scoped_lock;
 
54
 
46
55
  virtual ~gr_buffer ();
47
56
 
48
57
  /*!
49
58
   * \brief return number of items worth of space available for writing
50
59
   */
51
 
  int space_available () const;
 
60
  int space_available ();
 
61
 
 
62
  /*!
 
63
   * \brief return size of this buffer in items
 
64
   */
 
65
  int bufsize() const { return d_bufsize; }
52
66
 
53
67
  /*!
54
68
   * \brief return pointer to write buffer.
63
77
   */
64
78
  void update_write_pointer (int nitems);
65
79
 
66
 
 
67
 
  void set_done (bool done)   { d_done = done; }
 
80
  void set_done (bool done);
68
81
  bool done () const { return d_done; }
69
82
 
 
83
  /*!
 
84
   * \brief Return the block that writes to this buffer.
 
85
   */
 
86
  gr_block_sptr link() { return gr_block_sptr(d_link); }
 
87
 
 
88
  size_t nreaders() const { return d_readers.size(); }
 
89
  gr_buffer_reader* reader(size_t index) { return d_readers[index]; }
 
90
 
 
91
  boost::mutex *mutex() { return &d_mutex; }
 
92
 
70
93
  // -------------------------------------------------------------------------
71
94
 
72
95
 private:
73
96
 
74
97
  friend class gr_buffer_reader;
75
 
  friend gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item);
76
 
  friend gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload);
 
98
  friend gr_buffer_sptr gr_make_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
 
99
  friend gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link);
77
100
 
78
101
 protected:
79
102
  char                                 *d_base;         // base address of buffer
81
104
 private:
82
105
  gr_vmcircbuf                         *d_vmcircbuf;
83
106
  size_t                                d_sizeof_item;  // in bytes
 
107
  std::vector<gr_buffer_reader *>       d_readers;
 
108
  boost::weak_ptr<gr_block>             d_link;         // block that writes to this buffer
 
109
 
 
110
  //
 
111
  // The mutex protects d_write_index, d_done and the d_read_index's in the buffer readers.
 
112
  //
 
113
  boost::mutex                          d_mutex;
84
114
  unsigned int                          d_write_index;  // in items [0,d_bufsize)
85
 
  std::vector<gr_buffer_reader *>       d_readers;
86
115
  bool                                  d_done;
87
116
  
88
117
  unsigned
116
145
   *
117
146
   * Allocate a buffer that holds at least \p nitems of size \p sizeof_item.
118
147
   *
 
148
   * \param nitems is the minimum number of items the buffer will hold.
 
149
   * \param sizeof_item is the size of an item in bytes.
 
150
   * \param link is the block that writes to this buffer.
 
151
   *
119
152
   * The total size of the buffer will be rounded up to a system
120
153
   * dependent boundary.  This is typically the system page size, but
121
154
   * under MS windows is 64KB.
122
155
   */
123
 
  gr_buffer (int nitems, size_t sizeof_item);
 
156
  gr_buffer (int nitems, size_t sizeof_item, gr_block_sptr link);
124
157
 
125
158
  /*!
126
159
   * \brief disassociate \p reader from this buffer
130
163
};
131
164
 
132
165
/*!
133
 
 * \brief create a new gr_buffer_reader and attach it to buffer \p buf
 
166
 * \brief Create a new gr_buffer_reader and attach it to buffer \p buf
 
167
 * \param buf is the buffer the \p gr_buffer_reader reads from.
134
168
 * \param nzero_preload -- number of zero items to "preload" into buffer.
 
169
 * \param link is the block that reads from the buffer using this gr_buffer_reader.
135
170
 */
136
 
gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload);
 
171
gr_buffer_reader_sptr 
 
172
gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link=gr_block_sptr());
137
173
 
138
174
//! returns # of gr_buffers currently allocated
139
175
long gr_buffer_ncurrently_allocated ();
147
183
 */
148
184
 
149
185
class gr_buffer_reader {
150
 
 
151
186
 public:
 
187
 
 
188
  typedef gr_buffer::scoped_lock scoped_lock;
 
189
 
152
190
  ~gr_buffer_reader ();
153
191
 
154
192
  /*!
157
195
  int items_available () const;
158
196
 
159
197
  /*!
 
198
   * \brief Return buffer this reader reads from.
 
199
   */
 
200
  gr_buffer_sptr buffer () const { return d_buffer; }
 
201
 
 
202
 
 
203
  /*!
160
204
   * \brief Return maximum number of items that could ever be available for reading.
161
205
   * This is used as a sanity check in the scheduler to avoid looping forever.
162
206
   */
177
221
  void set_done (bool done)   { d_buffer->set_done (done); }
178
222
  bool done () const { return d_buffer->done (); }
179
223
 
 
224
  boost::mutex *mutex() { return d_buffer->mutex(); }
 
225
 
 
226
 
 
227
  /*!
 
228
   * \brief Return the block that reads via this reader.
 
229
   */
 
230
  gr_block_sptr link() { return gr_block_sptr(d_link); }
 
231
 
180
232
  // -------------------------------------------------------------------------
181
233
 
182
234
 private:
183
235
 
184
236
  friend class gr_buffer;
185
 
  friend gr_buffer_reader_sptr gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload);
 
237
  friend gr_buffer_reader_sptr 
 
238
  gr_buffer_add_reader (gr_buffer_sptr buf, int nzero_preload, gr_block_sptr link);
186
239
 
187
240
 
188
241
  gr_buffer_sptr                d_buffer;
189
242
  unsigned int                  d_read_index;   // in items [0,d->buffer.d_bufsize)
 
243
  boost::weak_ptr<gr_block>     d_link;         // block that reads via this buffer reader
190
244
 
191
245
  //! constructor is private.  Use gr_buffer::add_reader to create instances
192
 
  gr_buffer_reader (gr_buffer_sptr buffer, unsigned int read_index);
 
246
  gr_buffer_reader (gr_buffer_sptr buffer, unsigned int read_index, gr_block_sptr link);
193
247
};
194
248
 
195
249
//! returns # of gr_buffer_readers currently allocated