c601b588c2d66dfb87ef47ea672918e99ff3351e
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_tpb_thread_body.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 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <gr_tpb_thread_body.h>
25 #include <iostream>
26 #include <boost/thread.hpp>
27 #include <gruel/pmt.h>
28
29 using namespace pmt;
30
31 gr_tpb_thread_body::gr_tpb_thread_body(gr_block_sptr block)
32   : d_exec(block)
33 {
34   // std::cerr << "gr_tpb_thread_body: " << block << std::endl;
35
36   gr_block_detail *d = block->detail().get();
37   gr_block_executor::state s;
38
39   while (1){
40     boost::this_thread::interruption_point();
41  
42     while (!block->msg_queue()->empty_p())
43       block->handle_msg(block->msg_queue()->delete_head_nowait());
44
45     d->d_tpb.clear_changed();
46     s = d_exec.run_one_iteration();
47
48     switch(s){
49     case gr_block_executor::READY:              // Tell neighbors we made progress.
50       d->d_tpb.notify_neighbors(d);
51       break;
52
53     case gr_block_executor::READY_NO_OUTPUT:    // Notify upstream only
54       d->d_tpb.notify_upstream(d);
55       break;
56
57     case gr_block_executor::DONE:               // Game over.
58       d->d_tpb.notify_neighbors(d);
59       return;
60
61     case gr_block_executor::BLKD_IN:            // Wait for input.
62       while (!d->d_tpb.input_changed) 
63       {
64         boost::this_thread::interruption_point();
65         gruel::scoped_lock guard(d->d_tpb.mutex);
66         
67         // Block then wake on input_changed or msg arrived
68         while(!d->d_tpb.input_changed && !block->msg_queue()->empty_p())
69           d->d_tpb.input_cond.wait(guard); 
70
71         // Run msgq while unlocked
72         guard.unlock();
73         while (!block->msg_queue()->empty_p())
74           block->handle_msg(block->msg_queue()->delete_head_nowait());
75       }
76       break;
77
78       
79     case gr_block_executor::BLKD_OUT:           // Wait for output buffer space.
80       while (!d->d_tpb.output_changed) 
81       {
82         boost::this_thread::interruption_point();
83         gruel::scoped_lock guard(d->d_tpb.mutex);
84
85         // Block then wake on output_changed or msg arrived
86         while(!d->d_tpb.output_changed && !block->msg_queue()->empty_p())
87           d->d_tpb.output_cond.wait(guard); 
88
89         // Run msgq while unlocked
90         guard.unlock();
91         while (!block->msg_queue()->empty_p())
92           block->handle_msg(block->msg_queue()->delete_head_nowait());
93       }
94       break;
95
96     default:
97       assert(0);
98     }
99   }
100 }
101
102 gr_tpb_thread_body::~gr_tpb_thread_body()
103 {
104 }