Thread.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //
00003 // $Id$
00004 //
00005 // Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno    
00006 //
00007 // This file is part of MIP.
00008 //
00009 // MIP is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // MIP is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
00023 //
00024 // ----------------------------------------------------------------------------
00025 
00026 
00030 
00031 #ifndef __THREAD_H_
00032 #define __THREAD_H_
00033 
00034 #ifdef MIP_HOST_APPLE
00035 #include <applePatch.h>
00036 #endif
00037 
00038 #include <stdio.h>
00039 #include <math.h>
00040 #include <vector>
00041 #include <iostream>
00042 #include <string>
00043 #include <sstream>
00044 #include <sys/time.h>
00045 #include <sys/select.h>
00046 #include <assert.h>
00047 #include <pthread.h>
00048 
00049 
00050 #include <Types.h>
00051 #include <Time.h>
00052 
00056 
00058 /* @{ */
00059 
00060 
00061 using namespace std;
00062 
00077 
00078 namespace MipBaselib {
00079 class Thread{
00080  protected:
00081   pthread_t _tid; 
00082   bool _live;  
00083   bool _doWork; 
00084   bool _stopWork;
00085   bool _dirty;  
00088 //  TimeUtilities _tu; /**< time utilitities for the usleep */
00089   Time _sleepTime;  
00090   static const int THREAD_SLEEP_USECS = 100000;
00091   
00092   void* _workObject;   
00093   void (*_work)(void*); 
00094   void (*_workClean)(void*); 
00096  public:
00098   Thread();
00099   
00101    ~Thread();
00104   void setWorkObject(void* wO){
00105    _workObject = wO;
00106   }
00109   void setWork(void (*w)(void*)){
00110    _work = w;
00111   }
00115   void setWorkClean(void (*wC)(void*)){
00116    _workClean = wC;
00117   }
00118   
00121   void setSleepTime(Time sleepT){
00122    _sleepTime = sleepT;
00123   }
00124 
00125   
00130   void work(){
00131    if(_workObject!=0){
00132     if(_work!=0){
00133      (*_work)(_workObject);
00134     }
00135    }
00136   }
00137   
00144   void workClean(){
00145    if(_workObject!=0){
00146     if(_workClean!=0){
00147      (*_workClean)(_workObject);
00148     }
00149    }
00150   }
00151   
00154   void startWork(){
00155    _doWork = true;
00156   }
00157   
00160   void stopWork(){
00161    _doWork  = false;
00162    _stopWork = true;
00163   }
00164   
00167   void liveCycle();
00168 };
00169 
00170 
00171 
00180 class EnhancedMutEx{
00181  private:
00182   pthread_mutex_t _mutex;    
00183   pthread_t    _enabledTid; 
00184 
00188   int _trylock() {
00189    return pthread_mutex_trylock(&_mutex);
00190   }
00194   int _unlock(){
00195    return pthread_mutex_unlock(&_mutex);
00196   }
00197   
00199   bool _threadAllowed();
00200   
00203   bool _threadAllowedPrompt();
00204   
00206   void _threadEnable();
00207   
00208  public:
00210   EnhancedMutEx();
00211   
00215   bool askExclusiveAccess(Time timeout);
00216   
00219   void leaveExclusiveAccess();
00220   
00224   bool allowed(void){
00225    return _threadAllowedPrompt();
00226   }
00227   
00229   string print();
00230 };
00231 
00235 
00236 }; // end of namespace
00237 
00238 #endif
00239 
00240 /* @} */
00241 
00242 
00243 

Generated on Mon Feb 20 07:01:07 2017 for MIP by  doxygen 1.5.6