Imported Upstream version 3.2.2
[debian/gnuradio] / mblock / src / lib / mb_worker.h
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 #ifndef INCLUDED_MB_WORKER_H
23 #define INCLUDED_MB_WORKER_H
24
25 #include <gnuradio/omnithread.h>
26 #include <mblock/common.h>
27 #include <mblock/class_registry.h>
28
29
30 class mb_worker;
31 //typedef boost::shared_ptr<mb_worker> mb_worker_sptr;
32
33 class mb_runtime_thread_per_block;
34
35 /*!
36  * \brief Worker thread for thread_per_block runtime
37  * \internal
38  */
39 class mb_worker : public omni_thread
40 {
41 public:
42   //! worker thread states
43   enum worker_state_t {
44     TS_UNINITIALIZED,   // new, uninitialized
45     TS_RUNNING,         // normal steady-state condition.
46     TS_DEAD             // thread is dead
47   };
48
49   //! why we're dead
50   enum cause_of_death_t {
51     RIP_NOT_DEAD_YET,           // not dead
52     RIP_EXIT,                   // normal exit
53     RIP_TERMINATE,              // caught terminate exception
54     RIP_CTOR_EXCEPTION,         // constructor raised an exception
55     RIP_INIT_EXCEPTION,         // initial_transition rasised an exception
56     RIP_UNHANDLED_EXCEPTION     // somebody (most likely handle_message) raised an exception
57   };
58
59   /*
60    * Args used by new thread to create mb_mblock
61    */
62   mb_runtime_thread_per_block  *d_runtime;
63   mb_mblock_maker_t             d_maker;
64   std::string                   d_instance_name;
65   pmt_t                         d_user_arg;
66
67   mb_mblock_sptr                d_mblock;       //< holds pointer to created mblock
68
69   /*!
70    * \brief General mutex for all these fields.
71    *
72    * They are accessed by both the main runtime thread and the newly
73    * created thread that runs the mblock's main loop.
74    */
75   omni_mutex                    d_mutex; 
76   omni_condition                d_state_cond;   //< state change notifications
77   worker_state_t                d_state;
78   cause_of_death_t              d_why_dead;
79
80   mb_worker(mb_runtime_thread_per_block *runtime,
81             mb_mblock_maker_t maker,
82             const std::string &instance_name,
83             pmt_t user_arg);
84
85   // ~mb_worker();
86
87
88   /*!
89    * \brief This code runs as the top-level of the new thread
90    */
91   void worker_thread_top_level();
92   
93   /*!
94    * \brief Invokes the top-level of the new thread (name kind of sucks)
95    */
96   void *run_undetached(void *arg);
97
98 private:
99   // Neither d_mutex nor runtime->d_mutex may be held while calling this.
100   // It locks and unlocks them itself.
101   void set_state(worker_state_t state);
102 };
103
104
105
106 #endif /* INCLUDED_MB_WORKER_H */