GNU Radio 3.4.0 C++ API
ring.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2008,2010 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License along
00018  * with this program; if not, write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00020  */
00021 #ifndef INCLUDED_RING_H
00022 #define INCLUDED_RING_H
00023 
00024 #include <stddef.h>
00025 #include <vector>
00026 #include <boost/shared_ptr.hpp>
00027 #include <gruel/thread.h>
00028 
00029 namespace usrp2 {
00030 
00031   class ring;
00032   typedef boost::shared_ptr<ring> ring_sptr;
00033 
00034   class ring
00035   {
00036   private:
00037  
00038     size_t d_max;
00039     size_t d_read_ind;
00040     size_t d_write_ind;
00041 
00042     struct ring_desc
00043     {
00044       void *d_base;
00045       size_t d_len;
00046     };
00047     std::vector<ring_desc> d_ring;
00048 
00049     gruel::mutex d_mutex;
00050     gruel::condition_variable d_not_empty;
00051 
00052     void inc_read_ind()
00053     {
00054       if (d_read_ind + 1 >= d_max)
00055         d_read_ind = 0;
00056       else
00057         d_read_ind = d_read_ind + 1;
00058     }
00059 
00060     void inc_write_ind()
00061     {
00062       if (d_write_ind + 1 >= d_max)
00063         d_write_ind = 0;
00064       else
00065         d_write_ind = d_write_ind + 1;
00066     }
00067         
00068     bool empty() const { return d_read_ind == d_write_ind; }
00069     bool full() const { return (d_write_ind+1)%d_max == d_read_ind; }
00070 
00071   public:
00072     
00073     ring(unsigned int entries);
00074 
00075     void wait_for_not_empty();
00076 
00077     bool enqueue(void *p, size_t len);
00078     bool dequeue(void **p, size_t *len);
00079   };
00080 
00081 }  // namespace usrp2
00082 
00083 #endif /* INCLUDED_RING_H */