Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_msg_queue.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 #ifndef INCLUDED_GR_MSG_QUEUE_H
23 #define INCLUDED_GR_MSG_QUEUE_H
24
25 #include <gr_msg_handler.h>
26 #include <gnuradio/omnithread.h>
27
28 class gr_msg_queue;
29 typedef boost::shared_ptr<gr_msg_queue> gr_msg_queue_sptr;
30
31 gr_msg_queue_sptr gr_make_msg_queue(unsigned int limit=0);
32
33 /*!
34  * \brief thread-safe message queue
35  * \ingroup misc
36  */
37 class gr_msg_queue : public gr_msg_handler {
38   omni_mutex            d_mutex;
39   omni_condition        d_not_empty;
40   omni_condition        d_not_full;
41   gr_message_sptr       d_head;
42   gr_message_sptr       d_tail;
43   unsigned int          d_count;    // # of messages in queue.
44   unsigned int          d_limit;    // max # of messages in queue.  0 -> unbounded
45
46 public:
47   gr_msg_queue(unsigned int limit);
48   ~gr_msg_queue();
49
50   //! Generic msg_handler method: insert the message.
51   void handle(gr_message_sptr msg) { insert_tail (msg); }
52
53   /*!
54    * \brief Insert message at tail of queue.
55    * \param msg message
56    *
57    * Block if queue if full.
58    */
59   void insert_tail(gr_message_sptr msg);
60
61   /*!
62    * \brief Delete message from head of queue and return it.
63    * Block if no message is available.
64    */
65   gr_message_sptr delete_head();
66
67   /*!
68    * \brief If there's a message in the q, delete it and return it.
69    * If no message is available, return 0.
70    */
71   gr_message_sptr delete_head_nowait();
72   
73   //! Delete all messages from the queue
74   void flush();
75
76   //! is the queue empty?
77   bool empty_p() const { return d_count == 0; }
78   
79   //! is the queue full?
80   bool full_p() const { return d_limit != 0 && d_count >= d_limit; }
81   
82   //! return number of messages in queue
83   unsigned int count() const { return d_count; }
84
85   //! return limit on number of message in queue.  0 -> unbounded
86   unsigned int limit() const { return d_limit; }
87
88 };
89
90 #endif /* INCLUDED_GR_MSG_QUEUE_H */