3 * Copyright 2007 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
25 #include <mb_worker.h>
26 #include <mb_runtime_thread_per_block.h>
27 #include <mb_exception.h>
28 #include <mb_mblock.h>
29 #include <mb_gettid.h>
30 #include <mb_msg_accepter.h>
36 #define VERBOSE 0 // define to 0 or 1
39 static pmt_t s_worker_state_changed = pmt_intern("%worker-state-changed");
42 mb_worker::mb_worker(mb_runtime_thread_per_block *runtime,
43 mb_mblock_maker_t maker,
44 const std::string &instance_name,
46 : omni_thread((void *) 0, PRIORITY_NORMAL),
47 d_runtime(runtime), d_maker(maker),
48 d_instance_name(instance_name), d_user_arg(user_arg),
49 d_state_cond(&d_mutex), d_state(TS_UNINITIALIZED),
50 d_why_dead(RIP_NOT_DEAD_YET)
55 mb_worker::~mb_worker()
60 #ifdef HAVE_SCHED_SETAFFINITY
62 set_affinity(const std::string &instance_name, const std::string &class_name)
64 //static int counter = 0;
70 //CPU_SET(counter & 0x1, &mask);
74 int r = sched_setaffinity(mb_gettid(), sizeof(mask), &mask);
76 perror("sched_setaffinity");
81 set_affinity(const std::string &instance_name, const std::string &class_name)
87 mb_worker::set_state(worker_state_t state)
90 omni_mutex_lock l2(d_mutex);
92 d_state = state; // update our state
93 d_state_cond.broadcast(); // Notify everybody who cares...
96 // send msg to runtime, telling it something changed.
97 (*d_runtime->accepter())(s_worker_state_changed, PMT_F, PMT_F, MB_PRI_BEST);
101 mb_worker::run_undetached(void *ignored)
103 // FIXME add pthread_sigmask stuff
105 //set_affinity(d_instance_name, d_class_name);
106 set_affinity(d_instance_name, "");
109 worker_thread_top_level();
110 d_why_dead = RIP_EXIT;
112 catch (mbe_terminate){
113 d_why_dead = RIP_TERMINATE;
116 d_why_dead = RIP_EXIT;
118 catch (std::logic_error e){
119 if (d_why_dead == RIP_NOT_DEAD_YET)
120 d_why_dead = RIP_UNHANDLED_EXCEPTION;
122 std::cerr << "\nmb_worker::run_undetached: unhandled exception:\n";
123 std::cerr << " " << e.what() << std::endl;
126 if (d_why_dead == RIP_NOT_DEAD_YET)
127 d_why_dead = RIP_UNHANDLED_EXCEPTION;
131 std::cerr << "\nrun_undetached: about to return, d_why_dead = "
132 << d_why_dead << std::endl;
139 mb_worker::worker_thread_top_level()
142 std::cerr << "worker_thread_top_level (enter):" << std::endl
143 << " instance_name: " << d_instance_name << std::endl
144 << " omnithread id: " << id() << std::endl
145 << " gettid: " << mb_gettid() << std::endl
146 << " getpid: " << getpid() << std::endl;
148 cause_of_death_t pending_cause_of_death = RIP_NOT_DEAD_YET;
151 pending_cause_of_death = RIP_CTOR_EXCEPTION;
152 d_mblock = d_maker(d_runtime, d_instance_name, d_user_arg);
155 std::cerr << "worker_thread_top_level (post-construction):" << std::endl
156 << " instance_name: " << d_instance_name << std::endl;
158 pending_cause_of_death = RIP_INIT_EXCEPTION;
159 d_mblock->initial_transition();
162 std::cerr << "worker_thread_top_level (post-initial-transition):" << std::endl
163 << " instance_name: " << d_instance_name << std::endl;
165 set_state(TS_RUNNING);
167 pending_cause_of_death = RIP_UNHANDLED_EXCEPTION;
168 d_mblock->main_loop();
171 d_why_dead = pending_cause_of_death;
176 std::cerr << "worker_thread_top_level (exit):" << std::endl
177 << " instance_name: " << d_instance_name << std::endl;