00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4TestThreadManager.h"
00015
00016 #include "TApplication.h"
00017 #include "TH1.h"
00018 #include "TRandom.h"
00019 #include "TCanvas.h"
00020 #include "TPad.h"
00021 #include "TThread.h"
00022
00023 #include "TGo4Log.h"
00024 #include "TGo4Thread.h"
00025 #include "TGo4ThreadHandler.h"
00026 #include "TGo4TestRunnable.h"
00027
00028 #include "TGo4TerminateException.h"
00029 #include "TGo4RestartException.h"
00030 #include "TGo4LogicException.h"
00031
00032 TGo4TestRunnable::TGo4TestRunnable()
00033 :TGo4Runnable("dummy",0),fxPad(0),fxCanvas(0),fxHisto(0)
00034 {
00035 GO4TRACE((14,"TGo4TestRunnable::TGo4Runnable() constructor",__LINE__, __FILE__));
00036 }
00037
00038 TGo4TestRunnable::TGo4TestRunnable(const TGo4TestRunnable &right)
00039 :TGo4Runnable(right),fxPad(0),fxCanvas(0),fxHisto(0)
00040 {
00041 GO4TRACE((14,"TGo4TestRunnable::TGo4TestRunnable() copy ctor",__LINE__, __FILE__));
00042 fiMode=right.fiMode;
00043 }
00044
00045 TGo4TestRunnable::TGo4TestRunnable(const char* name, TGo4ThreadManager* man, Int_t mode)
00046 :TGo4Runnable(name,man),fxPad(0),fxCanvas(0),fxHisto(0)
00047 {
00048 GO4TRACE((14,"TGo4TestRunnable::TGo4TestRunnable(const char*,TGo4ThreadManager*,Int_t) constructor",__LINE__, __FILE__));
00049 fiMode=mode;
00050 }
00051
00052
00053 TGo4TestRunnable::~TGo4TestRunnable()
00054 {
00055 GO4TRACE((14,"TGo4TestRunnable::~TGo4TestRunnable() destructor",__LINE__, __FILE__));
00056 }
00057
00058 Int_t TGo4TestRunnable::Run (void* ptr)
00059 {
00060 GO4TRACE((12,"TGo4TestRunnable::Run()",__LINE__, __FILE__));
00061
00062 Int_t i=0;
00063 static Int_t t=0;
00064 static Int_t loops=0;
00065 Axis_t x=0;
00066 TGo4ThreadHandler* han=0;
00067 TGo4TestRunnable* th1run=0;
00068 TGo4Thread* go4thr=0;
00069 TCanvas* can=((TGo4TestThreadManager*)fxManager)->GetCanvas();
00070 TPad* pad1=((TGo4TestThreadManager*)fxManager)->GetPad1();
00071 TPad* pad2=((TGo4TestThreadManager*)fxManager)->GetPad2();
00072
00073 switch(fiMode)
00074 {
00075 case 0:
00076 {
00077 GO4TRACE((11,"TGo4TestRunnable::Run() mode 0",__LINE__, __FILE__));
00078 TGo4Log::Debug(" TestRunnable ''%s'' running, testcase %d -- ",
00079 GetName(),fiMode);
00080 fxGo4Thread->Sleep(5000);
00081 }
00082 break;
00083 case 1:
00084 {
00085 GO4TRACE((11,"TGo4TestRunnable::Run() mode 1",__LINE__, __FILE__));
00086 TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00087 GetName(),fiMode);
00088 fxGo4Thread->Sleep(30000);
00089 TGo4Log::Debug("\t ''%s'' killing all other Threads",
00090 GetName());
00091 han= (TGo4ThreadHandler*) fxManager->GetWorkHandler();
00092 han->Cancel("First Thread");
00093 han->Cancel("Second Thread");
00094 han->Cancel("Third Thread");
00095 han->Cancel("Fourth Thread");
00096 fxGo4Thread->Sleep(5000);
00097 TGo4Log::Debug("\t ''%s'' re-starting all other Threads", GetName());
00098 han->Create("First Thread");
00099 han->Create("Second Thread");
00100 han->Create("Third Thread");
00101 han->Create("Fourth Thread");
00102 }
00103 break;
00104 case 2:
00105 {
00106 GO4TRACE((11,"TGo4TestRunnable::Run() mode 2",__LINE__, __FILE__));
00107 TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00108 GetName(),fiMode);
00109 fxGo4Thread->Sleep(10000);
00110 TGo4Log::Debug("\t ''%s'' stopping all other Workfunctions:", GetName());
00111 han=(TGo4ThreadHandler*) fxManager->GetWorkHandler();
00112 han->Stop("First Thread");
00113 han->Stop("Second Thread");
00114 han->Stop("Third Thread");
00115 han->Stop("Fourth Thread");
00116 fxGo4Thread->Sleep(10000);
00117 TGo4Log::Debug("\t ''%s'' re-starting all other Workfunctions:", GetName());
00118 han->Start("First Thread");
00119 han->Start("Second Thread");
00120 han->Start("Third Thread");
00121 han->Start("Fourth Thread");
00122 }
00123 break;
00124 case 3:
00125
00126 {
00127 GO4TRACE((11,"TGo4TestRunnable::Run() mode 3",__LINE__, __FILE__));
00128 TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00129 GetName(),fiMode);
00130 fxGo4Thread->Sleep(30000);
00131 TGo4Log::Debug("\t ''%s'' terminating ThreadManager", GetName());
00132
00133 throw TGo4TerminateException(this);
00134 }
00135 break;
00136 case 4:
00137
00138 {
00139 GO4TRACE((11,"TGo4TestRunnable::Run() mode 4",__LINE__, __FILE__));
00140 TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00141 GetName(),fiMode);
00142 fxGo4Thread->Sleep(2000);
00143 TGo4Log::Debug("\t ''%s'' creating histogram", GetName());
00144 if(fxHisto==0)
00145 {
00146 fxHisto = new TH1D(GetName(),"histogram",2048,0,100);
00147 }
00148 else
00149 {
00150
00151 }
00152 while(i++<1000)
00153 {
00154 x=gRandom->Gaus(50,7);
00155 fxHisto->Fill(x);
00156 }
00157 fxManager->UnBlockApp();
00158 if(strstr(GetName(),"Maker1"))
00159 {
00160 pad1->cd();
00161 }
00162 else
00163 {
00164 pad2->cd();
00165 }
00166
00167 fxHisto->Draw();
00168 can->Modified();
00169 can->Update();
00170 TThread::CancelPoint();
00171 fxManager->BlockApp();
00172 TGo4Log::Debug("\t ''%s'' histogram filled, waiting", GetName());
00173 fxGo4Thread->Sleep(2000);
00174 TThread::CancelPoint();
00175 if(strstr(GetName(),"Maker1"))
00176 {
00177 TGo4Log::Debug("\t ''%s'' deleting histogram", GetName());
00178 delete fxHisto;
00179 fxHisto=0;
00180 }
00181 else
00182 {
00183 if((++t%5)==0)
00184 {
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 }
00201 }
00202 }
00203 break;
00204 case 5:
00205 {
00206 GO4TRACE((11,"TGo4TestRunnable::Run() mode 5",__LINE__, __FILE__));
00207 TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00208 GetName(),fiMode);
00209 fxGo4Thread->Sleep(15000);
00210 TGo4Log::Debug("\t ''%s'' removing first thread", GetName());
00211
00212 go4thr=((TGo4ThreadHandler*) fxManager->GetWorkHandler())->GetThread("First Thread");
00213 ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->RemoveThread("First Thread");
00214
00215 TString rname = TString::Format("NewHistogramMaker1:%d",loops++);
00216
00217
00218 if(loops==1)
00219 {
00220
00221 TGo4Log::Debug("\t ''%s'' creating new first thread", GetName());
00222 th1run= new TGo4TestRunnable(rname.Data(),fxManager,4);
00223 go4thr= new TGo4Thread("First Thread",th1run,kFALSE);
00224 }
00225 else
00226 {
00227 fxManager->UnBlockApp();
00228 go4thr->Cancel();
00229 fxManager->BlockApp();
00230 }
00231 TGo4Log::Debug("\t ''%s'' adding new first thread", GetName());
00232 ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->AddThread(go4thr);
00233 ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->Start("First Thread");
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 }
00247 break;
00248 case 6:
00249
00250 {
00251 GO4TRACE((11,"TGo4TestRunnable::Run() mode 6",__LINE__, __FILE__));
00252 TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00253 GetName(),fiMode);
00254 fxGo4Thread->Sleep(61000);
00255 TGo4Log::Debug("\t ''%s'' throwing exception now:", GetName());
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 throw TGo4RestartException(this,"Second Thread");
00268
00269
00270
00271
00272
00273 }
00274 break;
00275 case 7:
00276 {
00277 GO4TRACE((11,"TGo4TestRunnable::Run() mode 7",__LINE__, __FILE__));
00278 TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00279 GetName(),fiMode);
00280 TGo4Log::Debug("\t ''%s'' creating histogram", GetName());
00281 if(fxHisto==0)
00282 {
00283 fxHisto = new TH1D(GetName(),"histogram",2048,0,100);
00284 }
00285 else
00286 {
00287
00288 }
00289 while(i++<1000)
00290 {
00291 x=gRandom->Gaus(50,7);
00292 fxHisto->Fill(x);
00293 }
00294
00295
00296
00297
00298
00299
00300 TGo4Log::Debug("\t ''%s'' histogram %d filled, waiting", GetName(),loops);
00301 fxGo4Thread->Sleep(5000);
00302 TGo4Log::Debug("\t ''%s'' deleting histogram %d", GetName(),loops++);
00303 delete fxHisto;
00304 fxHisto=0;
00305 }
00306 break;
00307 default:
00308 {
00309 GO4TRACE((11,"TGo4TestRunnable::Run() mode default",__LINE__, __FILE__));
00310 TGo4Log::Debug(" TestRunnable ''%s'' -- unspecified Workfunc action! ",
00311 GetName(),fiMode);
00312 throw TGo4LogicException();
00313 }
00314 break;
00315 }
00316 return 0;
00317
00318 }
00319
00320 void TGo4TestRunnable::ThreadCatch (TGo4Exception& ex)
00321 {
00322 GO4TRACE((12,"TGo4TestRunnable::ThreadCatch()",__LINE__, __FILE__));
00323 TGo4Runnable::ThreadCatch(ex);
00324
00325
00326
00327
00328
00329
00330
00331
00332 }
00333
00334 void TGo4TestRunnable::UnexpectedCatch ()
00335 {
00336 GO4TRACE((12,"TGo4TestRunnable::UnexpectedCatch()",__LINE__, __FILE__));
00337
00338 TGo4Runnable::UnexpectedCatch();
00339
00340
00341
00342
00343
00344
00345 }
00346
00347
00348
00349
00350