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