Merge commit 'v3.3.1' into try-3.3.1
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_tpb_detail.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <gr_tpb_detail.h>
26 #include <gr_block.h>
27 #include <gr_block_detail.h>
28 #include <gr_buffer.h>
29
30 using namespace pmt;
31
32 /*
33  * We assume that no worker threads are ever running when the
34  * graph structure is being manipulated, thus it's safe for us to poke
35  * around in our neighbors w/o holding any locks.
36  */
37
38 void
39 gr_tpb_detail::notify_upstream(gr_block_detail *d)
40 {
41   // For each of our inputs, tell the guy upstream that we've consumed
42   // some input, and that he most likely has more output buffer space
43   // available.
44
45   for (size_t i = 0; i < d->d_input.size(); i++){
46     // Can you say, "pointer chasing?"
47     d->d_input[i]->buffer()->link()->detail()->d_tpb.set_output_changed();
48   }
49 }
50
51 void
52 gr_tpb_detail::notify_downstream(gr_block_detail *d)
53 {
54   // For each of our outputs, tell the guys downstream that they have
55   // new input available.
56
57   for (size_t i = 0; i < d->d_output.size(); i++){
58     gr_buffer_sptr buf = d->d_output[i];
59     for (size_t j = 0, k = buf->nreaders(); j < k; j++)
60       buf->reader(j)->link()->detail()->d_tpb.set_input_changed();
61   }
62 }
63
64 void
65 gr_tpb_detail::notify_neighbors(gr_block_detail *d)
66 {
67   notify_downstream(d);
68   notify_upstream(d);
69 }
70
71 void
72 gr_tpb_detail::insert_tail(pmt::pmt_t msg)
73 {
74   gruel::scoped_lock guard(mutex);
75
76   msg_queue.push_back(msg);
77
78   // wake up thread if BLKD_IN or BLKD_OUT
79   input_cond.notify_one();
80   output_cond.notify_one();
81 }
82
83 pmt_t 
84 gr_tpb_detail::delete_head_nowait()
85 {
86   gruel::scoped_lock guard(mutex);
87
88   if (empty_p())
89     return pmt_t();
90
91   pmt_t m(msg_queue.front());
92   msg_queue.pop_front();
93
94   return m;
95 }
96
97 /*
98  * Caller must already be holding the mutex
99  */
100 pmt_t 
101 gr_tpb_detail::delete_head_nowait_already_holding_mutex()
102 {
103   if (empty_p())
104     return pmt_t();
105
106   pmt_t m(msg_queue.front());
107   msg_queue.pop_front();
108
109   return m;
110 }