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