00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TGo4ThreadHandler.h"
00017
00018 #include <iostream.h>
00019
00020 #include "TGo4ThreadManager.h"
00021 #include "Go4Log/TGo4Log.h"
00022 #include "Go4LockGuard/TGo4LockGuard.h"
00023
00024 TGo4ThreadHandler::TGo4ThreadHandler(const TGo4ThreadHandler &right)
00025 : TNamed(right)
00026 {
00027 TRACE((15,"TGo4ThreadHandler::TGo4ThreadHandler() copy constructor",__LINE__, __FILE__));
00028 fxManager = right.fxManager;
00029 fxListMutex=new TMutex;
00030 fxOperMutex=new TMutex;
00031 fxArray= (TObjArray*) ((right.fxArray)->Clone());
00032 fxIterator=fxArray->MakeIterator();
00033 }
00034
00035 TGo4ThreadHandler::TGo4ThreadHandler (const char* name, TGo4ThreadManager* parent)
00036 :TNamed(name,"This is a TGo4ThreadHandler"),fbIsOperating(kFALSE)
00037 {
00038 TRACE((15,"TGo4ThreadHandler::TGo4ThreadHandler(Text_t*, TGo4ThreadManager*) constructor",__LINE__, __FILE__));
00039 fxManager=parent;
00040 fxListMutex=new TMutex;
00041 fxOperMutex=new TMutex;
00042 fxArray=new TObjArray;
00043 fxIterator=fxArray->MakeIterator();
00044 }
00045
00046
00047 TGo4ThreadHandler::~TGo4ThreadHandler()
00048 {
00049 TRACE((15,"TGo4ThreadHandler::~TGo4ThreadHandler() destructor",__LINE__, __FILE__));
00050 StopAll();
00051 CancelAll();
00052 TGo4Thread* th=0;
00053 {
00054 TGo4LockGuard listguard(fxListMutex);
00055 fxIterator->Reset();
00056 while((th= (TGo4Thread*) fxIterator->Next())!=0)
00057 {
00058 if(th->IsInternal())
00059
00060 {
00061 TRACE((14,"TGo4ThreadHandler::~TGo4ThreadHandler() Removing internal mode thread",__LINE__, __FILE__));
00062 fxArray->Remove(th);
00063 fxArray->Compress();
00064 fxArray->Expand(fxArray->GetLast()+1);
00065 delete th;
00066 }
00067 else
00068 {
00069
00070 TRACE((14,"TGo4ThreadHandler::~TGo4ThreadHandler() Non internal mode thread",__LINE__, __FILE__));
00071 }
00072 }
00073 delete fxIterator;
00074 fxArray->Clear();
00075 delete fxArray;
00076 }
00077 delete fxOperMutex;
00078 delete fxListMutex;
00079 }
00080
00081 Bool_t TGo4ThreadHandler::AddThread (TGo4Thread* gthr)
00082 {
00083 TRACE((14,"TGo4ThreadHandler::AddThread(TGo4Thread*)",__LINE__, __FILE__));
00084 Bool_t rev=kFALSE;
00085 {
00086 TGo4LockGuard listguard(fxListMutex);
00087 if(fxArray->FindObject(gthr)==0)
00088
00089 {
00090
00091 TRACE((13,"TGo4ThreadHandler::AddThread(TGo4Thread*) Adding new go4 thread to array",__LINE__, __FILE__));
00092 fxArray->AddLast(gthr);
00093 rev=kTRUE;
00094 }
00095 else
00096 {
00097
00098 TRACE((13,"TGo4ThreadHandler::AddThread(TGo4Thread*) Thread was already in array",__LINE__, __FILE__));
00099 rev=kFALSE;
00100 }
00101 }
00102 return rev;
00103 }
00104
00105 Bool_t TGo4ThreadHandler::RemoveThread (const char* name)
00106 {
00107 TRACE((14,"TGo4ThreadHandler::RemoveThread(Text_t*)",__LINE__, __FILE__));
00108 Bool_t rev=kFALSE;
00109 {
00110 TGo4LockGuard listguard(fxListMutex);
00111 TObject* obj=fxArray->FindObject(name);
00112 TGo4Thread* th= (TGo4Thread*) obj;
00113 if(obj!=0)
00114
00115 {
00116 TRACE((13,"TGo4ThreadHandler::RemoveThread(Text_t*) Removing thread from array",__LINE__, __FILE__));
00117 fxArray->Remove(obj);
00118 fxArray->Compress();
00119 fxArray->Expand(fxArray->GetLast()+1);
00120 if(th->IsInternal())
00121
00122 {
00123 TRACE((13,"TGo4ThreadHandler::RemoveThread(Text_t*) Deleting internal mode thread",__LINE__, __FILE__));
00124 TGo4LockGuard operguard(fxOperMutex);
00125 fbIsOperating=kTRUE;
00126 fxManager->UnBlockApp();
00127 delete th;
00128 fbIsOperating=kFALSE;
00129 fxManager->BlockApp();
00130 }
00131 else
00132 {
00133
00134 TRACE((13,"TGo4ThreadHandler::RemoveThread(Text_t*) Non internal mode thread is not deleted",__LINE__, __FILE__));
00135 }
00136 rev=kTRUE;
00137 }
00138 else
00139 {
00140 TRACE((13,"TGo4ThreadHandler::RemoveThread(Text_t*) Thread not found in array",__LINE__, __FILE__));
00141 rev=kFALSE;
00142 }
00143 }
00144 return rev;
00145 }
00146
00147 Bool_t TGo4ThreadHandler::NewThread(const char* name, TGo4Runnable* runnable)
00148 {
00149 TRACE((14,"TGo4ThreadHandler::NewThread(const char*,TGo4Runnable*)",__LINE__, __FILE__));
00150 TGo4Runnable* nrun=0;
00151 TGo4Thread* nthread=0;
00152 if(runnable==0)
00153 {
00154 TRACE((13,"TGo4ThreadHandler::NewThread(const char*,TGo4Runnable*) No runnable specified error",__LINE__, __FILE__));
00155 return kFALSE;
00156
00157 }
00158 else
00159 {
00160 TRACE((13,"TGo4ThreadHandler::NewThread(const char*,TGo4Runnable*) Assigning external runnable to new internal thread",__LINE__, __FILE__));
00161 nrun=runnable;
00162 }
00163 nthread= new TGo4Thread(name,nrun,kTRUE);
00164 return AddThread(nthread);
00165 }
00166
00167 Int_t TGo4ThreadHandler::CreateAll ()
00168 {
00169 TRACE((15,"TGo4ThreadHandler::CreateAll()",__LINE__, __FILE__));
00170 Int_t createdthreads=0;
00171 TGo4Thread* th=0;
00172 {
00173 TGo4LockGuard listguard(fxListMutex);
00174 TGo4LockGuard operguard(fxOperMutex);
00175 fbIsOperating=kTRUE;
00176 fxManager->UnBlockApp();
00177 fxIterator->Reset();
00178 while((th= (TGo4Thread*) fxIterator->Next())!=0)
00179 {
00180 if(th->Create())
00181 {
00182 TRACE((13,"TGo4ThreadHandler::CreateAll() Thread creation success",__LINE__, __FILE__));
00183 ++createdthreads;
00184 }
00185 else
00186 {
00187 TRACE((13,"TGo4ThreadHandler::CreateAll() Thread not created",__LINE__, __FILE__));
00188
00189 }
00190 }
00191 fxManager->BlockApp();
00192 fbIsOperating=kFALSE;
00193 }
00194 return createdthreads;
00195 }
00196
00197 Bool_t TGo4ThreadHandler::Create (const char* thname)
00198 {
00199 TRACE((14,"TGo4ThreadHandler::Create(Text_t*)",__LINE__, __FILE__));
00200 Bool_t rev=kFALSE;
00201 TGo4Thread* th=GetThread(thname);
00202 if(th!=0)
00203
00204 {
00205 TRACE((13,"TGo4ThreadHandler::Create(Text_t*) Creating new TThread for Go4Thread",__LINE__, __FILE__));
00206 TGo4LockGuard operguard(fxOperMutex);
00207 fbIsOperating=kTRUE;
00208 fxManager->UnBlockApp();
00209 rev=th->Create();
00210 fbIsOperating=kFALSE;
00211 fxManager->BlockApp();
00212 }
00213 else
00214
00215 {
00216 TRACE((13,"TGo4ThreadHandler::Create(Text_t*) Go4Thread was not found in thread array!",__LINE__, __FILE__));
00217 rev=kFALSE;
00218 }
00219 return rev;
00220 }
00221
00222 Int_t TGo4ThreadHandler::CancelAll ()
00223 {
00224 TRACE((15,"TGo4ThreadHandler::CancelAll()",__LINE__, __FILE__));
00225 Int_t cancelledthreads=0;
00226 TGo4Thread* th=0;
00227 {
00228 TGo4LockGuard listguard(fxListMutex);
00229 TGo4LockGuard operguard(fxOperMutex);
00230 fbIsOperating=kTRUE;
00231 fxIterator->Reset();
00232 while((th= (TGo4Thread*) fxIterator->Next())!=0)
00233 {
00234 if(th->Cancel())
00235 {
00236 TRACE((13,"TGo4ThreadHandler::CancelAll() Thread Cancel success",__LINE__, __FILE__));
00237 ++cancelledthreads;
00238 }
00239 else
00240 {
00241
00242 TRACE((13,"TGo4ThreadHandler::CancelAll() Thread was not canceled",__LINE__, __FILE__));
00243
00244 }
00245 }
00246 fbIsOperating=kFALSE;
00247 }
00248 return cancelledthreads;
00249 }
00250
00251 Bool_t TGo4ThreadHandler::Cancel (const char* thname)
00252 {
00253 TRACE((14,"TGo4ThreadHandler::Cancel(Text_t*)",__LINE__, __FILE__));
00254 Bool_t rev=kFALSE;
00255 TGo4Thread* th=GetThread(thname);
00256 if(th!=0)
00257
00258 {
00259 TRACE((13,"TGo4ThreadHandler::Cancel(Text_t*) Canceling TThread",__LINE__, __FILE__));
00260 TGo4LockGuard operguard(fxOperMutex);
00261 fbIsOperating=kTRUE;
00262 fxManager->UnBlockApp();
00263 rev=th->Cancel();
00264 fbIsOperating=kFALSE;
00265 fxManager->BlockApp();
00266 }
00267 else
00268
00269 {
00270 TRACE((13,"TGo4ThreadHandler::Cancel(Text_t*) Go4Thread was not found in thread array!",__LINE__, __FILE__));
00271 rev=kFALSE;
00272 }
00273 return rev;
00274 }
00275
00276 Int_t TGo4ThreadHandler::ReCreateAll ()
00277 {
00278 TRACE((15,"TGo4ThreadHandler::ReCreateAll()",__LINE__, __FILE__));
00279 Int_t recreatedthreads=0;
00280 TGo4Thread* th=0;
00281 {
00282 TGo4LockGuard listguard(fxListMutex);
00283 TGo4LockGuard operguard(fxOperMutex);
00284 fbIsOperating=kTRUE;
00285 fxIterator->Reset();
00286 while((th= (TGo4Thread*) fxIterator->Next())!=0)
00287 {
00288 if(th->ReCreate())
00289 {
00290 TRACE((13,"TGo4ThreadHandler::ReCreateAll() Thread ReCreate success",__LINE__, __FILE__));
00291 ++recreatedthreads;
00292 }
00293 else
00294 {
00295
00296 TRACE((13,"TGo4ThreadHandler::ReCreateAll() Thread was not recreated",__LINE__, __FILE__));
00297
00298 }
00299 }
00300 fbIsOperating=kFALSE;
00301 }
00302 return recreatedthreads;
00303 }
00304
00305 Bool_t TGo4ThreadHandler::ReCreate (const char* thname)
00306 {
00307 TRACE((14,"TGo4ThreadHandler::ReCreate(Text_t*)",__LINE__, __FILE__));
00308 Bool_t rev=kFALSE;
00309 TGo4Thread* th=GetThread(thname);
00310 if(th!=0)
00311
00312 {
00313 TRACE((13,"TGo4ThreadHandler::ReCreate(Text_t*) ReCreating TThread for Go4Thread",__LINE__, __FILE__));
00314 TGo4LockGuard operguard(fxOperMutex);
00315 fbIsOperating=kTRUE;
00316 fxManager->UnBlockApp();
00317 rev=th->ReCreate();
00318 fbIsOperating=kFALSE;
00319 fxManager->BlockApp();
00320 }
00321 else
00322
00323 {
00324 TRACE((13,"TGo4ThreadHandler::ReCreate(Text_t*) Go4Thread was not found in thread array!",__LINE__, __FILE__));
00325 rev=kFALSE;
00326 }
00327
00328 return rev;
00329 }
00330
00331 Int_t TGo4ThreadHandler::StartAll ()
00332 {
00333 TRACE((15,"TGo4ThreadHandler::StartAll()",__LINE__, __FILE__));
00334 Int_t startedfuncs=0;
00335 TGo4Thread* th=0;
00336 {
00337 TGo4LockGuard listguard(fxListMutex);
00338 TGo4LockGuard operguard(fxOperMutex);
00339 fbIsOperating=kTRUE;
00340 fxManager->UnBlockApp();
00341 fxIterator->Reset();
00342 while((th= (TGo4Thread*) fxIterator->Next())!=0)
00343 {
00344 if(!th->Start())
00345
00346
00347 {
00348
00349 TRACE((13,"TGo4ThreadHandler::StartAll() Thread Start success",__LINE__, __FILE__));
00350 ++startedfuncs;
00351 }
00352 else
00353 {
00354
00355
00356 TRACE((13,"TGo4ThreadHandler::StartAll() Thread was already running",__LINE__, __FILE__));
00357 }
00358 }
00359 fbIsOperating=kFALSE;
00360 fxManager->BlockApp();
00361 }
00362 return startedfuncs;
00363 }
00364
00365 Bool_t TGo4ThreadHandler::Start (const char* thname)
00366 {
00367 TRACE((14,"TGo4ThreadHandler::Start(const char*)",__LINE__, __FILE__));
00368 Bool_t rev=kFALSE;
00369 TGo4Thread* th=GetThread(thname);
00370 if(th!=0)
00371
00372 {
00373 TRACE((13,"TGo4ThreadHandler::Start(const char*) Starting Thread",__LINE__, __FILE__));
00374 TGo4LockGuard operguard(fxOperMutex);
00375 fbIsOperating=kTRUE;
00376 fxManager->UnBlockApp();
00377 rev=th->Start();
00378 fbIsOperating=kFALSE;
00379 fxManager->BlockApp();
00380 }
00381 else
00382
00383 {
00384 TRACE((13,"TGo4ThreadHandler::Start(Text_t*) Go4Thread was not found in thread array!",__LINE__, __FILE__));
00385 rev=kFALSE;
00386 }
00387 return rev;
00388 }
00389
00390 Int_t TGo4ThreadHandler::StopAll ()
00391 {
00392 TRACE((15,"TGo4ThreadHandler::StopAll()",__LINE__, __FILE__));
00393 Int_t stoppedfuncs=0;
00394 TGo4Thread* th=0;
00395 {
00396 TGo4LockGuard listguard(fxListMutex);
00397 fxIterator->Reset();
00398 while((th= (TGo4Thread*) fxIterator->Next())!=0)
00399 {
00400 if(th->Stop())
00401
00402 {
00403
00404 TRACE((13,"TGo4ThreadHandler::StopAll() Go4Thread Stop success",__LINE__, __FILE__));
00405 ++stoppedfuncs;
00406 }
00407 else
00408 {
00409
00410
00411 TRACE((13,"TGo4ThreadHandler::StopAll() Go4Thread was already stopped",__LINE__, __FILE__));
00412 }
00413 }
00414 }
00415 return stoppedfuncs;
00416 }
00417
00418 Bool_t TGo4ThreadHandler::Stop (const char* thname)
00419 {
00420 TRACE((14,"TGo4ThreadHandler::Stop(const char*)",__LINE__, __FILE__));
00421 Bool_t rev=kFALSE;
00422 TGo4Thread* th=GetThread(thname);
00423 if(th!=0)
00424
00425 {
00426 TRACE((13,"TGo4ThreadHandler::Stop(const char*) Stopping Go4Runnable",__LINE__, __FILE__));
00427 rev=th->Stop();
00428 }
00429 else
00430
00431 {
00432 TRACE((13,"TGo4ThreadHandler::Stop(const char*) Go4Thread was not found in thread array!",__LINE__, __FILE__));
00433 rev=kFALSE;
00434 }
00435 return rev;
00436 }
00437
00438 Int_t TGo4ThreadHandler::DumpThreads (Int_t mode)
00439 {
00440 TRACE((15,"TGo4ThreadHandler::DumpThreads(Int_t)",__LINE__, __FILE__));
00441 if(TGo4Log::GetIgnoreLevel()>0) return 2;
00442 Int_t retval=0;
00443 TGo4Thread* th=0;
00444 FILE* fp;
00445 Int_t i=0;
00446 char Filename[80];
00447 switch(mode)
00448 {
00449 case 0:
00450 {
00451 TRACE((14,"TGo4ThreadHandler::DumpThreads(Int_t) mode 0: File output",__LINE__, __FILE__));
00452 strcpy(Filename,"threaddump.txt");
00453 fp = fopen(Filename,"w");
00454 if (!fp)
00455 {
00456 TRACE((13,"TGo4ThreadHandler::DumpThreads(Int_t) fopen failed!!",__LINE__, __FILE__));
00457 TGo4Log::Debug(" ThreadHandler -- Error, Could not open thread dump file!");
00458 retval=1;
00459 break;
00460 }
00461 else
00462 {
00463 TRACE((13,"TGo4ThreadHandler::DumpThreads(Int_t) writing into opened file",__LINE__, __FILE__));
00464 fprintf(fp,"\nTGo4ThreadHandler thread information dump file:\n");
00465 {
00466 TGo4LockGuard listguard(fxListMutex);
00467 fxIterator->Reset();
00468 while((th= (TGo4Thread*) fxIterator->Next())!=0)
00469 {
00470 fprintf(fp,"TGo4Thread %d: \tPID:%d \tSelfID: %d",
00471 i++,th->GetPID(),(int) th->GetSelfID());
00472 fprintf(fp,"\t name: %s\n",th->GetName());
00473 }
00474 }
00475 if (fclose(fp))
00476 {
00477 TRACE((13,"TGo4ThreadHandler::DumpThreads(Int_t) fclose failed!!",__LINE__, __FILE__));
00478 TGo4Log::Debug(" ThreadHandlerr -- Error, Could not close thread dump file!");
00479 retval=1;
00480 }
00481 }
00482 }
00483 break;
00484 default:
00485 {
00486 TRACE((14,"TGo4ThreadHandler::DumpThreads(Int_t) mode: default ",__LINE__, __FILE__));
00487 TGo4Log::Debug(" ThreadHandlerr -- Error: DumpThreads mode unknown ");
00488 }
00489 break;
00490 }
00491 return retval;
00492 }
00493
00494 TGo4Thread* TGo4ThreadHandler::GetThread (const char* name)
00495 {
00496 TRACE((12,"TGo4ThreadHandler::GetThread(const char*)",__LINE__, __FILE__));
00497 TGo4Thread* thread=0;
00498 {
00499 TGo4LockGuard listguard(fxListMutex);
00500 thread = (TGo4Thread*) fxArray->FindObject(name);
00501 }
00502 return thread;
00503 }
00504
00505 Int_t TGo4ThreadHandler::GetEntries ()
00506 {
00507 TRACE((12,"TGo4ThreadHandler::GetEntries()",__LINE__, __FILE__));
00508 Int_t entries=0;
00509 {
00510 TGo4LockGuard listguard(fxListMutex);
00511 entries=fxArray->GetEntries();
00512 }
00513 return entries;
00514 }
00515
00516 Bool_t TGo4ThreadHandler::AllCreated ()
00517 {
00518 TRACE((14,"TGo4ThreadHandler::AllCreated()",__LINE__, __FILE__));
00519 Bool_t rev=kTRUE;
00520 TGo4Thread* th=0;
00521 {
00522 TGo4LockGuard listguard(fxListMutex);
00523 fxIterator->Reset();
00524 while((th= (TGo4Thread*) fxIterator->Next())!=0)
00525 {
00526 if((th->GetPID())==0)
00527 {
00528 TRACE((11,"TGo4ThreadHandler::AllCreated() TThread is _not_ existing",__LINE__, __FILE__));
00529 rev=kFALSE;
00530 break;
00531 }
00532 else
00533 {
00534 TRACE((11,"TGo4ThreadHandler::AllCreated() TThread is existing",__LINE__, __FILE__));
00535
00536 }
00537 }
00538 }
00539 return rev;
00540 }
00541
00542 Bool_t TGo4ThreadHandler::AllRunning ()
00543 {
00544 TRACE((14,"TGo4ThreadHandler::AllRunning()",__LINE__, __FILE__));
00545 Bool_t rev=kTRUE;
00546 TGo4Thread* th=0;
00547 {
00548 TGo4LockGuard listguard(fxListMutex);
00549 fxIterator->Reset();
00550 while((th= (TGo4Thread*) fxIterator->Next())!=0)
00551 {
00552 if(!(th->IsRunning()))
00553 {
00554 TRACE((11,"TGo4ThreadHandler::AllCreated() TGo4Thread is _not_ running",__LINE__, __FILE__));
00555 rev=kFALSE;
00556 break;
00557 }
00558 else
00559 {
00560 TRACE((11,"TGo4ThreadHandler::AllCreated() TGo4Thread is running",__LINE__, __FILE__));
00561
00562 }
00563 }
00564 }
00565 return rev;
00566 }
00567
00568 Bool_t TGo4ThreadHandler::AllWaiting ()
00569 {
00570 TRACE((14,"TGo4ThreadHandler::AllWaiting()",__LINE__, __FILE__));
00571 Bool_t rev=kTRUE;
00572 TGo4Thread* th=0;
00573 {
00574 TGo4LockGuard listguard(fxListMutex);
00575 fxIterator->Reset();
00576 while((th= (TGo4Thread*) fxIterator->Next())!=0)
00577 {
00578 if(!(th->IsWaiting()))
00579 {
00580 TRACE((11,"TGo4ThreadHandler::AllCreated() TGo4Thread is still running",__LINE__, __FILE__));
00581
00582 rev=kFALSE;
00583 break;
00584 }
00585 else
00586 {
00587 TRACE((11,"TGo4ThreadHandler::AllCreated() TGo4Thread is waiting",__LINE__, __FILE__));
00588
00589 }
00590 }
00591 }
00592 return rev;
00593 }
00594
00595