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