Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / scheduler.py
1 #
2 # Copyright 2004 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
20
21
22 from gnuradio.gr.exceptions import *
23 from gnuradio_swig_python import single_threaded_scheduler, sts_pyrun
24 import gr_threading as _threading
25 #import threading as _threading
26
27 class scheduler_thread(_threading.Thread):
28     def __init__(self, sts):
29         _threading.Thread.__init__(self)
30         self.sts = sts
31     def run(self):
32         # Invoke the single threaded scheduler's run method
33         #
34         # Note that we're in a new thread, and that sts_pyrun
35         # releases the global interpreter lock.  This has the
36         # effect of evaluating the graph in parallel to the
37         # main line control code.
38         sts_pyrun(self.sts)  
39         self.sts = None
40         
41 class scheduler(object):
42     def __init__(self, fg):
43         graphs = fg.partition_graph(fg.blocks)
44         # print "@@@ # graphs = %d" % (len(graphs))
45         
46         self.state = []
47         
48         for g in graphs:
49             list_of_blocks = [x.block() for x in g]
50             sts = single_threaded_scheduler(list_of_blocks)
51             thread = scheduler_thread(sts)
52             thread.setDaemon(1)
53             self.state.append((sts, thread))
54
55     def start(self):
56         for (sts, thread) in self.state:
57             thread.start()
58
59     def stop(self):
60         for (sts, thread) in self.state:
61             sts.stop()
62         self.wait()
63
64     def wait(self):
65         for (sts, thread) in self.state:
66             timeout = 0.100
67             while True:
68                 thread.join(timeout)
69                 if not thread.isAlive():
70                     break