plotter: require double buffering
[debian/gnuradio] / gnuradio-core / src / lib / runtime / gr_top_block_impl_sts.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,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
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gr_top_block.h>
28 #include <gr_top_block_impl_sts.h>
29 #include <gr_flat_flowgraph.h>
30 #include <gr_scheduler_thread.h>
31 #include <gr_local_sighandler.h>
32
33 #include <stdexcept>
34 #include <iostream>
35 #include <string.h>
36 #include <unistd.h>
37
38 #define GR_TOP_BLOCK_IMPL_STS_DEBUG 0
39
40 static gr_top_block_impl *s_impl = 0;
41
42
43 // FIXME: This prevents using more than one gr_top_block instance
44
45 static void 
46 runtime_sigint_handler(int signum)
47 {
48   if (GR_TOP_BLOCK_IMPL_STS_DEBUG){
49     char *msg = "SIGINT received, calling stop()\n";
50     ::write(1, msg, strlen(msg));       // write is OK to call from signal handler
51   }
52
53   if (s_impl)
54     s_impl->stop();
55 }
56
57 // ----------------------------------------------------------------
58
59 gr_top_block_impl_sts::gr_top_block_impl_sts(gr_top_block *owner) 
60   : gr_top_block_impl(owner)
61 {
62   if (s_impl)
63     throw std::logic_error("gr_top_block_impl_sts: multiple simultaneous gr_top_blocks not allowed");
64
65   s_impl = this;
66 }
67
68 gr_top_block_impl_sts::~gr_top_block_impl_sts()
69 {
70   s_impl = 0; // don't call delete we don't own these
71 }
72
73 void
74 gr_top_block_impl_sts::start_threads()
75 {
76   if (GR_TOP_BLOCK_IMPL_STS_DEBUG)
77     std::cout << "start_threads: entered" << std::endl;
78
79   d_graphs = d_ffg->partition();
80   for (std::vector<gr_basic_block_vector_t>::iterator p = d_graphs.begin();
81        p != d_graphs.end(); p++) {
82     gr_scheduler_thread *thread = new gr_scheduler_thread(make_gr_block_vector(*p));
83     d_threads.push_back(thread);
84     if (GR_TOP_BLOCK_IMPL_STS_DEBUG)
85       std::cout << "start_threads: starting " << thread << std::endl;
86     thread->start();
87   }
88 }
89
90 /*
91  * N.B. as currently implemented, it is possible that this may be
92  * invoked by the SIGINT handler which is fragile as hell...
93  */
94 void
95 gr_top_block_impl_sts::stop()
96 {
97   if (GR_TOP_BLOCK_IMPL_STS_DEBUG){
98     char *msg = "stop: entered\n";
99     ::write(1, msg, strlen(msg));
100   }
101
102   for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
103     if (*p)
104       (*p)->stop();
105   }
106 }
107
108 void
109 gr_top_block_impl_sts::wait()
110 {
111   if (GR_TOP_BLOCK_IMPL_STS_DEBUG)
112     std::cout << "wait: entered" << std::endl;
113
114   void *dummy_status; // don't ever dereference this
115   gr_local_sighandler sigint(SIGINT, runtime_sigint_handler);
116
117   for (gr_scheduler_thread_viter_t p = d_threads.begin(); p != d_threads.end(); p++) {
118     if (GR_TOP_BLOCK_IMPL_STS_DEBUG)
119       std::cout << "wait: joining thread " << (*p) << std::endl;
120     (*p)->join(&dummy_status); // omnithreads will self-delete, so pointer is now dead
121     (*p) = 0; // FIXME: switch to stl::list and actually remove from container
122     if (GR_TOP_BLOCK_IMPL_STS_DEBUG)
123       std::cout << "wait: join returned" << std::endl;
124   }
125
126   d_threads.clear();
127   d_running = false;
128 }