Imported Upstream version 3.2.2
[debian/gnuradio] / mblock / src / lib / mb_worker.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 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 <mb_worker.h>
26 #include <mb_runtime_thread_per_block.h>
27 #include <mblock/exception.h>
28 #include <mblock/mblock.h>
29 #include <mb_gettid.h>
30 #include <mblock/msg_accepter.h>
31 #include <iostream>
32 #include <cstdio>
33 #ifdef HAVE_SCHED_H
34 #include <sched.h>
35 #endif
36
37 #define VERBOSE 0               // define to 0 or 1
38
39
40 static pmt_t s_worker_state_changed = pmt_intern("%worker-state-changed");
41
42
43 mb_worker::mb_worker(mb_runtime_thread_per_block *runtime,
44                      mb_mblock_maker_t maker,
45                      const std::string &instance_name,
46                      pmt_t user_arg)
47   : omni_thread((void *) 0, PRIORITY_NORMAL),
48     d_runtime(runtime), d_maker(maker),
49     d_instance_name(instance_name), d_user_arg(user_arg),
50     d_state_cond(&d_mutex), d_state(TS_UNINITIALIZED),
51     d_why_dead(RIP_NOT_DEAD_YET)
52 {
53 }
54
55 #if 0
56 mb_worker::~mb_worker()
57 {
58 }
59 #endif
60
61 #ifdef HAVE_SCHED_SETAFFINITY
62 static void
63 set_affinity(const std::string &instance_name, const std::string &class_name)
64 {
65   //static int  counter = 0;
66   cpu_set_t     mask;
67   CPU_ZERO(&mask);
68
69   if (0){
70
71     //CPU_SET(counter & 0x1, &mask);
72     //counter++;
73     CPU_SET(0, &mask);
74
75     int r = sched_setaffinity(mb_gettid(), sizeof(mask), &mask);
76     if (r == -1)
77       perror("sched_setaffinity");
78   }
79 }
80 #else
81 static void
82 set_affinity(const std::string &instance_name, const std::string &class_name)
83 {
84 }
85 #endif
86
87 void
88 mb_worker::set_state(worker_state_t state)
89 {
90   {
91     omni_mutex_lock  l2(d_mutex);
92
93     d_state = state;                      // update our state
94     d_state_cond.broadcast();             // Notify everybody who cares...
95   }
96
97   // send msg to runtime, telling it something changed.
98   (*d_runtime->accepter())(s_worker_state_changed, PMT_F, PMT_F, MB_PRI_BEST);
99 }
100
101 void *
102 mb_worker::run_undetached(void *ignored)
103 {
104   // FIXME add pthread_sigmask stuff
105
106   //set_affinity(d_instance_name, d_class_name);
107   set_affinity(d_instance_name, "");
108
109   try {
110     worker_thread_top_level();
111     d_why_dead = RIP_EXIT;
112   }
113   catch (mbe_terminate){
114     d_why_dead = RIP_TERMINATE;
115   }
116   catch (mbe_exit){
117     d_why_dead = RIP_EXIT;
118   }
119   catch (std::logic_error e){
120     if (d_why_dead == RIP_NOT_DEAD_YET)
121       d_why_dead = RIP_UNHANDLED_EXCEPTION;
122
123     std::cerr << "\nmb_worker::run_undetached: unhandled exception:\n";
124     std::cerr << "  " << e.what() << std::endl;
125   }
126   catch (...){
127     if (d_why_dead == RIP_NOT_DEAD_YET)
128       d_why_dead = RIP_UNHANDLED_EXCEPTION;
129   }
130
131   if (VERBOSE)
132     std::cerr << "\nrun_undetached: about to return, d_why_dead = "
133               << d_why_dead << std::endl;
134
135   set_state(TS_DEAD);
136   return 0;
137 }
138
139 void
140 mb_worker::worker_thread_top_level()
141 {
142   if (VERBOSE)
143     std::cerr << "worker_thread_top_level (enter):" << std::endl
144               << "  instance_name: " << d_instance_name << std::endl
145               << "  omnithread id: " << id() << std::endl
146               << "  gettid:        " << mb_gettid() << std::endl
147               << "  getpid:        " << getpid() << std::endl;
148
149   cause_of_death_t pending_cause_of_death = RIP_NOT_DEAD_YET;
150   
151   try {
152     pending_cause_of_death = RIP_CTOR_EXCEPTION;
153     d_mblock = d_maker(d_runtime, d_instance_name, d_user_arg);
154
155     if (VERBOSE)
156       std::cerr << "worker_thread_top_level (post-construction):" << std::endl
157                 << "  instance_name: " << d_instance_name << std::endl;
158
159     pending_cause_of_death = RIP_INIT_EXCEPTION;
160     d_mblock->initial_transition();
161
162     if (VERBOSE)
163       std::cerr << "worker_thread_top_level (post-initial-transition):" << std::endl
164                 << "  instance_name: " << d_instance_name << std::endl;
165
166     set_state(TS_RUNNING);
167
168     pending_cause_of_death = RIP_UNHANDLED_EXCEPTION;
169     d_mblock->main_loop();
170   }
171   catch (...){
172     d_why_dead = pending_cause_of_death;
173     throw;
174   }
175
176   if (VERBOSE)
177     std::cerr << "worker_thread_top_level (exit):" << std::endl
178               << "  instance_name: " << d_instance_name << std::endl;
179 }