Statistics
| Branch: | Tag: | Revision:

root / gnuradio-core / src / lib / runtime / gr_scheduler_tpb.cc @ 0408e48c

History | View | Annotate | Download (2.4 kB)

1 2c8ea58e eb
/* -*- c++ -*- */
2 2c8ea58e eb
/*
3 2c8ea58e eb
 * Copyright 2008 Free Software Foundation, Inc.
4 2c8ea58e eb
 * 
5 2c8ea58e eb
 * This file is part of GNU Radio
6 2c8ea58e eb
 * 
7 2c8ea58e eb
 * GNU Radio is free software; you can redistribute it and/or modify
8 2c8ea58e eb
 * it under the terms of the GNU General Public License as published by
9 2c8ea58e eb
 * the Free Software Foundation; either version 3, or (at your option)
10 2c8ea58e eb
 * any later version.
11 2c8ea58e eb
 * 
12 2c8ea58e eb
 * GNU Radio is distributed in the hope that it will be useful,
13 2c8ea58e eb
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 2c8ea58e eb
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 2c8ea58e eb
 * GNU General Public License for more details.
16 2c8ea58e eb
 * 
17 2c8ea58e eb
 * You should have received a copy of the GNU General Public License along
18 2c8ea58e eb
 * with this program; if not, write to the Free Software Foundation, Inc.,
19 2c8ea58e eb
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 2c8ea58e eb
 */
21 2c8ea58e eb
22 2c8ea58e eb
#ifdef HAVE_CONFIG_H
23 2c8ea58e eb
#include <config.h>
24 2c8ea58e eb
#endif
25 2c8ea58e eb
#include <gr_scheduler_tpb.h>
26 2c8ea58e eb
#include <gr_tpb_thread_body.h>
27 2c8ea58e eb
#include <gruel/thread_body_wrapper.h>
28 2c8ea58e eb
#include <sstream>
29 2c8ea58e eb
30 2c8ea58e eb
/*
31 2c8ea58e eb
 * You know, a lambda expression would be sooo much easier...
32 2c8ea58e eb
 */
33 2c8ea58e eb
class tpb_container
34 2c8ea58e eb
{
35 2c8ea58e eb
  gr_block_sptr        d_block;
36 2c8ea58e eb
  
37 2c8ea58e eb
public:
38 2c8ea58e eb
  tpb_container(gr_block_sptr block) : d_block(block) {}
39 2c8ea58e eb
40 2c8ea58e eb
  void operator()()
41 2c8ea58e eb
  {
42 2c8ea58e eb
    gr_tpb_thread_body        body(d_block);
43 2c8ea58e eb
  }
44 2c8ea58e eb
};
45 2c8ea58e eb
46 2c8ea58e eb
47 2c8ea58e eb
gr_scheduler_sptr
48 2c8ea58e eb
gr_scheduler_tpb::make(gr_flat_flowgraph_sptr ffg)
49 2c8ea58e eb
{
50 2c8ea58e eb
  return gr_scheduler_sptr(new gr_scheduler_tpb(ffg));
51 2c8ea58e eb
}
52 2c8ea58e eb
53 2c8ea58e eb
gr_scheduler_tpb::gr_scheduler_tpb(gr_flat_flowgraph_sptr ffg)
54 2c8ea58e eb
  : gr_scheduler(ffg)
55 2c8ea58e eb
{
56 2c8ea58e eb
  // Get a topologically sorted vector of all the blocks in use.
57 2c8ea58e eb
  // Being topologically sorted probably isn't going to matter, but
58 2c8ea58e eb
  // there's a non-zero chance it might help...
59 2c8ea58e eb
60 2c8ea58e eb
  gr_basic_block_vector_t used_blocks = ffg->calc_used_blocks();
61 2c8ea58e eb
  used_blocks = ffg->topological_sort(used_blocks);
62 2c8ea58e eb
  gr_block_vector_t blocks = gr_flat_flowgraph::make_block_vector(used_blocks);
63 2c8ea58e eb
64 2c8ea58e eb
  // Ensure that the done flag is clear on all blocks
65 2c8ea58e eb
66 2c8ea58e eb
  for (size_t i = 0; i < blocks.size(); i++){
67 2c8ea58e eb
    blocks[i]->detail()->set_done(false);
68 2c8ea58e eb
  }
69 2c8ea58e eb
70 2c8ea58e eb
  // Fire off a thead for each block
71 2c8ea58e eb
72 2c8ea58e eb
  for (size_t i = 0; i < blocks.size(); i++){
73 2c8ea58e eb
    std::stringstream name;
74 2c8ea58e eb
    name << "thread-per-block[" << i << "]: " << blocks[i];
75 2c8ea58e eb
    d_threads.create_thread(
76 2c8ea58e eb
      gruel::thread_body_wrapper<tpb_container>(tpb_container(blocks[i]), name.str()));
77 2c8ea58e eb
  }
78 2c8ea58e eb
}
79 2c8ea58e eb
80 2c8ea58e eb
gr_scheduler_tpb::~gr_scheduler_tpb()
81 2c8ea58e eb
{
82 2c8ea58e eb
  stop();
83 2c8ea58e eb
}
84 2c8ea58e eb
85 2c8ea58e eb
void
86 2c8ea58e eb
gr_scheduler_tpb::stop()
87 2c8ea58e eb
{
88 2c8ea58e eb
  d_threads.interrupt_all();
89 2c8ea58e eb
}
90 2c8ea58e eb
91 2c8ea58e eb
void
92 2c8ea58e eb
gr_scheduler_tpb::wait()
93 2c8ea58e eb
{
94 2c8ea58e eb
  d_threads.join_all();
95 2c8ea58e eb
}