Imported Upstream version 3.2.2
[debian/gnuradio] / mblock / src / lib / mb_runtime_thread_per_block.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 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 #ifndef INCLUDED_MB_RUNTIME_THREAD_PER_BLOCK_H
22 #define INCLUDED_MB_RUNTIME_THREAD_PER_BLOCK_H
23
24 #include <mb_runtime_base.h>
25 #include <mb_worker.h>
26 #include <mblock/msg_queue.h>
27 #include <mb_timer_queue.h>
28
29 /*!
30  * \brief Concrete runtime that uses a thread per mblock
31  * \internal
32  *
33  * These are all implementation details.
34  */
35 class mb_runtime_thread_per_block : public mb_runtime_base
36 {
37 public:
38   omni_mutex                  d_workers_mutex;  // hold while manipulating d_workers
39   std::vector<mb_worker*>     d_workers;
40   bool                        d_shutdown_in_progress;
41   pmt_t                       d_shutdown_result;
42   mb_msg_queue                d_msgq;
43   mb_timer_queue              d_timer_queue;
44
45   typedef std::vector<mb_worker*>::iterator  worker_iter_t;
46
47   mb_runtime_thread_per_block();
48   ~mb_runtime_thread_per_block();
49
50   bool run(const std::string &instance_name,
51            const std::string &class_name,
52            pmt_t user_arg,
53            pmt_t *result);
54
55   void request_shutdown(pmt_t result);
56
57 protected:
58   mb_mblock_sptr
59   create_component(const std::string &instance_name,
60                    const std::string &class_name,
61                    pmt_t user_arg);
62
63   pmt_t
64   schedule_one_shot_timeout(const mb_time &abs_time, pmt_t user_data,
65                             mb_msg_accepter_sptr accepter);
66
67   pmt_t
68   schedule_periodic_timeout(const mb_time &first_abs_time,
69                             const mb_time &delta_time,
70                             pmt_t user_data,
71                             mb_msg_accepter_sptr accepter);
72   void
73   cancel_timeout(pmt_t handle);
74
75 private:
76   void reap_dead_workers();
77   void run_loop();
78
79   void send_all_sys_msg(pmt_t signal, pmt_t data = PMT_F,
80                         pmt_t metadata = PMT_F,
81                         mb_pri_t priority = MB_PRI_BEST);
82 };
83
84 #endif /* INCLUDED_MB_RUNTIME_THREAD_PER_BLOCK_H */