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