ccontrol.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <ccontrol.hxx>
00021 #include <iostream>
00022
00023 namespace control {
00024
00025
00026
00032 ccontrol::ccontrol():cthread(), cmutex(),_cond(false)
00033 {
00034 }
00040 ccontrol::ccontrol(const std::string& nameid, const control::ccontrol::worker_delegate& worker ): cmutex(),
00041 cthread(nameid, sigc::mem_fun(this, &ccontrol::controller_loop)){
00042 proceed_.connect( worker );
00043 _cond=false;
00044 }
00045
00052 ccontrol::~ccontrol()
00053 {
00054 std::cerr << __PRETTY_FUNCTION__ << std::endl << '{' << std::endl << "." << std::endl;
00055 std::cerr << name() << std::endl;
00056 proceed_.clear();
00057 std::cerr << "releasing condition variable..." << std::endl;
00058 release();
00059 std::cerr << '}' << std::endl;
00060 }
00061
00062
00086 int ccontrol::wait()
00087 {
00088
00089 int CV_ERR =0;
00090 while( ! _cond ){
00091 lock();
00092 CV_ERR = pthread_cond_wait( _cv, mutexhandle() );
00094 }
00095
00096 if( _state == control::proceed ) proceed_();
00097 if( _state == control::terminate){
00098 release();
00099 return control::terminate;
00100 }
00101
00102 _cond = false;
00103 return state();
00104 }
00105
00110 void ccontrol::signal()
00111 {
00112 if( _cond ) return;
00113 _cond = true;
00114
00115 pthread_cond_signal(_cv );
00116
00117 }
00118
00122 void ccontrol::terminate()
00123 {
00124 _state = ::control::terminate;
00125 if(!_cond ){
00126 _cond=true;
00127 ccontrol::signal();
00128 }
00129
00130 }
00131
00132
00138 bool ccontrol::start()
00139 {
00140 std::cerr << __PRETTY_FUNCTION__ << std::endl << '{' << std::endl << "." << std::endl;
00141 std::cerr << name() << std::endl;
00142 _state = control::proceed;
00143 std::cerr << std::endl << '}' << std::endl;
00144 return run();
00145 }
00146
00147
00152 bool ccontrol::init()
00153 {
00154 cmutex::init();
00155 _cv = new pthread_cond_t;
00156 pthread_cond_init(_cv, 0l);
00157
00158 return true;
00159
00160 }
00161
00162
00166 void ccontrol::release()
00167 {
00168 std::cerr << __PRETTY_FUNCTION__ << std::endl << '{' << std::endl << "." << std::endl;
00169 std::cerr << name() << std::endl;
00170 if(_mutex) unlock();
00171 if(_cv) {
00172 int ER = pthread_cond_destroy(_cv);
00173 if(ER){
00174 std::cerr << " -- error trying to release condition variable..." << std::endl;
00175 }
00176 delete _cv;
00177 _cv=0l;
00178 }
00179 if(_mutex){
00180 delete _mutex;
00181 _mutex = 0l;
00182 }
00183 std::cerr << "terminating inner thread instance..." << std::endl;
00184 cthread::exit();
00185 std::cerr << std::endl << '}' << std::endl;
00186 }
00187
00191 bool ccontrol::controller_loop()
00192 {
00193 do ; while(this->wait() != control::terminate);
00194 return control::terminate == _state;
00195 }
00196
00197
00198
00199 };
00200
00201
00202
API Documentation Generated by:
1.4.0
About the author |

Site started with trial version of DreamWeaver MX 2004 |
Finalizing with

|
Contact | ©2005, Serge Lussier ( Bretzel )