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