Merge branch 'upstream' into dfsg-orig
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_msg_queue.i
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 class gr_msg_queue;
24 typedef boost::shared_ptr<gr_msg_queue> gr_msg_queue_sptr;
25 %template(gr_msg_queue_sptr) boost::shared_ptr<gr_msg_queue>;
26
27 %rename(msg_queue) gr_make_msg_queue;
28 gr_msg_queue_sptr gr_make_msg_queue(unsigned limit=0);
29
30 /*!
31  * \brief thread-safe message queue
32  */
33 %ignore gr_msg_queue;
34 class gr_msg_queue : public gr_msg_handler {
35 public:
36   gr_msg_queue(unsigned int limit);
37   ~gr_msg_queue();
38
39   //! Generic msg_handler method: insert the message.
40   //void handle(gr_message_sptr msg) { insert_tail (msg); }
41
42   /*!
43    * \brief Insert message at tail of queue.
44    * \param msg message
45    *
46    * Block if queue if full.
47    */
48   //void insert_tail(gr_message_sptr msg);
49
50   /*!
51    * \brief Delete message from head of queue and return it.
52    * Block if no message is available.
53    */
54   //gr_message_sptr delete_head();
55
56   /*!
57    * \brief If there's a message in the q, delete it and return it.
58    * If no message is available, return 0.
59    */
60   gr_message_sptr delete_head_nowait();
61   
62   //! is the queue empty?
63   bool empty_p() const;
64   
65   //! is the queue full?
66   bool full_p() const;
67   
68   //! return number of messages in queue
69   unsigned int count() const;
70
71   //! Delete all messages from the queue
72   void flush();
73 };
74
75 /*
76  * The following kludge-o-rama releases the Python global interpreter
77  * lock around these potentially blocking calls.  We don't want
78  * libgnuradio-core to be dependent on Python, thus we create these
79  * functions that serve as replacements for the normal C++ delete_head
80  * and insert_tail methods.  The %pythoncode smashes these new C++
81  * functions into the gr.msg_queue wrapper class, so that everything
82  * appears normal.  (An evil laugh is heard in the distance...)
83  */
84 %inline %{
85   gr_message_sptr gr_py_msg_queue__delete_head(gr_msg_queue_sptr q) {
86     gr_message_sptr msg;
87     Py_BEGIN_ALLOW_THREADS;             // release global interpreter lock
88     msg = q->delete_head();             // possibly blocking call
89     Py_END_ALLOW_THREADS;               // acquire global interpreter lock
90     return msg;
91   }
92
93   void gr_py_msg_queue__insert_tail(gr_msg_queue_sptr q, gr_message_sptr msg) {
94     Py_BEGIN_ALLOW_THREADS;             // release global interpreter lock
95     q->insert_tail(msg);                // possibly blocking call
96     Py_END_ALLOW_THREADS;               // acquire global interpreter lock
97   }
98 %}
99
100 // smash in new python delete_head and insert_tail methods...
101 %pythoncode %{
102 gr_msg_queue_sptr.delete_head = gr_py_msg_queue__delete_head
103 gr_msg_queue_sptr.insert_tail = gr_py_msg_queue__insert_tail
104 gr_msg_queue_sptr.handle = gr_py_msg_queue__insert_tail
105 %}