GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
msg_queue.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005,2009 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_GR_MSG_QUEUE_H
12 #define INCLUDED_GR_MSG_QUEUE_H
13 
14 #include <gnuradio/api.h>
15 #include <gnuradio/msg_handler.h>
16 #include <gnuradio/thread/thread.h>
17 
18 namespace gr {
19 
20 /*!
21  * \brief thread-safe message queue
22  * \ingroup misc
23  */
25 {
26  gr::thread::mutex d_mutex;
29  message::sptr d_head;
30  message::sptr d_tail;
31  unsigned int d_count; // # of messages in queue.
32  unsigned int d_limit; // max # of messages in queue. 0 -> unbounded
33 
34 public:
35  typedef std::shared_ptr<msg_queue> sptr;
36 
37  static sptr make(unsigned int limit = 0);
38 
39  msg_queue(unsigned int limit);
40  ~msg_queue() override;
41 
42  //! Generic msg_handler method: insert the message.
43  void handle(message::sptr msg) override { insert_tail(msg); }
44 
45  /*!
46  * \brief Insert message at tail of queue.
47  * \param msg message
48  *
49  * Block if queue if full.
50  */
52 
53  /*!
54  * \brief Delete message from head of queue and return it.
55  * Block if no message is available.
56  */
58 
59  /*!
60  * \brief If there's a message in the q, delete it and return it.
61  * If no message is available, return 0.
62  */
64 
65  //! Delete all messages from the queue
66  void flush();
67 
68  //! is the queue empty?
69  bool empty_p() const { return d_count == 0; }
70 
71  //! is the queue full?
72  bool full_p() const { return d_limit != 0 && d_count >= d_limit; }
73 
74  //! return number of messages in queue
75  unsigned int count() const { return d_count; }
76 
77  //! return limit on number of message in queue. 0 -> unbounded
78  unsigned int limit() const { return d_limit; }
79 };
80 
81 } /* namespace gr */
82 
83 #endif /* INCLUDED_GR_MSG_QUEUE_H */
std::shared_ptr< message > sptr
Definition: message.h:30
abstract class of message handlers
Definition: msg_handler.h:27
thread-safe message queue
Definition: msg_queue.h:25
unsigned int count() const
return number of messages in queue
Definition: msg_queue.h:75
std::shared_ptr< msg_queue > sptr
Definition: msg_queue.h:35
~msg_queue() override
msg_queue(unsigned int limit)
message::sptr delete_head()
Delete message from head of queue and return it. Block if no message is available.
void flush()
Delete all messages from the queue.
static sptr make(unsigned int limit=0)
void handle(message::sptr msg) override
Generic msg_handler method: insert the message.
Definition: msg_queue.h:43
unsigned int limit() const
return limit on number of message in queue. 0 -> unbounded
Definition: msg_queue.h:78
bool empty_p() const
is the queue empty?
Definition: msg_queue.h:69
void insert_tail(message::sptr msg)
Insert message at tail of queue.
bool full_p() const
is the queue full?
Definition: msg_queue.h:72
message::sptr delete_head_nowait()
If there's a message in the q, delete it and return it. If no message is available,...
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
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