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