00001 // $Id: TGo4LockGuard.cxx 478 2009-10-29 12:26:09Z 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 "Riostream.h" 00017 #include "TMutex.h" 00018 00019 TMutex* TGo4LockGuard::fgxMainMutex=0; 00020 00021 Int_t TGo4LockGuard::fgiLockCount=0; 00022 00023 00024 TGo4LockGuard::TGo4LockGuard (TMutex* mutex) 00025 { 00026 // first call: create main mutex 00027 if(fgxMainMutex==0) 00028 fgxMainMutex= new TMutex(kTRUE); // we use recursive mode for cascading lockguards 00029 if(mutex==0) 00030 { 00031 // use global mutex 00032 fxMutex=fgxMainMutex; 00033 fbIsMainMutex=kTRUE; 00034 } 00035 else 00036 { 00037 // use external mutex 00038 fxMutex=mutex; 00039 fbIsMainMutex=kFALSE; 00040 } 00041 00042 if (TThread::Exists()>0) 00043 { 00044 // UInt_t id = TThread::SelfId(); 00045 fxMutex->Lock(); 00046 fbIsLocked=kTRUE; 00047 if(!fbIsMainMutex) 00048 { 00049 // if(TGo4Log::GetIgnoreLevel()>5) 00050 // { 00051 // cout <<"G-----{ Local Mutex "<< fxMutex << " acquired by thread id: "<<id << endl; 00052 // } 00053 } 00054 else 00055 { 00056 fgiLockCount++; 00057 //if(TGo4Log::GetIgnoreLevel()>5) 00058 // { 00059 // cout <<"G-----{ Global Mutex "<< fxMutex << "acquired by thread id: "<<id; 00060 // cout<< ", count:"<< fgiLockCount << endl; 00061 // } 00062 } 00063 } 00064 else 00065 { 00066 // no thread, no lock 00067 fbIsLocked=kFALSE; 00068 } 00069 } 00070 00071 TGo4LockGuard::~TGo4LockGuard() 00072 { 00073 if (fbIsLocked) 00074 { 00075 //UInt_t id = TThread::SelfId(); 00076 if(!fbIsMainMutex) 00077 { 00078 // if(TGo4Log::GetIgnoreLevel()>5) 00079 // { 00080 // cout <<"}-----G Local Mutex "<< fxMutex << "released by thread id: "<<id << endl; 00081 // } 00082 } 00083 else 00084 { 00085 fgiLockCount--; 00086 // if(TGo4Log::GetIgnoreLevel()>5) 00087 // { 00088 // cout <<"}-----G Global Mutex "<< fxMutex << "released by thread id: "<<id << endl; 00089 // cout<< ", count:"<< fgiLockCount << endl; 00090 // } 00091 } 00092 fxMutex->UnLock(); 00093 } 00094 else 00095 { 00096 // no woman, no cry 00097 } 00098 } 00099 00100 void TGo4LockGuard::LockMainMutex() 00101 { 00102 fgxMainMutex->Lock(); 00103 } 00104 00105 void TGo4LockGuard::UnLockMainMutex() 00106 { 00107 fgxMainMutex->UnLock(); 00108 }