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