00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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;
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();
00049 myname+="-";
00050 myname+=gSystem->GetPid();
00051 SetName(myname.Data());
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
00061 {
00062 TGo4Log::Debug(" ThreadManager -- Starting in application blocking mode ");
00063 fxBlocker->SetApplicationRun(kFALSE);
00064 }
00065 else
00066
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;
00078
00079
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
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
00118
00119 TRACE((11,"TGo4ThreadManager::BlockApp() unblocking mode",__LINE__, __FILE__));
00120 rev=kFALSE;
00121 }
00122 }
00123 else
00124 {
00125
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
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
00162 {
00163 ((TCondition*) fxBlocker->GetCondition() )->Signal();
00164 }
00165 rev=kTRUE;
00166 }
00167 break;
00168 default:
00169 {
00170 TRACE((16,"++TGo4ThreadManager::UnBlockApp() unknown mode"));
00171
00172 rev=kFALSE;
00173 }
00174 break;
00175 }
00176 return rev;
00177 }
00178
00179 Int_t TGo4ThreadManager::Initialization ()
00180 {
00181 TRACE((12,"TGo4ThreadManager::Initialization()",__LINE__, __FILE__));
00182 if(fbInitDone)
00183
00184 {
00185 TRACE((11,"TGo4ThreadManager::Initialization()--already init done, returning",__LINE__, __FILE__));
00186
00187 return 0;
00188 }
00189 else
00190
00191 {
00192 if(fbAutoCreate)
00193
00194 {
00195 TRACE((11,"TGo4ThreadManager::Initialization()--in AutoCreate mode",__LINE__, __FILE__));
00196 if( fxWorkHandler->AllCreated() )
00197
00198 {
00199 TGo4Log::Debug(" ThreadManager -- All threads are up, writing dump file ");
00200 BlockApp();
00201 fxWorkHandler->DumpThreads();
00202 if(fbAutoStart)
00203 {
00204
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
00213 }
00214
00215 fbInitDone=kTRUE;
00216 return 0;
00217 }
00218 else
00219
00220 {
00221 TGo4Log::Debug(" ThreadManager -- some threads are missing, re-doing Init ");
00222
00223
00224 return 1;
00225 }
00226 }
00227 else
00228 {
00229
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
00243 TRACE((13,"TGo4ThreadManager::Launch()-- executing AutoCreate mode",__LINE__, __FILE__));
00244 fxWorkHandler->CreateAll();
00245 }
00246 else
00247 {
00248
00249 TRACE((13,"TGo4ThreadManager::Launch()-- no AutoCreate mode",__LINE__, __FILE__));
00250 }
00251 fxBlocker->TurnOn();
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
00260 fbTerminating=kTRUE;
00261 fbTerminateApplication=termapp;
00262 {
00263 TRACE((13,"TGo4ThreadManager::Terminate()--waking up timer:",__LINE__, __FILE__));
00264 UnBlockApp();
00265 }
00266 }
00267
00268 void TGo4ThreadManager::TerminateFast ()
00269 {
00270 Terminate();
00271 }
00272
00273
00274 ClassImp(TGo4ThreadManager)
00275
00276