Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4ThreadManagerExample/TGo4TestRunnable.cxx

Go to the documentation of this file.
00001 //---------------------------------------------------------------
00002 //        Go4 Release Package v2.10-5 (build 21005) 
00003 //                      03-Nov-2005
00004 //---------------------------------------------------------------
00005 //       The GSI Online Offline Object Oriented (Go4) Project
00006 //       Experiment Data Processing at DVEE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "TGo4TestThreadManager.h"
00017 
00018 #include <iostream.h>
00019 
00020 #include "TGo4TestRunnable.h"
00021 #include "Go4Log/TGo4Log.h"
00022 
00023 TGo4TestRunnable::TGo4TestRunnable()
00024 :TGo4Runnable("dummy",0),fxPad(0),fxCanvas(0),fxHisto(0)
00025 {
00026   TRACE((14,"TGo4TestRunnable::TGo4Runnable() constructor",__LINE__, __FILE__));
00027 }
00028 
00029 TGo4TestRunnable::TGo4TestRunnable(const TGo4TestRunnable &right)
00030    :TGo4Runnable(right),fxPad(0),fxCanvas(0),fxHisto(0)
00031 {
00032    TRACE((14,"TGo4TestRunnable::TGo4TestRunnable() copy ctor",__LINE__, __FILE__));
00033    fiMode=right.fiMode;
00034 }
00035 
00036 TGo4TestRunnable::TGo4TestRunnable (Text_t* name, TGo4ThreadManager* man, Int_t mode)
00037   :TGo4Runnable(name,man),fxPad(0),fxCanvas(0),fxHisto(0)
00038 {
00039   TRACE((14,"TGo4TestRunnable::TGo4TestRunnable(Text_t*,TGo4ThreadManager*,Int_t) constructor",__LINE__, __FILE__));
00040   fiMode=mode;
00041 }
00042 
00043 
00044 TGo4TestRunnable::~TGo4TestRunnable()
00045 {
00046    TRACE((14,"TGo4TestRunnable::~TGo4TestRunnable() destructor",__LINE__, __FILE__));
00047 }
00048 
00049 
00050 TGo4TestRunnable & TGo4TestRunnable::operator=(const TGo4TestRunnable &right)
00051 {
00052    TRACE((14,"TGo4TestRunnable::operator=",__LINE__, __FILE__));
00053    if (&right!=this)
00054       {
00055          TRACE((13,"TGo4TestRunnable::operator= Processing copy",__LINE__, __FILE__));
00056          TGo4Runnable::operator=(right);
00057          fiMode=right.fiMode;
00058          fxCanvas=0;
00059          fxPad=0;
00060          fxHisto=0;
00061          return *this;
00062       }
00063    else
00064       {
00065          // copy is already source object
00066          TRACE((13,"TGo4TestRunnable::operator= source and destination objects are identical",__LINE__, __FILE__));
00067          return *this;
00068       }
00069 }
00070 
00071 
00072 
00073 Int_t TGo4TestRunnable::Run (void* ptr)
00074 {
00075    TRACE((12,"TGo4TestRunnable::Run()",__LINE__, __FILE__));
00076 
00077    static Text_t buf[256];
00078    Int_t i=0;
00079    static Int_t t=0;
00080    static Int_t loops=0;
00081    Axis_t x=0;
00082    TGo4ThreadHandler* han=0;
00083    TGo4TestRunnable* th1run=0;
00084    TGo4Thread* go4thr=0;
00085    TCanvas* can=((TGo4TestThreadManager*)fxManager)->GetCanvas();
00086    TPad* pad1=((TGo4TestThreadManager*)fxManager)->GetPad1();
00087    TPad* pad2=((TGo4TestThreadManager*)fxManager)->GetPad2();
00088 
00089   switch(fiMode)
00090    {
00091       case 0:
00092          {
00093          TRACE((11,"TGo4TestRunnable::Run() mode 0",__LINE__, __FILE__));
00094          TGo4Log::Debug(" TestRunnable ''%s'' running, testcase %d -- ",
00095                   GetName(),fiMode);
00096          fxGo4Thread->Sleep(5000);
00097          }
00098          break;
00099       case 1:
00100          {
00101          TRACE((11,"TGo4TestRunnable::Run() mode 1",__LINE__, __FILE__));
00102          TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00103                   GetName(),fiMode);
00104          fxGo4Thread->Sleep(30000);
00105          TGo4Log::Debug("\t ''%s'' killing all other Threads",
00106                   GetName());
00107          han= (TGo4ThreadHandler*) fxManager->GetWorkHandler();
00108          han->Cancel("First Thread");
00109          han->Cancel("Second Thread");
00110          han->Cancel("Third Thread");
00111          han->Cancel("Fourth Thread");
00112          fxGo4Thread->Sleep(5000);
00113          TGo4Log::Debug("\t ''%s'' re-starting all other Threads", GetName());
00114          han->Create("First Thread");
00115          han->Create("Second Thread");
00116          han->Create("Third Thread");
00117          han->Create("Fourth Thread");
00118          }
00119          break;
00120       case 2:
00121          {
00122          TRACE((11,"TGo4TestRunnable::Run() mode 2",__LINE__, __FILE__));
00123          TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00124                   GetName(),fiMode);
00125          fxGo4Thread->Sleep(10000);
00126          TGo4Log::Debug("\t ''%s'' stopping all other Workfunctions:", GetName());
00127          han=(TGo4ThreadHandler*) fxManager->GetWorkHandler();
00128          han->Stop("First Thread");
00129          han->Stop("Second Thread");
00130          han->Stop("Third Thread");
00131          han->Stop("Fourth Thread");
00132          fxGo4Thread->Sleep(10000);
00133          TGo4Log::Debug("\t ''%s'' re-starting all other Workfunctions:", GetName());
00134          han->Start("First Thread");
00135          han->Start("Second Thread");
00136          han->Start("Third Thread");
00137          han->Start("Fourth Thread");
00138          }
00139          break;
00140       case 3:
00141          // can we call methods of manager? aborting the whole thing?
00142          {
00143          TRACE((11,"TGo4TestRunnable::Run() mode 3",__LINE__, __FILE__));
00144          TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00145                   GetName(),fiMode);
00146          fxGo4Thread->Sleep(30000);
00147          TGo4Log::Debug("\t ''%s'' terminating ThreadManager", GetName());
00148          //fxManager->Terminate(); // the direct approach
00149          throw TGo4TerminateException(this); //same, using a control exception
00150          }
00151          break;
00152       case 4:
00153          // memory alloc and histogram drawing test
00154          {
00155          TRACE((11,"TGo4TestRunnable::Run() mode 4",__LINE__, __FILE__));
00156          TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00157                   GetName(),fiMode);
00158          fxGo4Thread->Sleep(2000);
00159          TGo4Log::Debug("\t ''%s'' creating histogram", GetName());
00160          if(fxHisto==0)
00161             {
00162                fxHisto = new TH1D(GetName(),"histogram",2048,0,100);
00163             }
00164          else
00165             {
00166                // keep old histo
00167             }
00168          while(i++<1000)
00169             {
00170                x=gRandom->Gaus(50,7);
00171                fxHisto->Fill(x);
00172             }
00173          fxManager->UnBlockApp();
00174          if(strstr(GetName(),"Maker1"))
00175             {
00176                pad1->cd();
00177             }
00178          else
00179             {
00180                pad2->cd();
00181             }
00182 
00183          fxHisto->Draw();
00184          can->Modified();
00185          can->Update();
00186          TThread::CancelPoint();
00187          fxManager->BlockApp();
00188          TGo4Log::Debug("\t ''%s'' histogram filled, waiting", GetName());
00189          fxGo4Thread->Sleep(2000);
00190          TThread::CancelPoint(); // necessary?
00191          if(strstr(GetName(),"Maker1"))
00192             {
00193                TGo4Log::Debug("\t ''%s'' deleting histogram", GetName());
00194                delete fxHisto;
00195                fxHisto=0;
00196             }
00197          else
00198             {
00199                if((++t%5)==0)
00200                   {
00201                   //throw TGo4RemoveException(this);
00202 
00203 //                  throw TGo4ReplaceException(
00204 //                        this,
00205 //                        new TGo4TestRunnable("New HistogramMaker2",fxManager,4)
00206 //                        );
00207 
00208                   //throw TGo4ReplaceException(this);
00209 
00210 //                     cout << GetName()<<" ReCreating my thread:"<<endl;
00211 //                     //fxGo4Thread->ReCreate();
00212 //                  cout << GetName()<<" throwing exception now:"<<endl;
00213 //                  throw TGo4RestartException(this);
00214 
00215 //                  t=0;
00216                   }
00217             }
00218          }
00219          break;
00220       case 5:
00221          {
00222          TRACE((11,"TGo4TestRunnable::Run() mode 5",__LINE__, __FILE__));
00223          TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00224                   GetName(),fiMode);
00225          fxGo4Thread->Sleep(15000);
00226          TGo4Log::Debug("\t ''%s'' removing first thread", GetName());
00227          if(loops==0)
00228             {
00229                sprintf(buf,"First Thread");
00230             }
00231          go4thr=((TGo4ThreadHandler*) fxManager->GetWorkHandler())->GetThread("First Thread");
00232          ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->RemoveThread("First Thread");
00233 
00234          snprintf(buf,255,"NewHistogramMaker1:%d",loops++);
00235          //th1run= new TGo4TestRunnable(buf,fxManager,4);
00236          //sprintf(buf,"New First Thread:%d",loops++);
00237          if(loops==1)
00238             {
00239                // at first thread is internal
00240                TGo4Log::Debug("\t ''%s'' creating new first thread", GetName());
00241                 th1run= new TGo4TestRunnable(buf,fxManager,4);
00242                 go4thr= new TGo4Thread("First Thread",th1run,kFALSE); // first replacement: new thread
00243             }
00244          else
00245             {
00246                fxManager->UnBlockApp();
00247                go4thr->Cancel(); // need to cancel if not in internal mode
00248                fxManager->BlockApp();
00249             }
00250          TGo4Log::Debug("\t ''%s'' adding new first thread", GetName());
00251          ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->AddThread(go4thr);
00252          ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->Start("First Thread");
00253 //         ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->NewThread(buf,th1run);
00254 //         ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->Start(buf);
00255 
00256 
00257 //         cout << GetName()<<" end of work, stopping"<<endl;
00258 //         fxGo4Thread->Stop();
00259 //
00260 
00261 //         cout << GetName()<<" end of work, canceling"<<endl;
00262 //         fxGo4Thread->Cancel();
00263 
00264 //         throw TGo4CancelException(this);
00265          }
00266          break;
00267       case 6:
00268          // exception test:
00269          {
00270          TRACE((11,"TGo4TestRunnable::Run() mode 6",__LINE__, __FILE__));
00271          TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00272                   GetName(),fiMode);
00273          fxGo4Thread->Sleep(61000);
00274          TGo4Log::Debug("\t ''%s'' throwing exception now:", GetName());
00275          // restart of myself:
00276          //throw TGo4RestartException(this);
00277          //throw TGo4CancelException(this);
00278 
00279          // restart of other thread by name:
00280 //         throw TGo4ReplaceException(
00281 //            this,
00282 //            new TGo4TestRunnable("New HistogramMaker2",fxManager,4),
00283 //            "Second Thread"
00284 //            );
00285 //
00286          throw TGo4RestartException(this,"Second Thread");
00287          //throw TGo4RemoveException(this,"First Thread");
00288          //fxGo4Thread->Stop();
00289 
00290          // unexpected test:
00291          //throw 1;
00292          }
00293          break;
00294          case 7:
00295          {
00296          TRACE((11,"TGo4TestRunnable::Run() mode 7",__LINE__, __FILE__));
00297          TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
00298                   GetName(),fiMode);
00299          TGo4Log::Debug("\t ''%s'' creating histogram", GetName());
00300          if(fxHisto==0)
00301             {
00302                fxHisto = new TH1D(GetName(),"histogram",2048,0,100);
00303             }
00304          else
00305             {
00306                // keep old histo
00307             }
00308          while(i++<1000)
00309             {
00310                x=gRandom->Gaus(50,7);
00311                fxHisto->Fill(x);
00312             }
00313 //         pad2->cd();
00314 //         {
00315 //         TThread::Lock();
00316 //            fxHisto->Draw();
00317 //         TThread::UnLock();
00318 //         }
00319          TGo4Log::Debug("\t ''%s'' histogram %d filled, waiting", GetName(),loops);
00320          fxGo4Thread->Sleep(5000);
00321          TGo4Log::Debug("\t ''%s'' deleting histogram %d", GetName(),loops++);
00322          delete fxHisto;
00323          fxHisto=0;
00324          }
00325          break;
00326       default:
00327          {
00328          TRACE((11,"TGo4TestRunnable::Run() mode default",__LINE__, __FILE__));
00329          TGo4Log::Debug(" TestRunnable ''%s'' --  unspecified Workfunc action! ",
00330                   GetName(),fiMode);
00331          throw TGo4LogicException();
00332          }
00333          break;
00334    }
00335    return 0;
00336 
00337 }
00338 
00339 void TGo4TestRunnable::ThreadCatch (TGo4Exception& ex)
00340 {
00341 TRACE((12,"TGo4TestRunnable::ThreadCatch()",__LINE__, __FILE__));
00342 TGo4Runnable::ThreadCatch(ex); // we use parent method
00343 
00344 //   cout  << "TGo4TestRunnable "<< GetName() << ":ThreadCatch --"
00345 //   << endl<< "\t"
00346 //   << ex.What()
00347 //   << " occured in thread "<< fxGo4Thread->GetSelfID()
00348 //   << "\n\t\tcalling default exception handler..."
00349 //   << endl;
00350 //   ex.Handle();
00351 }
00352 
00353 void TGo4TestRunnable::UnexpectedCatch ()
00354 {
00355 TRACE((12,"TGo4TestRunnable::UnexpectedCatch()",__LINE__, __FILE__));
00356 
00357 TGo4Runnable::UnexpectedCatch();
00358 
00359 //   cout  << "TGo4TestRunnable "<< GetName() << ":UnexpectedCatch --"
00360 //         << endl<< "\t"
00361 //         << "aborting Application"<<endl;
00362 //   gApplication->Terminate(0);
00363 //
00364 }
00365 
00366 
00367 
00368 
00369 
00370 
00371 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Tue Nov 8 10:56:07 2005 for Go4-v2.10-5 by doxygen1.2.15