GNU Radio Manual and C++ API Reference  3.7.9.2
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
messages/msg_queue.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2009,2013 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 
23 #ifndef INCLUDED_MSG_QUEUE_H
24 #define INCLUDED_MSG_QUEUE_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/thread/thread.h>
28 #include <pmt/pmt.h>
29 #include <deque>
30 
31 namespace gr {
32  namespace messages {
33 
34  class msg_queue;
35  typedef boost::shared_ptr<msg_queue> msg_queue_sptr;
36 
37  msg_queue_sptr make_msg_queue(unsigned int limit=0);
38 
39  /*!
40  * \brief thread-safe message queue
41  */
43  {
44  private:
45  gr::thread::mutex d_mutex;
48  unsigned int d_limit; // max # of messages in queue. 0 -> unbounded
49 
50  std::deque<pmt::pmt_t> d_msgs;
51 
52  public:
53  msg_queue(unsigned int limit);
54  ~msg_queue();
55 
56  /*!
57  * \brief Insert message at tail of queue.
58  * \param msg message
59  *
60  * Block if queue if full.
61  */
62  void insert_tail(pmt::pmt_t msg);
63 
64  /*!
65  * \brief Delete message from head of queue and return it.
66  * Block if no message is available.
67  */
68  pmt::pmt_t delete_head();
69 
70  /*!
71  * \brief If there's a message in the q, delete it and return it.
72  * If no message is available, return pmt::pmt_t().
73  */
74  pmt::pmt_t delete_head_nowait();
75 
76  //! Delete all messages from the queue
77  void flush();
78 
79  //! is the queue empty?
80  bool empty_p() const { return d_msgs.empty(); }
81 
82  //! is the queue full?
83  bool full_p() const { return d_limit != 0 && count() >= d_limit; }
84 
85  //! return number of messages in queue
86  unsigned int count() const { return d_msgs.size(); }
87 
88  //! return limit on number of message in queue. 0 -> unbounded
89  unsigned int limit() const { return d_limit; }
90  };
91 
92  } /* namespace messages */
93 } /* namespace gr */
94 
95 #endif /* INCLUDED_MSG_QUEUE_H */
thread-safe message queue
Definition: messages/msg_queue.h:42
msg_queue_sptr make_msg_queue(unsigned int limit=0)
unsigned int count() const
return number of messages in queue
Definition: messages/msg_queue.h:86
bool empty_p() const
is the queue empty?
Definition: messages/msg_queue.h:80
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
bool full_p() const
is the queue full?
Definition: messages/msg_queue.h:83
Include this header to use the message passing features.
Definition: logger.h:131
boost::mutex mutex
Definition: thread.h:46
boost::intrusive_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting). See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
Definition: pmt.h:56
unsigned int limit() const
return limit on number of message in queue. 0 -> unbounded
Definition: messages/msg_queue.h:89
boost::condition_variable condition_variable
Definition: thread.h:48