GNU Radio 3.4.2 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/thread.h>
00026 #include <gruel/pmt.h>
00027 #include <deque>
00028 
00029 namespace gruel {
00030 
00031   class msg_queue;
00032   typedef boost::shared_ptr<msg_queue> msg_queue_sptr;
00033 
00034   msg_queue_sptr make_msg_queue(unsigned int limit=0);
00035 
00036   /*!
00037    * \brief thread-safe message queue
00038    */
00039   class msg_queue {
00040 
00041     gruel::mutex              d_mutex;
00042     gruel::condition_variable d_not_empty;
00043     gruel::condition_variable d_not_full;
00044     unsigned int              d_limit;    // max # of messages in queue.  0 -> unbounded
00045 
00046     std::deque<pmt::pmt_t>    d_msgs;
00047 
00048   public:
00049     msg_queue(unsigned int limit);
00050     ~msg_queue();
00051 
00052     /*!
00053      * \brief Insert message at tail of queue.
00054      * \param msg message
00055      *
00056      * Block if queue if full.
00057      */
00058     void insert_tail(pmt::pmt_t msg);
00059 
00060     /*!
00061      * \brief Delete message from head of queue and return it.
00062      * Block if no message is available.
00063      */
00064     pmt::pmt_t delete_head();
00065     
00066     /*!
00067      * \brief If there's a message in the q, delete it and return it.
00068      * If no message is available, return pmt_t().
00069      */
00070     pmt::pmt_t delete_head_nowait();
00071     
00072     //! Delete all messages from the queue
00073     void flush();
00074 
00075     //! is the queue empty?
00076     bool empty_p() const { return d_msgs.empty(); }
00077   
00078     //! is the queue full?
00079     bool full_p() const { return d_limit != 0 && count() >= d_limit; }
00080   
00081     //! return number of messages in queue
00082     unsigned int count() const { return d_msgs.size(); }
00083 
00084     //! return limit on number of message in queue.  0 -> unbounded
00085     unsigned int limit() const { return d_limit; }
00086   };
00087 
00088 } /* namespace gruel */
00089 
00090 #endif /* INCLUDED_MSG_QUEUE_H */