GNU Radio 3.6.4.1 C++ API
gr_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 #ifndef INCLUDED_GR_MSG_QUEUE_H
23 #define INCLUDED_GR_MSG_QUEUE_H
24 
25 #include <gr_core_api.h>
26 #include <gr_msg_handler.h>
27 #include <gruel/thread.h>
28 
29 class gr_msg_queue;
31 
33 
34 /*!
35  * \brief thread-safe message queue
36  * \ingroup misc
37  */
39 
40  gruel::mutex d_mutex;
41  gruel::condition_variable d_not_empty;
42  gruel::condition_variable d_not_full;
43  gr_message_sptr d_head;
44  gr_message_sptr d_tail;
45  unsigned int d_count; // # of messages in queue.
46  unsigned int d_limit; // max # of messages in queue. 0 -> unbounded
47 
48 public:
49  gr_msg_queue(unsigned int limit);
50  ~gr_msg_queue();
51 
52  //! Generic msg_handler method: insert the message.
53  void handle(gr_message_sptr msg) { insert_tail (msg); }
54 
55  /*!
56  * \brief Insert message at tail of queue.
57  * \param msg message
58  *
59  * Block if queue if full.
60  */
61  void insert_tail(gr_message_sptr msg);
62 
63  /*!
64  * \brief Delete message from head of queue and return it.
65  * Block if no message is available.
66  */
67  gr_message_sptr delete_head();
68 
69  /*!
70  * \brief If there's a message in the q, delete it and return it.
71  * If no message is available, return 0.
72  */
73  gr_message_sptr delete_head_nowait();
74 
75  //! Delete all messages from the queue
76  void flush();
77 
78  //! is the queue empty?
79  bool empty_p() const { return d_count == 0; }
80 
81  //! is the queue full?
82  bool full_p() const { return d_limit != 0 && d_count >= d_limit; }
83 
84  //! return number of messages in queue
85  unsigned int count() const { return d_count; }
86 
87  //! return limit on number of message in queue. 0 -> unbounded
88  unsigned int limit() const { return d_limit; }
89 
90 };
91 
92 #endif /* INCLUDED_GR_MSG_QUEUE_H */