Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TGo4ThreadManager.cxx

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "TGo4ThreadManager.h"
00017 
00018 #include "Riostream.h"
00019 
00020 #include "TApplication.h"
00021 #include "TSystem.h"
00022 #include "TCondition.h"
00023 #include "snprintf.h"
00024 
00025 #include "TGo4Log.h"
00026 #include "TGo4ThreadHandler.h"
00027 #include "TGo4AppControlTimer.h"
00028 
00029 const Long_t TGo4ThreadManager::fglTIMERPERIOD = 200; // 2000
00030 
00031 TGo4ThreadManager::TGo4ThreadManager(const TGo4ThreadManager &right)
00032   :TNamed(right),
00033    fbInitDone(kFALSE),
00034    fbTerminating(kFALSE),
00035    fbTerminateApplication(kFALSE)
00036 {
00037    TRACE((15,"TGo4ThreadManager::TGo4ThreadManager copy ctor",__LINE__, __FILE__));
00038    fxWorkHandler=right.fxWorkHandler;
00039    fxBlocker = right.fxBlocker;
00040 }
00041 
00042 TGo4ThreadManager::TGo4ThreadManager (const char* name, Bool_t blockingmode, Bool_t autostart, Bool_t autocreate)
00043    :TNamed(name,"This is a TGo4ThreadManager"),
00044       fbInitDone(kFALSE),
00045       fbTerminating(kFALSE),
00046       fbTerminateApplication(kFALSE)
00047 {
00048    TRACE((15,"TGo4ThreadManager::TGo4ThreadManager (const char*, Bool_t, Bool_t, Bool_t) constructor",__LINE__, __FILE__));
00049 
00050    TString myname=GetName();
00051    myname+="-";
00052    myname+=gSystem->HostName(); // add hostname
00053    myname+="-";
00054    myname+=gSystem->GetPid(); // add pid to threadmanager name
00055    SetName(myname.Data());    // be unique for multiple client connections!
00056    TString nomen("ThreadHandler of "); nomen+=GetName();
00057    fxWorkHandler=new TGo4ThreadHandler(nomen.Data(), this);
00058    fxBlocker=new TGo4AppControlTimer(this,fglTIMERPERIOD);
00059    fbAppBlocking=blockingmode;
00060    fbAutoCreate=autocreate;
00061    fbAutoStart=autostart;
00062    if(fbAppBlocking)
00063       // manager in blocking mode (analysis without gui)
00064       {
00065          TGo4Log::Debug(" ThreadManager -- Starting in application blocking mode ");
00066          fxBlocker->SetApplicationRun(kFALSE);
00067       }
00068    else
00069       // manager in non blocking mode (for gui)
00070       {
00071          TGo4Log::Debug(" ThreadManager --  Starting in application non-blocking mode ");
00072          fxBlocker->SetApplicationRun(kTRUE);
00073       }
00074 }
00075 
00076 
00077 TGo4ThreadManager::~TGo4ThreadManager()
00078 {
00079    TRACE((15,"TGo4ThreadManager::~TGo4ThreadManager destructor",__LINE__, __FILE__));
00080    delete fxWorkHandler; // this will cancel all threads and delete the internal instances
00081    //delete fxBlocker; // dtor is called from fxBlocker, may not delete it!
00082    //gApplication->Terminate(0);
00083 }
00084 
00085 Bool_t TGo4ThreadManager::BlockApp ()
00086 {
00087    TRACE((12,"TGo4ThreadManager::BlockApp()",__LINE__, __FILE__));
00088    Bool_t rev=kFALSE;
00089    if(fbAppBlocking)
00090       {
00091       if( !fbTerminating && !( fxWorkHandler->IsOperating() ) )
00092          {
00093             TRACE((11,"TGo4ThreadManager::BlockApp() blocking mode",__LINE__, __FILE__));
00094             fxBlocker->SetApplicationRun(kFALSE);
00095             rev=kTRUE;
00096          }
00097       else
00098          {
00099          // in case of Termination or threadhandler operation:
00100          // do not block app again, may deadlock control timer
00101             TRACE((11,"TGo4ThreadManager::BlockApp() unblocking mode",__LINE__, __FILE__));
00102             rev=kFALSE;
00103          }
00104       }
00105    else
00106       {
00107       // manager is in non blocking mode (enables gui callback operations)
00108          rev=kFALSE;
00109       }
00110    return rev;
00111 }
00112 
00113 Bool_t TGo4ThreadManager::UnBlockApp (Int_t mode)
00114 {
00115    TRACE((12,"TGo4ThreadManager::UnBlockApp()",__LINE__, __FILE__));
00116    Bool_t rev=kFALSE;
00117    switch(mode)
00118       {
00119       case 0:
00120          {
00121          TRACE((11,"TGo4ThreadManager::UnBlockApp() mode 0",__LINE__, __FILE__));
00122          if(!fxBlocker->GetApplicationRun())
00123             // only send condition if timer is really waiting
00124             {
00125 
00126                fxBlocker->SetApplicationRun(kTRUE);
00127                ((TCondition*) fxBlocker->GetCondition() )->Signal();
00128             }
00129          rev=kTRUE;
00130          }
00131          break;
00132       case 1:
00133          {
00134          TRACE((11,"TGo4ThreadManager::UnBlockApp() mode 1",__LINE__, __FILE__));
00135          fxBlocker->SetApplicationRun(kTRUE);
00136          rev=kTRUE;
00137          }
00138          break;
00139       case 2:
00140          {
00141          TRACE((11,"TGo4ThreadManager::UnBlockApp() mode 2",__LINE__, __FILE__));
00142          if(!fxBlocker->GetApplicationRun())
00143             // only send condition if timer is really waiting
00144             {
00145                ((TCondition*)  fxBlocker->GetCondition() )->Signal();
00146             }
00147          rev=kTRUE;
00148          }
00149          break;
00150       default:
00151          {
00152          TRACE((16,"++TGo4ThreadManager::UnBlockApp() unknown mode"));
00153          //cerr << "TGo4ThreadManager::UnBlockApp() unknown mode"<<endl;
00154          rev=kFALSE;
00155          }
00156          break;
00157       }  // switch(mode)
00158   return rev;
00159 }
00160 
00161 Int_t TGo4ThreadManager::Initialization ()
00162 {
00163   TRACE((12,"TGo4ThreadManager::Initialization()",__LINE__, __FILE__));
00164   if(fbInitDone)
00165     // already initialized, return ok value
00166     {
00167        TRACE((11,"TGo4ThreadManager::Initialization()--already init done, returning",__LINE__, __FILE__));
00168        //cout << "already init done, returning"<<endl;
00169       return 0;
00170     }
00171   else
00172     // first call of initialization: check threads
00173     {
00174       if(fbAutoCreate)
00175          // auto thread creation mode: wait for all threads being up
00176          {
00177             TRACE((11,"TGo4ThreadManager::Initialization()--in AutoCreate mode",__LINE__, __FILE__));
00178             if( fxWorkHandler->AllCreated() )
00179                // test for threads, block timer and start work if they are up
00180                {
00181                   TGo4Log::Debug(" ThreadManager -- All threads are up, writing dump file ");
00182                   BlockApp();
00183                   fxWorkHandler->DumpThreads();
00184                   if(fbAutoStart)
00185                      {
00186                         // autostart mode of runnables
00187                         TRACE((11,"TGo4ThreadManager::Initialization()--in AutoStart mode",__LINE__, __FILE__));
00188                          fxWorkHandler->StartAll();
00189                      }
00190                   else
00191                      {
00192                         TRACE((11,"TGo4ThreadManager::Initialization()--in non-AutoStart mode",__LINE__, __FILE__));
00193 
00194                         // do not start runnables
00195                      }
00196 
00197                   fbInitDone=kTRUE;
00198                   return 0;
00199                }
00200             else
00201               // some threads are missing, suspend starting until next timer cycle
00202               {
00203                 TGo4Log::Debug(" ThreadManager -- some threads are missing, re-doing Init ");
00204 
00205                 //cout <<"TGo4ThreadManager waiting for threads"<<endl;
00206                 return 1;
00207               }
00208          }
00209       else
00210          {
00211             // do not check or dump threads
00212             TRACE((11,"TGo4ThreadManager::Initialization()--not in AutoCreate mode",__LINE__, __FILE__));
00213             fbInitDone=kTRUE;
00214             return 0;
00215          }
00216     }
00217 }
00218 
00219 void TGo4ThreadManager::Launch ()
00220 {
00221    TRACE((15,"TGo4ThreadManager::Launch()",__LINE__, __FILE__));
00222    if(fbAutoCreate)
00223       {
00224          // create all TThreads of TGo4Threads in threadhandler list
00225          TRACE((13,"TGo4ThreadManager::Launch()-- executing AutoCreate mode",__LINE__, __FILE__));
00226          fxWorkHandler->CreateAll();
00227       }
00228    else
00229       {
00230          // do not create TThreads
00231          TRACE((13,"TGo4ThreadManager::Launch()-- no AutoCreate mode",__LINE__, __FILE__));
00232       }
00233    fxBlocker->TurnOn();  // later in method which is called at the end of derived ctor?
00234 }
00235 
00236 void TGo4ThreadManager::Terminate (Bool_t termapp)
00237 {
00238    TRACE((15,"TGo4ThreadManager::Terminate()",__LINE__, __FILE__));
00239    TGo4Log::Debug(" ThreadManager -- Preparing Termination... ");
00240    fxWorkHandler->StopAll();
00241    //gSystem->Sleep(10000); // wait for workfunc to return
00242    fbTerminating=kTRUE;
00243    fbTerminateApplication=termapp;
00244    {
00245    TRACE((13,"TGo4ThreadManager::Terminate()--waking up timer:",__LINE__, __FILE__));
00246    UnBlockApp(); // wake up blocking timer
00247    }
00248 }
00249 
00250 void TGo4ThreadManager::TerminateFast()
00251 {
00252    Terminate();
00253 }
00254 
00255 
00256 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:31 2008 for Go4-v3.04-1 by  doxygen 1.4.2