00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "C_TaskControl.hpp"
00021 #include "Utils.hpp"
00022 #include "GeneratorTrace.hpp"
00023
00024 C_TaskControl::C_TaskControl() {
00025 GEN_DEBUG(0, "C_TaskControl::C_TaskControl() start");
00026 M_state = C_TaskControl::E_STATE_UNKNOWN ;
00027 m_end_executed = false ;
00028 GEN_DEBUG(0, "C_TaskControl::C_TaskControl() end");
00029 }
00030
00031 C_TaskControl::~C_TaskControl() {
00032 GEN_DEBUG(0, "C_TaskControl::~C_TaskControl() start");
00033 M_state = C_TaskControl::E_STATE_UNKNOWN ;
00034 m_end_executed = false ;
00035 GEN_DEBUG(0, "C_TaskControl::~C_TaskControl() end");
00036 }
00037
00038 T_GeneratorError C_TaskControl::init () {
00039
00040 T_GeneratorError L_error_code ;
00041 GEN_DEBUG(0, "C_TaskControl::init() start");
00042 L_error_code = InitProcedure();
00043 if (L_error_code == E_GEN_NO_ERROR) {
00044 M_state = C_TaskControl::E_STATE_INIT ;
00045 }
00046 GEN_DEBUG(0, "C_TaskControl::init() end");
00047 return (L_error_code);
00048 }
00049
00050 T_GeneratorError C_TaskControl::run_task_once () {
00051
00052 T_GeneratorError L_error_code = E_GEN_NO_ERROR;
00053
00054 GEN_DEBUG(0, "C_TaskControl::run_task_once() start State:"
00055 << M_state);
00056
00057 switch (M_state) {
00058
00059 case C_TaskControl::E_STATE_INIT:
00060 M_state = C_TaskControl::E_STATE_RUNNING ;
00061 L_error_code = TaskProcedure () ;
00062 break ;
00063
00064 case C_TaskControl::E_STATE_RUNNING:
00065 case C_TaskControl::E_STATE_STOPPING:
00066 L_error_code = TaskProcedure () ;
00067 break ;
00068
00069 case C_TaskControl::E_STATE_STOPPED:
00070 if (m_end_executed == false) {
00071 L_error_code = EndProcedure() ;
00072 m_end_executed = true ;
00073 }
00074 break ;
00075
00076 default:
00077 GEN_ERROR (E_GEN_FATAL_ERROR, "Incorrect state " << M_state);
00078 L_error_code = E_GEN_FATAL_ERROR ;
00079 break ;
00080
00081 }
00082
00083 GEN_DEBUG(0, "C_TaskControl::run_task_once() end State "
00084 << M_state);
00085
00086 return (L_error_code);
00087 }
00088
00089 T_GeneratorError C_TaskControl::run_all_once () {
00090
00091 T_GeneratorError L_error_code = E_GEN_NO_ERROR;
00092
00093 GEN_DEBUG(0, "C_TaskControl::run_all_once() start");
00094 if (M_state == C_TaskControl::E_STATE_INIT) {
00095 M_state = C_TaskControl::E_STATE_RUNNING ;
00096 L_error_code = TaskProcedure () ;
00097 M_state = C_TaskControl::E_STATE_STOPPED ;
00098 L_error_code = EndProcedure() ;
00099 } else {
00100 GEN_ERROR (E_GEN_FATAL_ERROR, "Incorrect C_Generator state " << M_state);
00101 L_error_code = E_GEN_FATAL_ERROR ;
00102 }
00103 return (L_error_code);
00104 GEN_DEBUG(0, "C_TaskControl::run_all_once() end");
00105 }
00106
00107 T_GeneratorError C_TaskControl::run () {
00108
00109 T_GeneratorError L_error_code = E_GEN_NO_ERROR;
00110
00111 GEN_DEBUG(0, "C_TaskControl::run() start");
00112 if (M_state == C_TaskControl::E_STATE_INIT) {
00113 M_state = C_TaskControl::E_STATE_RUNNING ;
00114 while (M_state != C_TaskControl::E_STATE_STOPPED) {
00115 L_error_code = TaskProcedure () ;
00116 }
00117 if (L_error_code == E_GEN_NO_ERROR) {
00118 L_error_code = EndProcedure() ;
00119 }
00120 } else {
00121 GEN_ERROR (E_GEN_FATAL_ERROR, "Incorrect C_Generator state " << M_state);
00122 L_error_code = E_GEN_FATAL_ERROR ;
00123 }
00124 return (L_error_code);
00125 GEN_DEBUG(0, "C_TaskControl::run() end");
00126 }
00127
00128 void C_TaskControl::stop () {
00129
00130
00131
00132 GEN_DEBUG(0, "C_TaskControl::stop() start");
00133
00134 switch (M_state) {
00135 case E_STATE_RUNNING:
00136 GEN_DEBUG(0, "C_TaskControl::stop() RUNNING=>STOPPING");
00137 M_state = C_TaskControl::E_STATE_STOPPING ;
00138 (void) StoppingProcedure();
00139 break ;
00140 case E_STATE_STOPPING:
00141 GEN_DEBUG(0, "C_TaskControl::stop() STOPPING");
00142 (void) ForcedStoppingProcedure ();
00143 break ;
00144 default:
00145 break ;
00146 }
00147 GEN_DEBUG(0, "C_TaskControl::stop() end");
00148
00149 }
00150
00151 C_TaskControl::T_State C_TaskControl::get_state() {
00152 GEN_DEBUG(1, "C_TaskControl::get_state()");
00153 return (M_state);
00154 }
00155
00156 iostream_output& operator<<(iostream_output& P_ostream,
00157 C_TaskControl::T_State& P_state) {
00158 const char* c_string_state [] = {
00159 "E_STATE_UNKNOWN",
00160 "E_STATE_INIT",
00161 "E_STATE_RUNNING",
00162 "E_STATE_STOPPING",
00163 "E_STATE_STOPPED"
00164 } ;
00165
00166 P_ostream << c_string_state[P_state] ;
00167
00168 return (P_ostream);
00169 }
00170
00171
00172 static void* call_run (void* P_data) {
00173
00174 C_TaskControl *L_taskControl ;
00175 T_GeneratorError L_errorCode ;
00176
00177 GEN_DEBUG(0, "call_run() start");
00178
00179 L_taskControl = (C_TaskControl*) P_data ;
00180
00181 L_errorCode = L_taskControl -> run () ;
00182
00183 pthread_exit ((void*)L_errorCode) ;
00184
00185 GEN_DEBUG(0, "call_run() end");
00186
00187 return (NULL) ;
00188 }
00189
00190
00191 pthread_t* start_thread_control (C_TaskControl *P_taskControl) {
00192
00193 pthread_t *L_thread ;
00194 int L_return ;
00195
00196 GEN_DEBUG(0, "start_thread_control() start");
00197 ALLOC_VAR(L_thread, pthread_t*, sizeof(pthread_t));
00198
00199 L_return = pthread_create (L_thread,
00200 NULL,
00201 call_run,
00202 (void*) P_taskControl) ;
00203
00204 if (L_return != 0) {
00205 GEN_FATAL(0, "pthread_create() error");
00206 }
00207
00208 GEN_DEBUG(0, "start_controller() end");
00209 return (L_thread);
00210 }
00211
00212 void wait_thread_control_end (pthread_t *P_thread) {
00213
00214 void *L_return ;
00215 pthread_join (*P_thread, &L_return);
00216
00217
00218 FREE_VAR(P_thread);
00219 }
00220