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
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  * 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_GR_MSG_QUEUE_H
24 #define INCLUDED_GR_MSG_QUEUE_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/msg_handler.h>
28 #include <gnuradio/thread/thread.h>
29 
30 namespace gr {
31 
32  /*!
33  * \brief thread-safe message queue
34  * \ingroup misc
35  */
37  {
38  gr::thread::mutex d_mutex;
41  message::sptr d_head;
42  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  typedef boost::shared_ptr<msg_queue> sptr;
48 
49  static sptr make(unsigned int limit=0);
50 
51  msg_queue(unsigned int limit);
52  ~msg_queue();
53 
54  //! Generic msg_handler method: insert the message.
55  void handle(message::sptr msg) { insert_tail (msg); }
56 
57  /*!
58  * \brief Insert message at tail of queue.
59  * \param msg message
60  *
61  * Block if queue if full.
62  */
63  void insert_tail(message::sptr msg);
64 
65  /*!
66  * \brief Delete message from head of queue and return it.
67  * Block if no message is available.
68  */
69  message::sptr delete_head();
70 
71  /*!
72  * \brief If there's a message in the q, delete it and return it.
73  * If no message is available, return 0.
74  */
75  message::sptr delete_head_nowait();
76 
77  //! Delete all messages from the queue
78  void flush();
79 
80  //! is the queue empty?
81  bool empty_p() const { return d_count == 0; }
82 
83  //! is the queue full?
84  bool full_p() const { return d_limit != 0 && d_count >= d_limit; }
85 
86  //! return number of messages in queue
87  unsigned int count() const { return d_count; }
88 
89  //! return limit on number of message in queue. 0 -> unbounded
90  unsigned int limit() const { return d_limit; }
91  };
92 
93 } /* namespace gr */
94 
95 #endif /* INCLUDED_GR_MSG_QUEUE_H */
boost::shared_ptr< msg_queue > sptr
Definition: msg_queue.h:47
unsigned int limit() const
return limit on number of message in queue. 0 -> unbounded
Definition: msg_queue.h:90
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
void handle(message::sptr msg)
Generic msg_handler method: insert the message.
Definition: msg_queue.h:55
bool full_p() const
is the queue full?
Definition: msg_queue.h:84
thread-safe message queue
Definition: msg_queue.h:36
Include this header to use the message passing features.
Definition: logger.h:131
unsigned int count() const
return number of messages in queue
Definition: msg_queue.h:87
bool empty_p() const
is the queue empty?
Definition: msg_queue.h:81
boost::shared_ptr< message > sptr
Definition: message.h:42
boost::mutex mutex
Definition: thread.h:46
boost::condition_variable condition_variable
Definition: thread.h:48
abstract class of message handlers
Definition: msg_handler.h:38