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