3 * Copyright 2006,2009,2010 Free Software Foundation, Inc.
5 * This file is part of GNU Radio.
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)
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.
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.
23 #ifndef _CIRCULAR_BUFFER_H_
24 #define _CIRCULAR_BUFFER_H_
26 #include <gruel/thread.h>
35 #define DEBUG(X) do{X} while(0);
37 #define DEBUG(X) do{} while(0);
47 // the following are in Items (type T)
48 size_t d_bufLen_I, d_readNdx_I, d_writeNdx_I;
49 size_t d_n_avail_write_I, d_n_avail_read_I;
51 // stuff to control access to class internals
52 gruel::mutex* d_internal;
53 gruel::condition_variable* d_readBlock;
54 gruel::condition_variable* d_writeBlock;
56 // booleans to decide how to control reading, writing, and aborting
57 bool d_doWriteBlock, d_doFullRead, d_doAbort;
59 void delete_mutex_cond () {
75 circular_buffer (size_t bufLen_I,
76 bool doWriteBlock = true, bool doFullRead = false) {
78 throw std::runtime_error ("circular_buffer(): "
79 "Number of items to buffer must be > 0.\n");
80 d_bufLen_I = bufLen_I;
81 d_buffer = (T*) new T[d_bufLen_I];
82 d_doWriteBlock = doWriteBlock;
83 d_doFullRead = doFullRead;
85 d_readBlock = d_writeBlock = NULL;
87 DEBUG (std::cerr << "c_b(): buf len (items) = " << d_bufLen_
88 << ", doWriteBlock = " << (d_doWriteBlock ? "true" : "false")
89 << ", doFullRead = " << (d_doFullRead ? "true" : "false")
98 inline size_t n_avail_write_items () {
99 gruel::scoped_lock l (*d_internal);
100 size_t retVal = d_n_avail_write_I;
104 inline size_t n_avail_read_items () {
105 gruel::scoped_lock l (*d_internal);
106 size_t retVal = d_n_avail_read_I;
110 inline size_t buffer_length_items () {return (d_bufLen_I);};
111 inline bool do_write_block () {return (d_doWriteBlock);};
112 inline bool do_full_read () {return (d_doFullRead);};
116 bzero (d_buffer, d_bufLen_I * sizeof (T));
117 d_readNdx_I = d_writeNdx_I = d_n_avail_read_I = 0;
118 d_n_avail_write_I = d_bufLen_I;
119 delete_mutex_cond ();
120 // create a mutex to handle contention of shared resources;
121 // any routine needed access to shared resources uses lock()
122 // before doing anything, then unlock() when finished.
123 d_internal = new gruel::mutex ();
124 // link the internal mutex to the read and write conditions;
125 // when wait() is called, the internal mutex will automatically
126 // be unlock()'ed. Upon return (from a notify_one() to the condition),
127 // the internal mutex will be lock()'ed.
128 d_readBlock = new gruel::condition_variable ();
129 d_writeBlock = new gruel::condition_variable ();
133 * enqueue: add the given buffer of item-length to the queue,
134 * first-in-first-out (FIFO).
137 * buf: a pointer to the buffer holding the data
139 * bufLen_I: the buffer length in items (of the instantiated type)
142 * -1: on overflow (write is not blocking, and data is being
143 * written faster than it is being read)
144 * 0: if nothing to do (0 length buffer)
146 * 2: in the process of aborting, do doing nothing
148 * will throw runtime errors if inputs are improper:
149 * buffer pointer is NULL
150 * buffer length is larger than the instantiated buffer length
153 int enqueue (T* buf, size_t bufLen_I) {
154 DEBUG (std::cerr << "enqueue: buf = " << (void*) buf
155 << ", bufLen = " << bufLen_I
156 << ", #av_wr = " << d_n_avail_write_I
157 << ", #av_rd = " << d_n_avail_read_I << std::endl);
158 if (bufLen_I > d_bufLen_I) {
159 std::cerr << "ERROR: cannot add buffer longer ("
160 << bufLen_I << ") than instantiated length ("
161 << d_bufLen_I << ")." << std::endl;
162 throw std::runtime_error ("circular_buffer::enqueue()");
168 throw std::runtime_error ("circular_buffer::enqueue(): "
169 "input buffer is NULL.\n");
170 gruel::scoped_lock l (*d_internal);
174 // set the return value to 1: success; change if needed
176 if (bufLen_I > d_n_avail_write_I) {
177 if (d_doWriteBlock) {
178 while (bufLen_I > d_n_avail_write_I) {
179 DEBUG (std::cerr << "enqueue: #len > #a, waiting." << std::endl);
180 // wait; will automatically unlock() the internal mutex via
182 d_writeBlock->wait (l);
183 // and auto re-lock() it here.
185 DEBUG (std::cerr << "enqueue: #len > #a, aborting." << std::endl);
188 DEBUG (std::cerr << "enqueue: #len > #a, done waiting." << std::endl);
191 d_n_avail_read_I = d_bufLen_I - bufLen_I;
192 d_n_avail_write_I = bufLen_I;
193 DEBUG (std::cerr << "circular_buffer::enqueue: overflow" << std::endl);
197 size_t n_now_I = d_bufLen_I - d_writeNdx_I, n_start_I = 0;
198 if (n_now_I > bufLen_I)
200 else if (n_now_I < bufLen_I)
201 n_start_I = bufLen_I - n_now_I;
202 bcopy (buf, &(d_buffer[d_writeNdx_I]), n_now_I * sizeof (T));
204 bcopy (&(buf[n_now_I]), d_buffer, n_start_I * sizeof (T));
205 d_writeNdx_I = n_start_I;
207 d_writeNdx_I += n_now_I;
208 d_n_avail_read_I += bufLen_I;
209 d_n_avail_write_I -= bufLen_I;
210 d_readBlock->notify_one ();
215 * dequeue: removes from the queue the number of items requested, or
216 * available, into the given buffer on a FIFO basis.
219 * buf: a pointer to the buffer into which to copy the data
221 * bufLen_I: pointer to the number of items to remove in items
222 * (of the instantiated type)
225 * 0: if nothing to do (0 length buffer)
227 * 2: in the process of aborting, do doing nothing
229 * will throw runtime errors if inputs are improper:
230 * buffer pointer is NULL
231 * buffer length pointer is NULL
232 * buffer length is larger than the instantiated buffer length
235 int dequeue (T* buf, size_t* bufLen_I) {
236 DEBUG (std::cerr << "dequeue: buf = " << ((void*) buf)
237 << ", *bufLen = " << (*bufLen_I)
238 << ", #av_wr = " << d_n_avail_write_I
239 << ", #av_rd = " << d_n_avail_read_I << std::endl);
241 throw std::runtime_error ("circular_buffer::dequeue(): "
242 "input bufLen pointer is NULL.\n");
244 throw std::runtime_error ("circular_buffer::dequeue(): "
245 "input buffer pointer is NULL.\n");
246 size_t l_bufLen_I = *bufLen_I;
249 if (l_bufLen_I > d_bufLen_I) {
250 std::cerr << "ERROR: cannot remove buffer longer ("
251 << l_bufLen_I << ") than instantiated length ("
252 << d_bufLen_I << ")." << std::endl;
253 throw std::runtime_error ("circular_buffer::dequeue()");
256 gruel::scoped_lock l (*d_internal);
261 while (d_n_avail_read_I < l_bufLen_I) {
262 DEBUG (std::cerr << "dequeue: #a < #len, waiting." << std::endl);
263 // wait; will automatically unlock() the internal mutex via
265 d_readBlock->wait (l);
266 // and re-lock() it here.
268 DEBUG (std::cerr << "dequeue: #a < #len, aborting." << std::endl);
271 DEBUG (std::cerr << "dequeue: #a < #len, done waiting." << std::endl);
274 while (d_n_avail_read_I == 0) {
275 DEBUG (std::cerr << "dequeue: #a == 0, waiting." << std::endl);
276 // wait; will automatically unlock() the internal mutex via
278 d_readBlock->wait (l);
279 // and re-lock() it here.
281 DEBUG (std::cerr << "dequeue: #a == 0, aborting." << std::endl);
284 DEBUG (std::cerr << "dequeue: #a == 0, done waiting." << std::endl);
287 if (l_bufLen_I > d_n_avail_read_I)
288 l_bufLen_I = d_n_avail_read_I;
289 size_t n_now_I = d_bufLen_I - d_readNdx_I, n_start_I = 0;
290 if (n_now_I > l_bufLen_I)
291 n_now_I = l_bufLen_I;
292 else if (n_now_I < l_bufLen_I)
293 n_start_I = l_bufLen_I - n_now_I;
294 bcopy (&(d_buffer[d_readNdx_I]), buf, n_now_I * sizeof (T));
296 bcopy (d_buffer, &(buf[n_now_I]), n_start_I * sizeof (T));
297 d_readNdx_I = n_start_I;
299 d_readNdx_I += n_now_I;
300 *bufLen_I = l_bufLen_I;
301 d_n_avail_read_I -= l_bufLen_I;
302 d_n_avail_write_I += l_bufLen_I;
303 d_writeBlock->notify_one ();
308 gruel::scoped_lock l (*d_internal);
310 d_writeBlock->notify_one ();
311 d_readBlock->notify_one ();
315 #endif /* _CIRCULAR_BUFFER_H_ */