GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4ThreadManager.cxx
Go to the documentation of this file.
1 // $Id: TGo4ThreadManager.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 "TGo4ThreadManager.h"
15 
16 #include "TApplication.h"
17 #include "TSystem.h"
18 #include "TCondition.h"
19 
20 #include "TGo4Log.h"
21 #include "TGo4ThreadHandler.h"
22 #include "TGo4AppControlTimer.h"
23 
24 const Long_t TGo4ThreadManager::fglTIMERPERIOD = 200; // 2000
25 
27  :TNamed(right),
28  fbInitDone(kFALSE),
29  fbTerminating(kFALSE),
30  fbTerminateApplication(kFALSE)
31 {
32  GO4TRACE((15,"TGo4ThreadManager::TGo4ThreadManager copy ctor",__LINE__, __FILE__));
34  fxBlocker = right.fxBlocker;
35 }
36 
37 TGo4ThreadManager::TGo4ThreadManager (const char* name, Bool_t blockingmode, Bool_t autostart, Bool_t autocreate)
38  :TNamed(name,"This is a TGo4ThreadManager"),
39  fbInitDone(kFALSE),
40  fbTerminating(kFALSE),
41  fbTerminateApplication(kFALSE),
42  fbBeingQuit(kFALSE)
43 {
44  GO4TRACE((15,"TGo4ThreadManager::TGo4ThreadManager (const char*, Bool_t, Bool_t, Bool_t) constructor",__LINE__, __FILE__));
45 
46  TString myname=GetName();
47  myname+="-";
48  myname+=gSystem->HostName(); // add hostname
49  myname+="-";
50  myname+=gSystem->GetPid(); // add pid to threadmanager name
51  SetName(myname.Data()); // be unique for multiple client connections!
52  TString nomen("ThreadHandler of "); nomen+=GetName();
53  fxWorkHandler=new TGo4ThreadHandler(nomen.Data(), this);
55  fbAppBlocking=blockingmode;
56  fbAutoCreate=autocreate;
57  fbAutoStart=autostart;
58  if(fbAppBlocking)
59  // manager in blocking mode (analysis without gui)
60  {
61  TGo4Log::Debug(" ThreadManager -- Starting in application blocking mode ");
62  fxBlocker->SetApplicationRun(kFALSE);
63  }
64  else
65  // manager in non blocking mode (for gui)
66  {
67  TGo4Log::Debug(" ThreadManager -- Starting in application non-blocking mode ");
68  fxBlocker->SetApplicationRun(kTRUE);
69  }
70 }
71 
72 
74 {
75  GO4TRACE((15,"TGo4ThreadManager::~TGo4ThreadManager destructor",__LINE__, __FILE__));
76  delete fxWorkHandler; // this will cancel all threads and delete the internal instances
77  //delete fxBlocker; // dtor is called from fxBlocker, may not delete it!
78  //gApplication->Terminate(0);
79 }
80 
82 {
83  GO4TRACE((12,"TGo4ThreadManager::BlockApp()",__LINE__, __FILE__));
84  Bool_t rev=kFALSE;
85  if(fbAppBlocking)
86  {
87  if( !fbTerminating && !( fxWorkHandler->IsOperating() ) )
88  {
89  GO4TRACE((11,"TGo4ThreadManager::BlockApp() blocking mode",__LINE__, __FILE__));
91  rev=kTRUE;
92  }
93  else
94  {
95  // in case of Termination or threadhandler operation:
96  // do not block app again, may deadlock control timer
97  GO4TRACE((11,"TGo4ThreadManager::BlockApp() unblocking mode",__LINE__, __FILE__));
98  rev=kFALSE;
99  }
100  }
101  else
102  {
103  // manager is in non blocking mode (enables gui callback operations)
104  rev=kFALSE;
105  }
106  return rev;
107 }
108 
109 Bool_t TGo4ThreadManager::UnBlockApp (Int_t mode)
110 {
111  GO4TRACE((12,"TGo4ThreadManager::UnBlockApp()",__LINE__, __FILE__));
112  Bool_t rev=kFALSE;
113  switch(mode)
114  {
115  case 0:
116  {
117  GO4TRACE((11,"TGo4ThreadManager::UnBlockApp() mode 0",__LINE__, __FILE__));
119  // only send condition if timer is really waiting
120  {
121 
123  ((TCondition*) fxBlocker->GetCondition() )->Signal();
124  }
125  rev=kTRUE;
126  }
127  break;
128  case 1:
129  {
130  GO4TRACE((11,"TGo4ThreadManager::UnBlockApp() mode 1",__LINE__, __FILE__));
132  rev=kTRUE;
133  }
134  break;
135  case 2:
136  {
137  GO4TRACE((11,"TGo4ThreadManager::UnBlockApp() mode 2",__LINE__, __FILE__));
139  // only send condition if timer is really waiting
140  {
141  ((TCondition*) fxBlocker->GetCondition() )->Signal();
142  }
143  rev=kTRUE;
144  }
145  break;
146  default:
147  {
148  GO4TRACE((16,"++TGo4ThreadManager::UnBlockApp() unknown mode"));
149  //std::cerr << "TGo4ThreadManager::UnBlockApp() unknown mode"<< std::endl;
150  rev=kFALSE;
151  }
152  break;
153  } // switch(mode)
154  return rev;
155 }
156 
158 {
159  GO4TRACE((12,"TGo4ThreadManager::Initialization()",__LINE__, __FILE__));
160  if(fbInitDone)
161  // already initialized, return ok value
162  {
163  GO4TRACE((11,"TGo4ThreadManager::Initialization()--already init done, returning",__LINE__, __FILE__));
164  //std::cout << "already init done, returning"<< std::endl;
165  return 0;
166  }
167  else
168  // first call of initialization: check threads
169  {
170  if(fbAutoCreate)
171  // auto thread creation mode: wait for all threads being up
172  {
173  GO4TRACE((11,"TGo4ThreadManager::Initialization()--in AutoCreate mode",__LINE__, __FILE__));
174  if( fxWorkHandler->AllCreated() )
175  // test for threads, block timer and start work if they are up
176  {
177  TGo4Log::Debug(" ThreadManager -- All threads are up, writing dump file ");
178  BlockApp();
180  if(fbAutoStart)
181  {
182  // autostart mode of runnables
183  GO4TRACE((11,"TGo4ThreadManager::Initialization()--in AutoStart mode",__LINE__, __FILE__));
185  }
186  else
187  {
188  GO4TRACE((11,"TGo4ThreadManager::Initialization()--in non-AutoStart mode",__LINE__, __FILE__));
189 
190  // do not start runnables
191  }
192 
193  fbInitDone=kTRUE;
194  return 0;
195  }
196  else
197  // some threads are missing, suspend starting until next timer cycle
198  {
199  TGo4Log::Debug(" ThreadManager -- some threads are missing, re-doing Init ");
200 
201  //std::cout <<"TGo4ThreadManager waiting for threads"<< std::endl;
202  return 1;
203  }
204  }
205  else
206  {
207  // do not check or dump threads
208  GO4TRACE((11,"TGo4ThreadManager::Initialization()--not in AutoCreate mode",__LINE__, __FILE__));
209  fbInitDone=kTRUE;
210  return 0;
211  }
212  }
213 }
214 
216 {
217  GO4TRACE((15,"TGo4ThreadManager::Launch()",__LINE__, __FILE__));
218  if(fbAutoCreate)
219  {
220  // create all TThreads of TGo4Threads in threadhandler list
221  GO4TRACE((13,"TGo4ThreadManager::Launch()-- executing AutoCreate mode",__LINE__, __FILE__));
223  }
224  else
225  {
226  // do not create TThreads
227  GO4TRACE((13,"TGo4ThreadManager::Launch()-- no AutoCreate mode",__LINE__, __FILE__));
228  }
229  fxBlocker->TurnOn(); // later in method which is called at the end of derived ctor?
230 }
231 
232 void TGo4ThreadManager::Terminate (Bool_t termapp)
233 {
234  GO4TRACE((15,"TGo4ThreadManager::Terminate()",__LINE__, __FILE__));
235  TGo4Log::Debug(" ThreadManager -- Preparing Termination... ");
237  //gSystem->Sleep(10000); // wait for workfunc to return
238  fbTerminating=kTRUE;
239  fbTerminateApplication=termapp;
240  {
241  GO4TRACE((13,"TGo4ThreadManager::Terminate()--waking up timer:",__LINE__, __FILE__));
242  UnBlockApp(); // wake up blocking timer
243  }
244 }
245 
247 {
248  Terminate();
249 }
250 
virtual Int_t Initialization()
virtual void TerminateFast()
Bool_t IsOperating() const
TGo4ThreadHandler * fxWorkHandler
TGo4AppControlTimer * fxBlocker
Bool_t UnBlockApp(Int_t mode=0)
Int_t DumpThreads(Int_t mode=0)
const TCondition * GetCondition() const
#define GO4TRACE(X)
Definition: TGo4Log.h:26
void SetApplicationRun(Bool_t flag=kTRUE)
virtual void Terminate(Bool_t termapp=kTRUE)
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:270
static const Long_t fglTIMERPERIOD