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