Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_scheduler_tpb.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 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_scheduler_tpb.h>
26 #include <gr_tpb_thread_body.h>
27 #include <gruel/thread_body_wrapper.h>
28 #include <sstream>
29
30 /*
31  * You know, a lambda expression would be sooo much easier...
32  */
33 class tpb_container
34 {
35   gr_block_sptr d_block;
36   
37 public:
38   tpb_container(gr_block_sptr block) : d_block(block) {}
39
40   void operator()()
41   {
42     gr_tpb_thread_body  body(d_block);
43   }
44 };
45
46
47 gr_scheduler_sptr
48 gr_scheduler_tpb::make(gr_flat_flowgraph_sptr ffg)
49 {
50   return gr_scheduler_sptr(new gr_scheduler_tpb(ffg));
51 }
52
53 gr_scheduler_tpb::gr_scheduler_tpb(gr_flat_flowgraph_sptr ffg)
54   : gr_scheduler(ffg)
55 {
56   // Get a topologically sorted vector of all the blocks in use.
57   // Being topologically sorted probably isn't going to matter, but
58   // there's a non-zero chance it might help...
59
60   gr_basic_block_vector_t used_blocks = ffg->calc_used_blocks();
61   used_blocks = ffg->topological_sort(used_blocks);
62   gr_block_vector_t blocks = gr_flat_flowgraph::make_block_vector(used_blocks);
63
64   // Ensure that the done flag is clear on all blocks
65
66   for (size_t i = 0; i < blocks.size(); i++){
67     blocks[i]->detail()->set_done(false);
68   }
69
70   // Fire off a thead for each block
71
72   for (size_t i = 0; i < blocks.size(); i++){
73     std::stringstream name;
74     name << "thread-per-block[" << i << "]: " << blocks[i];
75     d_threads.create_thread(
76       gruel::thread_body_wrapper<tpb_container>(tpb_container(blocks[i]), name.str()));
77   }
78 }
79
80 gr_scheduler_tpb::~gr_scheduler_tpb()
81 {
82   stop();
83 }
84
85 void
86 gr_scheduler_tpb::stop()
87 {
88   d_threads.interrupt_all();
89 }
90
91 void
92 gr_scheduler_tpb::wait()
93 {
94   d_threads.join_all();
95 }