Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / host / lib / ring.h
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 #ifndef INCLUDED_RING_H
22 #define INCLUDED_RING_H
23
24 #include <gnuradio/omnithread.h>
25 #include <stddef.h>
26 #include <vector>
27 #include <boost/shared_ptr.hpp>
28
29 namespace usrp2 {
30
31   class ring;
32   typedef boost::shared_ptr<ring> ring_sptr;
33
34   class ring
35   {
36   private:
37  
38     size_t d_max;
39     size_t d_read_ind;
40     size_t d_write_ind;
41
42     struct ring_desc
43     {
44       void *d_base;
45       size_t d_len;
46     };
47     std::vector<ring_desc> d_ring;
48
49     omni_mutex d_mutex;
50     omni_condition d_not_empty;
51
52     void inc_read_ind()
53     {
54       if (d_read_ind + 1 >= d_max)
55         d_read_ind = 0;
56       else
57         d_read_ind = d_read_ind + 1;
58     }
59
60     void inc_write_ind()
61     {
62       if (d_write_ind + 1 >= d_max)
63         d_write_ind = 0;
64       else
65         d_write_ind = d_write_ind + 1;
66     }
67         
68     bool empty() const { return d_read_ind == d_write_ind; }
69     bool full() const { return (d_write_ind+1)%d_max == d_read_ind; }
70
71   public:
72     
73     ring(unsigned int entries);
74
75     void wait_for_not_empty();
76
77     bool enqueue(void *p, size_t len);
78     bool dequeue(void **p, size_t *len);
79   };
80
81 }  // namespace usrp2
82
83 #endif /* INCLUDED_RING_H */