00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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;
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();
00053 myname+="-";
00054 myname+=gSystem->GetPid();
00055 SetName(myname.Data());
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
00064 {
00065 TGo4Log::Debug(" ThreadManager -- Starting in application blocking mode ");
00066 fxBlocker->SetApplicationRun(kFALSE);
00067 }
00068 else
00069
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;
00081
00082
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
00100
00101 TRACE((11,"TGo4ThreadManager::BlockApp() unblocking mode",__LINE__, __FILE__));
00102 rev=kFALSE;
00103 }
00104 }
00105 else
00106 {
00107
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
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
00144 {
00145 ((TCondition*) fxBlocker->GetCondition() )->Signal();
00146 }
00147 rev=kTRUE;
00148 }
00149 break;
00150 default:
00151 {
00152 TRACE((16,"++TGo4ThreadManager::UnBlockApp() unknown mode"));
00153
00154 rev=kFALSE;
00155 }
00156 break;
00157 }
00158 return rev;
00159 }
00160
00161 Int_t TGo4ThreadManager::Initialization ()
00162 {
00163 TRACE((12,"TGo4ThreadManager::Initialization()",__LINE__, __FILE__));
00164 if(fbInitDone)
00165
00166 {
00167 TRACE((11,"TGo4ThreadManager::Initialization()--already init done, returning",__LINE__, __FILE__));
00168
00169 return 0;
00170 }
00171 else
00172
00173 {
00174 if(fbAutoCreate)
00175
00176 {
00177 TRACE((11,"TGo4ThreadManager::Initialization()--in AutoCreate mode",__LINE__, __FILE__));
00178 if( fxWorkHandler->AllCreated() )
00179
00180 {
00181 TGo4Log::Debug(" ThreadManager -- All threads are up, writing dump file ");
00182 BlockApp();
00183 fxWorkHandler->DumpThreads();
00184 if(fbAutoStart)
00185 {
00186
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
00195 }
00196
00197 fbInitDone=kTRUE;
00198 return 0;
00199 }
00200 else
00201
00202 {
00203 TGo4Log::Debug(" ThreadManager -- some threads are missing, re-doing Init ");
00204
00205
00206 return 1;
00207 }
00208 }
00209 else
00210 {
00211
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
00225 TRACE((13,"TGo4ThreadManager::Launch()-- executing AutoCreate mode",__LINE__, __FILE__));
00226 fxWorkHandler->CreateAll();
00227 }
00228 else
00229 {
00230
00231 TRACE((13,"TGo4ThreadManager::Launch()-- no AutoCreate mode",__LINE__, __FILE__));
00232 }
00233 fxBlocker->TurnOn();
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
00242 fbTerminating=kTRUE;
00243 fbTerminateApplication=termapp;
00244 {
00245 TRACE((13,"TGo4ThreadManager::Terminate()--waking up timer:",__LINE__, __FILE__));
00246 UnBlockApp();
00247 }
00248 }
00249
00250 void TGo4ThreadManager::TerminateFast()
00251 {
00252 Terminate();
00253 }
00254
00255
00256