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