00001 // $Id: TGo4LockGuard.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 "TGo4LockGuard.h" 00015 00016 #include "TMutex.h" 00017 00018 TMutex* TGo4LockGuard::fgxMainMutex=0; 00019 00020 Int_t TGo4LockGuard::fgiLockCount=0; 00021 00022 00023 TGo4LockGuard::TGo4LockGuard (TMutex* mutex, Bool_t) 00024 //: fbForceLock(forcelock) 00025 { 00026 // first call: create main mutex 00027 // std::cout <<"G-----TGo4LockGuard ctor" << std::endl; 00028 // 00029 if(fgxMainMutex==0) 00030 fgxMainMutex= new TMutex(kTRUE); // we use recursive mode for cascading lockguards 00031 if(mutex==0) 00032 { 00033 // use global mutex 00034 fxMutex=fgxMainMutex; 00035 fbIsMainMutex=kTRUE; 00036 } 00037 else 00038 { 00039 // use external mutex 00040 fxMutex=mutex; 00041 fbIsMainMutex=kFALSE; 00042 } 00043 00044 //#if ROOT_VERSION_CODE < ROOT_VERSION(5,31,0) 00045 //if (fbForceLock || TThread::Exists()>0) 00046 //#else 00047 // JAM for the moment, we disable check for existing threads until problem with ROOT > 5.31 is solved 00048 // suspect this is due to changed library linkage in root build 00049 // note: was solved after ROOT version 5.32.00-rc1 00050 //if(1) 00051 //#endif 00052 00053 // NOTE2: since TThread::Exists() will use internal mutex lock anyway, we do not gain anything here 00054 // this did save a lock in the old days only! JAM 00055 { 00056 // UInt_t id = TThread::SelfId(); 00057 fxMutex->Lock(); 00058 fbIsLocked=kTRUE; 00059 if(!fbIsMainMutex) 00060 { 00061 // if(TGo4Log::GetIgnoreLevel()>5) 00062 // { 00063 00064 } 00065 else 00066 { 00067 fgiLockCount++; 00068 //if(TGo4Log::GetIgnoreLevel()>5) 00069 // { 00070 // std::cout <<"G-----{ Global Mutex "<< fxMutex << "acquired by thread id: "<<id; 00071 // std::cout<< ", count:"<< fgiLockCount << std::endl; 00072 // } 00073 } 00074 } 00075 // else 00076 // { 00077 // // no thread, no lock 00078 // fbIsLocked=kFALSE; 00079 // } 00080 } 00081 00082 TGo4LockGuard::~TGo4LockGuard() 00083 { 00084 if (fbIsLocked) 00085 { 00086 // UInt_t id = TThread::SelfId(); 00087 if(!fbIsMainMutex) 00088 { 00089 // if(TGo4Log::GetIgnoreLevel()>5) 00090 // { 00091 // std::cout <<"}-----G Local Mutex "<< fxMutex << "released by thread id: "<<id << std::endl; 00092 // } 00093 } 00094 else 00095 { 00096 fgiLockCount--; 00097 // if(TGo4Log::GetIgnoreLevel()>5) 00098 // { 00099 // std::cout <<"}-----G Global Mutex "<< fxMutex << "released by thread id: "<<id << std::endl; 00100 // std::cout<< ", count:"<< fgiLockCount << std::endl; 00101 // } 00102 } 00103 fxMutex->UnLock(); 00104 } 00105 else 00106 { 00107 // no woman, no cry 00108 } 00109 } 00110 00111 void TGo4LockGuard::LockMainMutex() 00112 { 00113 fgxMainMutex->Lock(); 00114 } 00115 00116 void TGo4LockGuard::UnLockMainMutex() 00117 { 00118 fgxMainMutex->UnLock(); 00119 }