Project stdui: "Simple Type Dialog User-Interface"

|
Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ccontrol.cc

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Serge Lussier   *
00003  *   serge.lussier@videotron.ca   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
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    //lock();
00089    int CV_ERR =0;
00090    while( ! _cond ){
00091       lock();
00092       CV_ERR = pthread_cond_wait( _cv, mutexhandle() );
00094    }
00095    //unlock(); // pas besoin de debloquer le mutex...
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    //lock();
00115    pthread_cond_signal(_cv );
00116    //unlock();
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:  doxygen 1.4.0
About the author | Site started with trial version of DreamWeaver MX 2004 | Finalizing with  | Contact | ©2005, Serge Lussier ( Bretzel )