GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4TestRunnable.cxx
Go to the documentation of this file.
1 // $Id: TGo4TestRunnable.cxx 999 2013-07-25 11:58:59Z linev $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4TestThreadManager.h"
15 
16 #include "TApplication.h"
17 #include "TH1.h"
18 #include "TRandom.h"
19 #include "TCanvas.h"
20 #include "TPad.h"
21 #include "TThread.h"
22 
23 #include "TGo4Log.h"
24 #include "TGo4Thread.h"
25 #include "TGo4ThreadHandler.h"
26 #include "TGo4TestRunnable.h"
27 
28 #include "TGo4TerminateException.h"
29 #include "TGo4RestartException.h"
30 #include "TGo4LogicException.h"
31 
33 :TGo4Runnable("dummy",0),fxPad(0),fxCanvas(0),fxHisto(0)
34 {
35  GO4TRACE((14,"TGo4TestRunnable::TGo4Runnable() constructor",__LINE__, __FILE__));
36 }
37 
39  :TGo4Runnable(right),fxPad(0),fxCanvas(0),fxHisto(0)
40 {
41  GO4TRACE((14,"TGo4TestRunnable::TGo4TestRunnable() copy ctor",__LINE__, __FILE__));
42  fiMode=right.fiMode;
43 }
44 
45 TGo4TestRunnable::TGo4TestRunnable(const char* name, TGo4ThreadManager* man, Int_t mode)
46  :TGo4Runnable(name,man),fxPad(0),fxCanvas(0),fxHisto(0)
47 {
48  GO4TRACE((14,"TGo4TestRunnable::TGo4TestRunnable(const char*,TGo4ThreadManager*,Int_t) constructor",__LINE__, __FILE__));
49  fiMode=mode;
50 }
51 
52 
54 {
55  GO4TRACE((14,"TGo4TestRunnable::~TGo4TestRunnable() destructor",__LINE__, __FILE__));
56 }
57 
58 Int_t TGo4TestRunnable::Run (void* ptr)
59 {
60  GO4TRACE((12,"TGo4TestRunnable::Run()",__LINE__, __FILE__));
61 
62  Int_t i=0;
63  static Int_t t=0;
64  static Int_t loops=0;
65  Axis_t x=0;
66  TGo4ThreadHandler* han=0;
67  TGo4TestRunnable* th1run=0;
68  TGo4Thread* go4thr=0;
69  TCanvas* can=((TGo4TestThreadManager*)fxManager)->GetCanvas();
70  TPad* pad1=((TGo4TestThreadManager*)fxManager)->GetPad1();
71  TPad* pad2=((TGo4TestThreadManager*)fxManager)->GetPad2();
72 
73  switch(fiMode)
74  {
75  case 0:
76  {
77  GO4TRACE((11,"TGo4TestRunnable::Run() mode 0",__LINE__, __FILE__));
78  TGo4Log::Debug(" TestRunnable ''%s'' running, testcase %d -- ",
79  GetName(),fiMode);
80  fxGo4Thread->Sleep(5000);
81  }
82  break;
83  case 1:
84  {
85  GO4TRACE((11,"TGo4TestRunnable::Run() mode 1",__LINE__, __FILE__));
86  TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
87  GetName(),fiMode);
88  fxGo4Thread->Sleep(30000);
89  TGo4Log::Debug("\t ''%s'' killing all other Threads",
90  GetName());
92  han->Cancel("First Thread");
93  han->Cancel("Second Thread");
94  han->Cancel("Third Thread");
95  han->Cancel("Fourth Thread");
96  fxGo4Thread->Sleep(5000);
97  TGo4Log::Debug("\t ''%s'' re-starting all other Threads", GetName());
98  han->Create("First Thread");
99  han->Create("Second Thread");
100  han->Create("Third Thread");
101  han->Create("Fourth Thread");
102  }
103  break;
104  case 2:
105  {
106  GO4TRACE((11,"TGo4TestRunnable::Run() mode 2",__LINE__, __FILE__));
107  TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
108  GetName(),fiMode);
109  fxGo4Thread->Sleep(10000);
110  TGo4Log::Debug("\t ''%s'' stopping all other Workfunctions:", GetName());
112  han->Stop("First Thread");
113  han->Stop("Second Thread");
114  han->Stop("Third Thread");
115  han->Stop("Fourth Thread");
116  fxGo4Thread->Sleep(10000);
117  TGo4Log::Debug("\t ''%s'' re-starting all other Workfunctions:", GetName());
118  han->Start("First Thread");
119  han->Start("Second Thread");
120  han->Start("Third Thread");
121  han->Start("Fourth Thread");
122  }
123  break;
124  case 3:
125  // can we call methods of manager? aborting the whole thing?
126  {
127  GO4TRACE((11,"TGo4TestRunnable::Run() mode 3",__LINE__, __FILE__));
128  TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
129  GetName(),fiMode);
130  fxGo4Thread->Sleep(30000);
131  TGo4Log::Debug("\t ''%s'' terminating ThreadManager", GetName());
132  //fxManager->Terminate(); // the direct approach
133  throw TGo4TerminateException(this); //same, using a control exception
134  }
135  break;
136  case 4:
137  // memory alloc and histogram drawing test
138  {
139  GO4TRACE((11,"TGo4TestRunnable::Run() mode 4",__LINE__, __FILE__));
140  TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
141  GetName(),fiMode);
142  fxGo4Thread->Sleep(2000);
143  TGo4Log::Debug("\t ''%s'' creating histogram", GetName());
144  if(fxHisto==0)
145  {
146  fxHisto = new TH1D(GetName(),"histogram",2048,0,100);
147  }
148  else
149  {
150  // keep old histo
151  }
152  while(i++<1000)
153  {
154  x=gRandom->Gaus(50,7);
155  fxHisto->Fill(x);
156  }
158  if(strstr(GetName(),"Maker1"))
159  {
160  pad1->cd();
161  }
162  else
163  {
164  pad2->cd();
165  }
166 
167  fxHisto->Draw();
168  can->Modified();
169  can->Update();
170  TThread::CancelPoint();
171  fxManager->BlockApp();
172  TGo4Log::Debug("\t ''%s'' histogram filled, waiting", GetName());
173  fxGo4Thread->Sleep(2000);
174  TThread::CancelPoint(); // necessary?
175  if(strstr(GetName(),"Maker1"))
176  {
177  TGo4Log::Debug("\t ''%s'' deleting histogram", GetName());
178  delete fxHisto;
179  fxHisto=0;
180  }
181  else
182  {
183  if((++t%5)==0)
184  {
185  //throw TGo4RemoveException(this);
186 
187 // throw TGo4ReplaceException(
188 // this,
189 // new TGo4TestRunnable("New HistogramMaker2",fxManager,4)
190 // );
191 
192  //throw TGo4ReplaceException(this);
193 
194 // std::cout << GetName()<<" ReCreating my thread:"<< std::endl;
195 // //fxGo4Thread->ReCreate();
196 // std::cout << GetName()<<" throwing exception now:"<< std::endl;
197 // throw TGo4RestartException(this);
198 
199 // t=0;
200  }
201  }
202  }
203  break;
204  case 5:
205  {
206  GO4TRACE((11,"TGo4TestRunnable::Run() mode 5",__LINE__, __FILE__));
207  TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
208  GetName(),fiMode);
209  fxGo4Thread->Sleep(15000);
210  TGo4Log::Debug("\t ''%s'' removing first thread", GetName());
211 
212  go4thr=((TGo4ThreadHandler*) fxManager->GetWorkHandler())->GetThread("First Thread");
213  ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->RemoveThread("First Thread");
214 
215  TString rname = TString::Format("NewHistogramMaker1:%d",loops++);
216  //th1run= new TGo4TestRunnable(buf,fxManager,4);
217  //sprintf(buf,"New First Thread:%d",loops++);
218  if(loops==1)
219  {
220  // at first thread is internal
221  TGo4Log::Debug("\t ''%s'' creating new first thread", GetName());
222  th1run= new TGo4TestRunnable(rname.Data(),fxManager,4);
223  go4thr= new TGo4Thread("First Thread",th1run,kFALSE); // first replacement: new thread
224  }
225  else
226  {
228  go4thr->Cancel(); // need to cancel if not in internal mode
229  fxManager->BlockApp();
230  }
231  TGo4Log::Debug("\t ''%s'' adding new first thread", GetName());
232  ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->AddThread(go4thr);
233  ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->Start("First Thread");
234 // ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->NewThread(buf,th1run);
235 // ((TGo4ThreadHandler*) fxManager->GetWorkHandler())->Start(buf);
236 
237 
238 // std::cout << GetName()<<" end of work, stopping"<< std::endl;
239 // fxGo4Thread->Stop();
240 //
241 
242 // std::cout << GetName()<<" end of work, canceling"<< std::endl;
243 // fxGo4Thread->Cancel();
244 
245 // throw TGo4CancelException(this);
246  }
247  break;
248  case 6:
249  // exception test:
250  {
251  GO4TRACE((11,"TGo4TestRunnable::Run() mode 6",__LINE__, __FILE__));
252  TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
253  GetName(),fiMode);
254  fxGo4Thread->Sleep(61000);
255  TGo4Log::Debug("\t ''%s'' throwing exception now:", GetName());
256  // restart of myself:
257  //throw TGo4RestartException(this);
258  //throw TGo4CancelException(this);
259 
260  // restart of other thread by name:
261 // throw TGo4ReplaceException(
262 // this,
263 // new TGo4TestRunnable("New HistogramMaker2",fxManager,4),
264 // "Second Thread"
265 // );
266 //
267  throw TGo4RestartException(this,"Second Thread");
268  //throw TGo4RemoveException(this,"First Thread");
269  //fxGo4Thread->Stop();
270 
271  // unexpected test:
272  //throw 1;
273  }
274  break;
275  case 7:
276  {
277  GO4TRACE((11,"TGo4TestRunnable::Run() mode 7",__LINE__, __FILE__));
278  TGo4Log::Debug(" ControlRunnable ''%s'' running, testcase %d -- ",
279  GetName(),fiMode);
280  TGo4Log::Debug("\t ''%s'' creating histogram", GetName());
281  if(fxHisto==0)
282  {
283  fxHisto = new TH1D(GetName(),"histogram",2048,0,100);
284  }
285  else
286  {
287  // keep old histo
288  }
289  while(i++<1000)
290  {
291  x=gRandom->Gaus(50,7);
292  fxHisto->Fill(x);
293  }
294 // pad2->cd();
295 // {
296 // TThread::Lock();
297 // fxHisto->Draw();
298 // TThread::UnLock();
299 // }
300  TGo4Log::Debug("\t ''%s'' histogram %d filled, waiting", GetName(),loops);
301  fxGo4Thread->Sleep(5000);
302  TGo4Log::Debug("\t ''%s'' deleting histogram %d", GetName(),loops++);
303  delete fxHisto;
304  fxHisto=0;
305  }
306  break;
307  default:
308  {
309  GO4TRACE((11,"TGo4TestRunnable::Run() mode default",__LINE__, __FILE__));
310  TGo4Log::Debug(" TestRunnable ''%s'' -- unspecified Workfunc action! ",
311  GetName(),fiMode);
312  throw TGo4LogicException();
313  }
314  break;
315  }
316  return 0;
317 
318 }
319 
321 {
322 GO4TRACE((12,"TGo4TestRunnable::ThreadCatch()",__LINE__, __FILE__));
323 TGo4Runnable::ThreadCatch(ex); // we use parent method
324 
325 // std::cout << "TGo4TestRunnable "<< GetName() << ":ThreadCatch --"
326 // << std::endl<< "\t"
327 // << ex.What()
328 // << " occured in thread "<< fxGo4Thread->GetSelfID()
329 // << "\n\t\tcalling default exception handler..."
330 // << std::endl;
331 // ex.Handle();
332 }
333 
335 {
336 GO4TRACE((12,"TGo4TestRunnable::UnexpectedCatch()",__LINE__, __FILE__));
337 
339 
340 // std::cout << "TGo4TestRunnable "<< GetName() << ":UnexpectedCatch --"
341 // << std::endl<< "\t"
342 // << "aborting Application"<< std::endl;
343 // gApplication->Terminate(0);
344 //
345 }
346 
347 
348 
349 
350 
virtual void ThreadCatch(TGo4Exception &ex)
TGo4ThreadManager * fxManager
Definition: TGo4Runnable.h:71
TGo4Thread * fxGo4Thread
Definition: TGo4Runnable.h:76
virtual ~TGo4TestRunnable()
TGo4Thread * GetThread() const
Definition: TGo4Runnable.h:45
Bool_t UnBlockApp(Int_t mode=0)
friend class TGo4Thread
Definition: TGo4Runnable.h:34
Bool_t Cancel()
Definition: TGo4Thread.cxx:187
static void Sleep(UInt_t millisecs)
Definition: TGo4Thread.cxx:336
virtual void UnexpectedCatch()
virtual Int_t Run(void *ptr)
virtual void UnexpectedCatch()
TGo4ThreadHandler * GetWorkHandler() const
Bool_t Create(const char *thname)
Bool_t Stop(const char *thname)
Bool_t Start(const char *thname)
Bool_t Cancel(const char *thname)
#define GO4TRACE(X)
Definition: TGo4Log.h:26
virtual void ThreadCatch(TGo4Exception &ex)
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:270