GNU Radio 3.5.1 C++ API
msg_queue.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2009 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
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 #ifndef INCLUDED_MSG_QUEUE_H
00023 #define INCLUDED_MSG_QUEUE_H
00024 
00025 #include <gruel/api.h>
00026 #include <gruel/thread.h>
00027 #include <gruel/pmt.h>
00028 #include <deque>
00029 
00030 namespace gruel {
00031 
00032   class msg_queue;
00033   typedef boost::shared_ptr<msg_queue> msg_queue_sptr;
00034 
00035   msg_queue_sptr make_msg_queue(unsigned int limit=0);
00036 
00037   /*!
00038    * \brief thread-safe message queue
00039    */
00040   class GRUEL_API msg_queue {
00041 
00042     gruel::mutex              d_mutex;
00043     gruel::condition_variable d_not_empty;
00044     gruel::condition_variable d_not_full;
00045     unsigned int              d_limit;    // max # of messages in queue.  0 -> unbounded
00046 
00047     std::deque<pmt::pmt_t>    d_msgs;
00048 
00049   public:
00050     msg_queue(unsigned int limit);
00051     ~msg_queue();
00052 
00053     /*!
00054      * \brief Insert message at tail of queue.
00055      * \param msg message
00056      *
00057      * Block if queue if full.
00058      */
00059     void insert_tail(pmt::pmt_t msg);
00060 
00061     /*!
00062      * \brief Delete message from head of queue and return it.
00063      * Block if no message is available.
00064      */
00065     pmt::pmt_t delete_head();
00066     
00067     /*!
00068      * \brief If there's a message in the q, delete it and return it.
00069      * If no message is available, return pmt_t().
00070      */
00071     pmt::pmt_t delete_head_nowait();
00072     
00073     //! Delete all messages from the queue
00074     void flush();
00075 
00076     //! is the queue empty?
00077     bool empty_p() const { return d_msgs.empty(); }
00078   
00079     //! is the queue full?
00080     bool full_p() const { return d_limit != 0 && count() >= d_limit; }
00081   
00082     //! return number of messages in queue
00083     unsigned int count() const { return d_msgs.size(); }
00084 
00085     //! return limit on number of message in queue.  0 -> unbounded
00086     unsigned int limit() const { return d_limit; }
00087   };
00088 
00089 } /* namespace gruel */
00090 
00091 #endif /* INCLUDED_MSG_QUEUE_H */