summaryrefslogtreecommitdiff
path: root/mblock/src/lib/mb_worker.h
blob: 9831de926068b4e88afb60b971ce0ba638b6da69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* -*- c++ -*- */
/*
 * Copyright 2007,2008 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef INCLUDED_MB_WORKER_H
#define INCLUDED_MB_WORKER_H

#include <gnuradio/omnithread.h>
#include <mblock/common.h>
#include <mblock/class_registry.h>


class mb_worker;
//typedef boost::shared_ptr<mb_worker> mb_worker_sptr;

class mb_runtime_thread_per_block;

/*!
 * \brief Worker thread for thread_per_block runtime
 * \implementation
 */
class mb_worker : public omni_thread
{
public:
  //! worker thread states
  enum worker_state_t {
    TS_UNINITIALIZED,	// new, uninitialized
    TS_RUNNING,		// normal steady-state condition.
    TS_DEAD		// thread is dead
  };

  //! why we're dead
  enum cause_of_death_t {
    RIP_NOT_DEAD_YET,		// not dead
    RIP_EXIT,			// normal exit
    RIP_TERMINATE,		// caught terminate exception
    RIP_CTOR_EXCEPTION,		// constructor raised an exception
    RIP_INIT_EXCEPTION,		// initial_transition rasised an exception
    RIP_UNHANDLED_EXCEPTION	// somebody (most likely handle_message) raised an exception
  };

  /*
   * Args used by new thread to create mb_mblock
   */
  mb_runtime_thread_per_block  *d_runtime;
  mb_mblock_maker_t		d_maker;
  std::string			d_instance_name;
  pmt_t				d_user_arg;

  mb_mblock_sptr		d_mblock;	//< holds pointer to created mblock

  /*!
   * \brief General mutex for all these fields.
   *
   * They are accessed by both the main runtime thread and the newly
   * created thread that runs the mblock's main loop.
   */
  omni_mutex			d_mutex; 
  omni_condition		d_state_cond;	//< state change notifications
  worker_state_t		d_state;
  cause_of_death_t		d_why_dead;

  mb_worker(mb_runtime_thread_per_block *runtime,
	    mb_mblock_maker_t maker,
	    const std::string &instance_name,
	    pmt_t user_arg);

  // ~mb_worker();


  /*!
   * \brief This code runs as the top-level of the new thread
   */
  void worker_thread_top_level();
  
  /*!
   * \brief Invokes the top-level of the new thread (name kind of sucks)
   */
  void *run_undetached(void *arg);

private:
  // Neither d_mutex nor runtime->d_mutex may be held while calling this.
  // It locks and unlocks them itself.
  void set_state(worker_state_t state);
};



#endif /* INCLUDED_MB_WORKER_H */