00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4ThreadManager.h"
00015
00016 #include "TApplication.h"
00017 #include "TSystem.h"
00018 #include "TCondition.h"
00019
00020 #include "TGo4Log.h"
00021 #include "TGo4ThreadHandler.h"
00022 #include "TGo4AppControlTimer.h"
00023
00024 const Long_t TGo4ThreadManager::fglTIMERPERIOD = 200;
00025
00026 TGo4ThreadManager::TGo4ThreadManager(const TGo4ThreadManager &right)
00027 :TNamed(right),
00028 fbInitDone(kFALSE),
00029 fbTerminating(kFALSE),
00030 fbTerminateApplication(kFALSE)
00031 {
00032 GO4TRACE((15,"TGo4ThreadManager::TGo4ThreadManager copy ctor",__LINE__, __FILE__));
00033 fxWorkHandler=right.fxWorkHandler;
00034 fxBlocker = right.fxBlocker;
00035 }
00036
00037 TGo4ThreadManager::TGo4ThreadManager (const char* name, Bool_t blockingmode, Bool_t autostart, Bool_t autocreate)
00038 :TNamed(name,"This is a TGo4ThreadManager"),
00039 fbInitDone(kFALSE),
00040 fbTerminating(kFALSE),
00041 fbTerminateApplication(kFALSE),
00042 fbBeingQuit(kFALSE)
00043 {
00044 GO4TRACE((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();
00049 myname+="-";
00050 myname+=gSystem->GetPid();
00051 SetName(myname.Data());
00052 TString nomen("ThreadHandler of "); nomen+=GetName();
00053 fxWorkHandler=new TGo4ThreadHandler(nomen.Data(), this);
00054 fxBlocker=new TGo4AppControlTimer(this,fglTIMERPERIOD);
00055 fbAppBlocking=blockingmode;
00056 fbAutoCreate=autocreate;
00057 fbAutoStart=autostart;
00058 if(fbAppBlocking)
00059
00060 {
00061 TGo4Log::Debug(" ThreadManager -- Starting in application blocking mode ");
00062 fxBlocker->SetApplicationRun(kFALSE);
00063 }
00064 else
00065
00066 {
00067 TGo4Log::Debug(" ThreadManager -- Starting in application non-blocking mode ");
00068 fxBlocker->SetApplicationRun(kTRUE);
00069 }
00070 }
00071
00072
00073 TGo4ThreadManager::~TGo4ThreadManager()
00074 {
00075 GO4TRACE((15,"TGo4ThreadManager::~TGo4ThreadManager destructor",__LINE__, __FILE__));
00076 delete fxWorkHandler;
00077
00078
00079 }
00080
00081 Bool_t TGo4ThreadManager::BlockApp ()
00082 {
00083 GO4TRACE((12,"TGo4ThreadManager::BlockApp()",__LINE__, __FILE__));
00084 Bool_t rev=kFALSE;
00085 if(fbAppBlocking)
00086 {
00087 if( !fbTerminating && !( fxWorkHandler->IsOperating() ) )
00088 {
00089 GO4TRACE((11,"TGo4ThreadManager::BlockApp() blocking mode",__LINE__, __FILE__));
00090 fxBlocker->SetApplicationRun(kFALSE);
00091 rev=kTRUE;
00092 }
00093 else
00094 {
00095
00096
00097 GO4TRACE((11,"TGo4ThreadManager::BlockApp() unblocking mode",__LINE__, __FILE__));
00098 rev=kFALSE;
00099 }
00100 }
00101 else
00102 {
00103
00104 rev=kFALSE;
00105 }
00106 return rev;
00107 }
00108
00109 Bool_t TGo4ThreadManager::UnBlockApp (Int_t mode)
00110 {
00111 GO4TRACE((12,"TGo4ThreadManager::UnBlockApp()",__LINE__, __FILE__));
00112 Bool_t rev=kFALSE;
00113 switch(mode)
00114 {
00115 case 0:
00116 {
00117 GO4TRACE((11,"TGo4ThreadManager::UnBlockApp() mode 0",__LINE__, __FILE__));
00118 if(!fxBlocker->GetApplicationRun())
00119
00120 {
00121
00122 fxBlocker->SetApplicationRun(kTRUE);
00123 ((TCondition*) fxBlocker->GetCondition() )->Signal();
00124 }
00125 rev=kTRUE;
00126 }
00127 break;
00128 case 1:
00129 {
00130 GO4TRACE((11,"TGo4ThreadManager::UnBlockApp() mode 1",__LINE__, __FILE__));
00131 fxBlocker->SetApplicationRun(kTRUE);
00132 rev=kTRUE;
00133 }
00134 break;
00135 case 2:
00136 {
00137 GO4TRACE((11,"TGo4ThreadManager::UnBlockApp() mode 2",__LINE__, __FILE__));
00138 if(!fxBlocker->GetApplicationRun())
00139
00140 {
00141 ((TCondition*) fxBlocker->GetCondition() )->Signal();
00142 }
00143 rev=kTRUE;
00144 }
00145 break;
00146 default:
00147 {
00148 GO4TRACE((16,"++TGo4ThreadManager::UnBlockApp() unknown mode"));
00149
00150 rev=kFALSE;
00151 }
00152 break;
00153 }
00154 return rev;
00155 }
00156
00157 Int_t TGo4ThreadManager::Initialization ()
00158 {
00159 GO4TRACE((12,"TGo4ThreadManager::Initialization()",__LINE__, __FILE__));
00160 if(fbInitDone)
00161
00162 {
00163 GO4TRACE((11,"TGo4ThreadManager::Initialization()--already init done, returning",__LINE__, __FILE__));
00164
00165 return 0;
00166 }
00167 else
00168
00169 {
00170 if(fbAutoCreate)
00171
00172 {
00173 GO4TRACE((11,"TGo4ThreadManager::Initialization()--in AutoCreate mode",__LINE__, __FILE__));
00174 if( fxWorkHandler->AllCreated() )
00175
00176 {
00177 TGo4Log::Debug(" ThreadManager -- All threads are up, writing dump file ");
00178 BlockApp();
00179 fxWorkHandler->DumpThreads();
00180 if(fbAutoStart)
00181 {
00182
00183 GO4TRACE((11,"TGo4ThreadManager::Initialization()--in AutoStart mode",__LINE__, __FILE__));
00184 fxWorkHandler->StartAll();
00185 }
00186 else
00187 {
00188 GO4TRACE((11,"TGo4ThreadManager::Initialization()--in non-AutoStart mode",__LINE__, __FILE__));
00189
00190
00191 }
00192
00193 fbInitDone=kTRUE;
00194 return 0;
00195 }
00196 else
00197
00198 {
00199 TGo4Log::Debug(" ThreadManager -- some threads are missing, re-doing Init ");
00200
00201
00202 return 1;
00203 }
00204 }
00205 else
00206 {
00207
00208 GO4TRACE((11,"TGo4ThreadManager::Initialization()--not in AutoCreate mode",__LINE__, __FILE__));
00209 fbInitDone=kTRUE;
00210 return 0;
00211 }
00212 }
00213 }
00214
00215 void TGo4ThreadManager::Launch ()
00216 {
00217 GO4TRACE((15,"TGo4ThreadManager::Launch()",__LINE__, __FILE__));
00218 if(fbAutoCreate)
00219 {
00220
00221 GO4TRACE((13,"TGo4ThreadManager::Launch()-- executing AutoCreate mode",__LINE__, __FILE__));
00222 fxWorkHandler->CreateAll();
00223 }
00224 else
00225 {
00226
00227 GO4TRACE((13,"TGo4ThreadManager::Launch()-- no AutoCreate mode",__LINE__, __FILE__));
00228 }
00229 fxBlocker->TurnOn();
00230 }
00231
00232 void TGo4ThreadManager::Terminate (Bool_t termapp)
00233 {
00234 GO4TRACE((15,"TGo4ThreadManager::Terminate()",__LINE__, __FILE__));
00235 TGo4Log::Debug(" ThreadManager -- Preparing Termination... ");
00236 fxWorkHandler->StopAll();
00237
00238 fbTerminating=kTRUE;
00239 fbTerminateApplication=termapp;
00240 {
00241 GO4TRACE((13,"TGo4ThreadManager::Terminate()--waking up timer:",__LINE__, __FILE__));
00242 UnBlockApp();
00243 }
00244 }
00245
00246 void TGo4ThreadManager::TerminateFast()
00247 {
00248 Terminate();
00249 }
00250