00001 // $Id: TGo4Queue.cxx 999 2013-07-25 11:58:59Z linev $ 00002 //----------------------------------------------------------------------- 00003 // The GSI Online Offline Object Oriented (Go4) Project 00004 // Experiment Data Processing at EE department, GSI 00005 //----------------------------------------------------------------------- 00006 // Copyright (C) 2000- GSI Helmholtzzentrum f�r Schwerionenforschung GmbH 00007 // Planckstr. 1, 64291 Darmstadt, Germany 00008 // Contact: http://go4.gsi.de 00009 //----------------------------------------------------------------------- 00010 // This software can be used under the license agreements as stated 00011 // in Go4License.txt file which is part of the distribution. 00012 //----------------------------------------------------------------------- 00013 00014 #include "TGo4Queue.h" 00015 00016 #include "TList.h" 00017 #include "TMutex.h" 00018 #include "TCondition.h" 00019 00020 #include "TGo4Log.h" 00021 #include "TGo4LockGuard.h" 00022 #include "TGo4RuntimeException.h" 00023 00024 TGo4Queue::TGo4Queue(const char* name) : 00025 TNamed(name ? name : "Default Queue", "This is a Go4 Queue"), 00026 fiEntries(0), 00027 fiMaxEntries(100), 00028 fbWakeUpCall(kFALSE) 00029 { 00030 fxMutex = new TMutex; 00031 fxCondition = new TCondition; 00032 fxList = new TList; 00033 } 00034 00035 TGo4Queue::~TGo4Queue() 00036 { 00037 delete fxList; fxList = 0; 00038 delete fxCondition; fxCondition = 0; 00039 delete fxMutex; fxMutex = 0; 00040 } 00041 00042 void TGo4Queue::Clear(Option_t* opt) 00043 { 00044 TGo4LockGuard qguard(fxMutex); 00045 fxList->Clear(); 00046 } 00047 00048 TObject* TGo4Queue::Wait() 00049 { 00050 if(IsEmpty()) { 00051 //std::cout <<"WWWWWWWWWWWWWWWWWW --- TGo4Queue "<< GetName() <<" is in Condition Wait..." << std::endl; 00052 fxCondition->Wait(); 00053 if(fbWakeUpCall) { 00054 fbWakeUpCall = kFALSE; 00055 return 0; // signal by Wake(), give null back! 00056 } 00057 } 00058 return Next(); 00059 } 00060 00061 TObject* TGo4Queue::Next() 00062 { 00063 TGo4LockGuard qguard(fxMutex); 00064 return (fxList->Remove(fxList->LastLink()) ); 00065 } 00066 00067 void TGo4Queue::Add(TObject* ob) 00068 { 00069 { 00070 TGo4LockGuard qguard(fxMutex); 00071 if(fxList->GetSize() <= fiMaxEntries) 00072 { 00073 fxList->AddFirst(ob); 00074 //std::cout <<"QA Queue " <<GetName() <<" added obj, size="<<fxList->GetSize()<< std::endl; 00075 } 00076 else 00077 { 00078 //std::cout <<" Queue Add Error: queue "<< GetName() <<" is full !!!"<< std::endl; 00079 throw TGo4RuntimeException(); 00080 } 00081 } //TGo4LockGuard qguard(fxMutex); 00082 fxCondition->Signal(); 00083 } 00084 00085 Bool_t TGo4Queue::IsEmpty() 00086 { 00087 TGo4LockGuard qguard(fxMutex); 00088 return fxList->IsEmpty(); 00089 } 00090 00091 void TGo4Queue::Wake() 00092 { 00093 //std::cout <<"TGo4Queue::Wake() in "<<GetName() << std::endl; 00094 fbWakeUpCall = kTRUE; 00095 fxCondition->Signal(); 00096 }