Merge branch 'dfsg-orig'
[debian/gnuradio] / gruel / src / include / gruel / msg_queue.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009 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_MSG_QUEUE_H
23 #define INCLUDED_MSG_QUEUE_H
24
25 #include <gruel/thread.h>
26 #include <gruel/pmt.h>
27 #include <deque>
28
29 namespace gruel {
30
31   class msg_queue;
32   typedef boost::shared_ptr<msg_queue> msg_queue_sptr;
33
34   msg_queue_sptr make_msg_queue(unsigned int limit=0);
35
36   /*!
37    * \brief thread-safe message queue
38    */
39   class msg_queue {
40
41     gruel::mutex              d_mutex;
42     gruel::condition_variable d_not_empty;
43     gruel::condition_variable d_not_full;
44     unsigned int              d_limit;    // max # of messages in queue.  0 -> unbounded
45
46     std::deque<pmt::pmt_t>    d_msgs;
47
48   public:
49     msg_queue(unsigned int limit);
50     ~msg_queue();
51
52     /*!
53      * \brief Insert message at tail of queue.
54      * \param msg message
55      *
56      * Block if queue if full.
57      */
58     void insert_tail(pmt::pmt_t msg);
59
60     /*!
61      * \brief Delete message from head of queue and return it.
62      * Block if no message is available.
63      */
64     pmt::pmt_t delete_head();
65     
66     /*!
67      * \brief If there's a message in the q, delete it and return it.
68      * If no message is available, return pmt_t().
69      */
70     pmt::pmt_t delete_head_nowait();
71     
72     //! Delete all messages from the queue
73     void flush();
74
75     //! is the queue empty?
76     bool empty_p() const { return d_msgs.empty(); }
77   
78     //! is the queue full?
79     bool full_p() const { return d_limit != 0 && count() >= d_limit; }
80   
81     //! return number of messages in queue
82     unsigned int count() const { return d_msgs.size(); }
83
84     //! return limit on number of message in queue.  0 -> unbounded
85     unsigned int limit() const { return d_limit; }
86   };
87
88 } /* namespace gruel */
89
90 #endif /* INCLUDED_MSG_QUEUE_H */