GSI Object Oriented Online Offline (Go4)  GO4-6.1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4AppControlTimer.cxx
Go to the documentation of this file.
1 // $Id: TGo4AppControlTimer.cxx 2712 2020-02-28 07:29:46Z 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 "TGo4AppControlTimer.h"
15 
16 #include "TCondition.h"
17 #include "TApplication.h"
18 #include "TThread.h"
19 #include "TSystem.h"
20 
21 #include "TGo4Log.h"
22 #include "TGo4ThreadManager.h"
23 #include "TGo4ThreadHandler.h"
24 
26 
28 
29 TGo4AppControlTimer::TGo4AppControlTimer (TGo4ThreadManager* manager, Long_t msperiod, Bool_t mode)
30  :TTimer(msperiod,mode),
31  fxManager(0),
32  fbApplicationRun(kFALSE)
33 {
34  GO4TRACE((15,"TGo4AppControlTimer::TGo4AppControlTimer(TGo4ThreadManager*, Long_t, Bool_t) ctor",__LINE__, __FILE__));
35  fxManager=manager;
36  fxCondition=new TCondition();
37 }
38 
39 
41 {
42 GO4TRACE((15,"TGo4AppControlTimer::~TGo4AppControlTimer() dtor",__LINE__, __FILE__));
43 //std::cout <<"!!!!!!!!!!!!!!! Destructor of TGo4AppControlTimer" << std::endl;
44  if(fxCondition)
45  {
46  delete fxCondition;
47  fxCondition=0;
48  }
49 }
50 
52 {
53  //GO4TRACE((12,"TGo4AppControlTimer::Notify()",__LINE__, __FILE__));
54 
55 // TurnOff();
56  if(fxManager==0)
57  {
58  TGo4Log::Error("TGo4AppControlTimer::Notify () NEVER COME HERE!!!");
59  TGo4Log::Error("TGo4AppControlTimer::Notify () with zero threadmanager!!!");
60  return kFALSE;
61 
62  }
63  if(fxManager->Initialization()==0)
64  // init successful?
65  {
66  GO4TRACE((11,"TGo4AppControlTimer: Successful Initialization",__LINE__, __FILE__));
67  if(!fbApplicationRun)
68  // root TApplication shall be blocked
69  {
70  GO4TRACE((11,"TGo4AppControlTimer: Application shall be blocked!",__LINE__, __FILE__));
71  if(TThread::Exists())
72  // necessary to avoid gThreadImp bug in condition wait...
73  {
74  // normal mode:
75  GO4TRACE((10,"TGo4AppControlTimer: blocking ROOT gSystem!",__LINE__, __FILE__));
76  TGo4Log::Debug(" Application Control Timer -- Blocking ROOT gSystem!! ");
77  fxCondition->Wait(); // wait for running signal
78  TGo4Log::Debug(" Application Control Timer -- Releasing ROOT gSystem!! ");
79  }
80  else
81  {
82  // no thread up, cannot wait for condition
83  GO4TRACE((10,"TGo4AppControlTimer: no TThread existing, not blocking !",__LINE__, __FILE__));
84  }
85  }
86  else
87  {
88  // do not block :)
89  GO4TRACE((11,"TGo4AppControlTimer: Application shall not be blocked!",__LINE__, __FILE__));
90  }
91 
93  // check if Timer was woken for program end
94  {
95  GO4TRACE((11,"TGo4AppControlTimer: Terminating mode",__LINE__, __FILE__));
96  Int_t t=0;
97  while(!(fxManager->GetWorkHandler()->AllWaiting()) && (t++<fgiTERMWAITCYCLES) )
98  {
99  TGo4Log::Debug(" Application Control Timer -- waiting for Runnables to stop... ");
100  //std::cout << "TGo4AppControlTimer waiting for Runnables to stop..."<<t<< std::endl;
101  gSystem->Sleep(fguTERMWAIT);
103  }
104  TGo4Log::Debug(" Application Control Timer -- deleting TGo4ThreadManager! ");
105  Bool_t pleaseterminate=fxManager->IsTerminateApp();
106  delete fxManager; // executes dtors
107  fxManager=0;
108  TurnOff(); // do not call this Notify again in Terminate
109  if(pleaseterminate)
110  {
111  TGo4Log::Debug(" Application Control Timer -- Terminating Application now! Goodbye...");
112  gApplication->Terminate(0);
113  }
114  else
115  {
116  TGo4Log::Debug(" Application Control Timer -- Finished Threadmanager deletion, killing myself now...");
117  delete this;
118  }
119  return kFALSE;
120  } // if(pleaseterminate)
121  else
122  {
123  // prepare for another timer cycle
124  GO4TRACE((11,"TGo4AppControlTimer: Resetting for new cycle",__LINE__, __FILE__));
125  Reset();
126  // TurnOn();
127  }
128  } //if(fxManager->Initialization()==0)
129  else
130  // init not successful
131  {
132  GO4TRACE((11,"TGo4AppControlTimer: Initialization failed",__LINE__, __FILE__));
133  TGo4Log::Debug(" Application Control Timer -- ThreadManager Init not successful, retrying! ");
134  Reset();
135 // TurnOn();
136  }
137  return kFALSE;
138 }
139 
141 {
142  GO4TRACE((12,"TGo4AppControlTimer::GetApplicationRun ()",__LINE__, __FILE__));
143  return fbApplicationRun;
144 }
145 
146 
147 
148 
virtual Int_t Initialization()
Bool_t IsTerminateApp() const
static Int_t fgiTERMWAITCYCLES
TGo4ThreadManager * fxManager
TGo4ThreadHandler * GetWorkHandler() const
#define GO4TRACE(X)
Definition: TGo4Log.h:26
Bool_t IsTerminating() const
static void Error(const char *text,...)
Definition: TGo4Log.cxx:313
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:274