Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / host / lib / ring.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "ring.h"
27
28 namespace usrp2 {
29
30   ring::ring(unsigned int entries)
31     : d_max(entries), d_read_ind(0), d_write_ind(0), d_ring(entries),
32       d_mutex(), d_not_empty(&d_mutex)
33   {
34     for (unsigned int i = 0; i < entries; i++) {
35       d_ring[i].d_base = 0;
36       d_ring[i].d_len = 0;
37     }
38   }
39
40   void 
41   ring::wait_for_not_empty() 
42   { 
43     omni_mutex_lock l(d_mutex);
44     while (empty()) 
45       d_not_empty.wait();
46   }
47
48   bool
49   ring::enqueue(void *p, size_t len)
50   {
51     omni_mutex_lock l(d_mutex);
52     if (full())
53       return false;
54       
55     d_ring[d_write_ind].d_len = len;
56     d_ring[d_write_ind].d_base = p;
57
58     inc_write_ind();
59     d_not_empty.signal();
60     return true;
61   }
62
63   bool
64   ring::dequeue(void **p, size_t *len)
65   {
66     omni_mutex_lock l(d_mutex);
67     if (empty())
68       return false;
69       
70     *p   = d_ring[d_read_ind].d_base;
71     *len = d_ring[d_read_ind].d_len;
72
73     inc_read_ind();
74     return true;
75   }
76   
77 } // namespace usrp2
78