Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_scheduler_sts.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_sts.h>
26 #include <gr_single_threaded_scheduler.h>
27 #include <gruel/thread_body_wrapper.h>
28
29 class sts_container
30 {
31   gr_block_vector_t     d_blocks;
32   
33 public:
34
35   sts_container(gr_block_vector_t blocks)
36     : d_blocks(blocks) {}
37
38   void operator()()
39   {
40     gr_make_single_threaded_scheduler(d_blocks)->run();
41   }
42 };
43
44
45 gr_scheduler_sptr
46 gr_scheduler_sts::make(gr_flat_flowgraph_sptr ffg)
47 {
48   return gr_scheduler_sptr(new gr_scheduler_sts(ffg));
49 }
50
51 gr_scheduler_sts::gr_scheduler_sts(gr_flat_flowgraph_sptr ffg)
52   : gr_scheduler(ffg)
53 {
54   // Split the flattened flow graph into discrete partitions, each
55   // of which is topologically sorted.
56
57   std::vector<gr_basic_block_vector_t> graphs = ffg->partition();
58
59   // For each partition, create a thread to evaluate it using
60   // an instance of the gr_single_threaded_scheduler
61
62   for (std::vector<gr_basic_block_vector_t>::iterator p = graphs.begin();
63        p != graphs.end(); p++) {
64
65     gr_block_vector_t blocks = gr_flat_flowgraph::make_block_vector(*p);
66     d_threads.create_thread(
67         gruel::thread_body_wrapper<sts_container>(sts_container(blocks),
68                                                   "single-threaded-scheduler"));
69   }
70 }
71
72 gr_scheduler_sts::~gr_scheduler_sts()
73 {
74   stop();
75 }
76
77 void
78 gr_scheduler_sts::stop()
79 {
80   d_threads.interrupt_all();
81 }
82
83 void
84 gr_scheduler_sts::wait()
85 {
86   d_threads.join_all();
87 }