Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4ThreadManager/TGo4ThreadManager.cxx

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

Generated on Tue Nov 8 10:56:07 2005 for Go4-v2.10-5 by doxygen1.2.15