Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TGo4TestRunnable.cxx

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE 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 "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          // can we call methods of manager? aborting the whole thing?
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          //fxManager->Terminate(); // the direct approach
00139          throw TGo4TerminateException(this); //same, using a control exception
00140          }
00141          break;
00142       case 4:
00143          // memory alloc and histogram drawing test
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                // keep old histo
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(); // necessary?
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                   //throw TGo4RemoveException(this);
00192 
00193 //                  throw TGo4ReplaceException(
00194 //                        this,
00195 //                        new TGo4TestRunnable("New HistogramMaker2",fxManager,4)
00196 //                        );
00197 
00198                   //throw TGo4ReplaceException(this);
00199 
00200 //                     cout << GetName()<<" ReCreating my thread:"<<endl;
00201 //                     //fxGo4Thread->ReCreate();
00202 //                  cout << GetName()<<" throwing exception now:"<<endl;
00203 //                  throw TGo4RestartException(this);
00204 
00205 //                  t=0;
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          //th1run= new TGo4TestRunnable(buf,fxManager,4);
00226          //sprintf(buf,"New First Thread:%d",loops++);
00227          if(loops==1)
00228             {
00229                // at first thread is internal
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); // first replacement: new thread
00233             }
00234          else
00235             {
00236                fxManager->UnBlockApp();
00237                go4thr->Cancel(); // need to cancel if not in internal mode
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 //         ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->NewThread(buf,th1run);
00244 //         ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->Start(buf);
00245 
00246 
00247 //         cout << GetName()<<" end of work, stopping"<<endl;
00248 //         fxGo4Thread->Stop();
00249 //
00250 
00251 //         cout << GetName()<<" end of work, canceling"<<endl;
00252 //         fxGo4Thread->Cancel();
00253 
00254 //         throw TGo4CancelException(this);
00255          }
00256          break;
00257       case 6:
00258          // exception test:
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          // restart of myself:
00266          //throw TGo4RestartException(this);
00267          //throw TGo4CancelException(this);
00268 
00269          // restart of other thread by name:
00270 //         throw TGo4ReplaceException(
00271 //            this,
00272 //            new TGo4TestRunnable("New HistogramMaker2",fxManager,4),
00273 //            "Second Thread"
00274 //            );
00275 //
00276          throw TGo4RestartException(this,"Second Thread");
00277          //throw TGo4RemoveException(this,"First Thread");
00278          //fxGo4Thread->Stop();
00279 
00280          // unexpected test:
00281          //throw 1;
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                // keep old histo
00297             }
00298          while(i++<1000)
00299             {
00300                x=gRandom->Gaus(50,7);
00301                fxHisto->Fill(x);
00302             }
00303 //         pad2->cd();
00304 //         {
00305 //         TThread::Lock();
00306 //            fxHisto->Draw();
00307 //         TThread::UnLock();
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); // we use parent method
00333 
00334 //   cout  << "TGo4TestRunnable "<< GetName() << ":ThreadCatch --"
00335 //   << endl<< "\t"
00336 //   << ex.What()
00337 //   << " occured in thread "<< fxGo4Thread->GetSelfID()
00338 //   << "\n\t\tcalling default exception handler..."
00339 //   << endl;
00340 //   ex.Handle();
00341 }
00342 
00343 void TGo4TestRunnable::UnexpectedCatch ()
00344 {
00345 TRACE((12,"TGo4TestRunnable::UnexpectedCatch()",__LINE__, __FILE__));
00346 
00347 TGo4Runnable::UnexpectedCatch();
00348 
00349 //   cout  << "TGo4TestRunnable "<< GetName() << ":UnexpectedCatch --"
00350 //         << endl<< "\t"
00351 //         << "aborting Application"<<endl;
00352 //   gApplication->Terminate(0);
00353 //
00354 }
00355 
00356 
00357 
00358 
00359 
00360 
00361 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:31 2008 for Go4-v3.04-1 by  doxygen 1.4.2