GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
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  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef INCLUDED_MSG_QUEUE_H
12 #define INCLUDED_MSG_QUEUE_H
13 
14 #include <gnuradio/api.h>
15 #include <gnuradio/thread/thread.h>
16 #include <pmt/pmt.h>
17 #include <deque>
18 
19 namespace gr {
20 namespace messages {
21 
22 class msg_queue;
23 typedef std::shared_ptr<msg_queue> msg_queue_sptr;
24 
25 msg_queue_sptr make_msg_queue(unsigned int limit = 0);
26 
27 /*!
28  * \brief thread-safe message queue
29  */
31 {
32 private:
33  gr::thread::mutex d_mutex;
36  unsigned int d_limit; // max # of messages in queue. 0 -> unbounded
37 
38  std::deque<pmt::pmt_t> d_msgs;
39 
40 public:
41  msg_queue(unsigned int limit);
43 
44  /*!
45  * \brief Insert message at tail of queue.
46  * \param msg message
47  *
48  * Block if queue if full.
49  */
51 
52  /*!
53  * \brief Delete message from head of queue and return it.
54  * Block if no message is available.
55  */
57 
58  /*!
59  * \brief If there's a message in the q, delete it and return it.
60  * If no message is available, return pmt::pmt_t().
61  */
63 
64  //! Delete all messages from the queue
65  void flush();
66 
67  //! is the queue empty?
68  bool empty_p() const { return d_msgs.empty(); }
69 
70  //! is the queue full?
71  bool full_p() const { return d_limit != 0 && count() >= d_limit; }
72 
73  //! return number of messages in queue
74  unsigned int count() const { return d_msgs.size(); }
75 
76  //! return limit on number of message in queue. 0 -> unbounded
77  unsigned int limit() const { return d_limit; }
78 };
79 
80 } /* namespace messages */
81 } /* namespace gr */
82 
83 #endif /* INCLUDED_MSG_QUEUE_H */
thread-safe message queue
Definition: messages/msg_queue.h:31
bool full_p() const
is the queue full?
Definition: messages/msg_queue.h:71
pmt::pmt_t delete_head_nowait()
If there's a message in the q, delete it and return it. If no message is available,...
void flush()
Delete all messages from the queue.
bool empty_p() const
is the queue empty?
Definition: messages/msg_queue.h:68
unsigned int count() const
return number of messages in queue
Definition: messages/msg_queue.h:74
pmt::pmt_t delete_head()
Delete message from head of queue and return it. Block if no message is available.
void insert_tail(pmt::pmt_t msg)
Insert message at tail of queue.
unsigned int limit() const
return limit on number of message in queue. 0 -> unbounded
Definition: messages/msg_queue.h:77
msg_queue(unsigned int limit)
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
msg_queue_sptr make_msg_queue(unsigned int limit=0)
GR_RUNTIME_API const pmt::pmt_t msg()
boost::mutex mutex
Definition: thread.h:37
boost::condition_variable condition_variable
Definition: thread.h:39
GNU Radio logging wrapper.
Definition: basic_block.h:29
std::shared_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting).
Definition: pmt.h:83