00001 // @(#)root/thread:$Id: TMutex.cxx 29797 2009-08-17 14:35:51Z rdm $ 00002 // Author: Fons Rademakers 26/06/97 00003 00004 /************************************************************************* 00005 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. * 00006 * All rights reserved. * 00007 * * 00008 * For the licensing terms see $ROOTSYS/LICENSE. * 00009 * For the list of contributors see $ROOTSYS/README/CREDITS. * 00010 *************************************************************************/ 00011 00012 ////////////////////////////////////////////////////////////////////////// 00013 // // 00014 // TMutex // 00015 // // 00016 // This class implements mutex locks. A mutex is a mutual exclusive // 00017 // lock. The actual work is done via the TMutexImp class (either // 00018 // TPosixMutex or TWin32Mutex). // 00019 // // 00020 ////////////////////////////////////////////////////////////////////////// 00021 00022 #include "TInterpreter.h" 00023 #include "TMutex.h" 00024 #include "TThreadFactory.h" 00025 #include <errno.h> 00026 00027 00028 ClassImp(TMutex) 00029 00030 //______________________________________________________________________________ 00031 TMutex::TMutex(Bool_t recursive) 00032 { 00033 // Create a mutex lock. The actual mutex implementation will be 00034 // provided via the TThreadFactory. 00035 00036 fMutexImp = gThreadFactory->CreateMutexImp(recursive); 00037 00038 if (!fMutexImp) 00039 Error("TMutex", "could not create TMutexImp"); 00040 } 00041 00042 //______________________________________________________________________________ 00043 Int_t TMutex::Lock() 00044 { 00045 // Lock the mutex. Returns 0 when no error, EDEADLK when mutex was already 00046 // locked by this thread and this mutex is not reentrant. 00047 00048 Int_t iret = fMutexImp->Lock(); 00049 00050 return iret; 00051 } 00052 00053 //______________________________________________________________________________ 00054 Int_t TMutex::TryLock() 00055 { 00056 // Try to lock mutex. Returns 0 when no error, EDEADLK when mutex was 00057 // already locked by this thread and this mutex is not reentrant. 00058 00059 Int_t iret = fMutexImp->TryLock(); 00060 00061 return iret; 00062 } 00063 00064 //______________________________________________________________________________ 00065 Int_t TMutex::UnLock() 00066 { 00067 // Unlock the mutex. Returns 0 when no error, EPERM when mutex was already 00068 // unlocked by this thread. 00069 00070 return fMutexImp->UnLock(); 00071 } 00072 00073 //______________________________________________________________________________ 00074 Int_t TMutex::CleanUp() 00075 { 00076 // Clean up of mutex. 00077 00078 return UnLock(); 00079 } 00080 00081 //______________________________________________________________________________ 00082 TVirtualMutex *TMutex::Factory(Bool_t recursive) 00083 { 00084 // Create mutex and return pointer to it. Calling function must care 00085 // about proper deletion. The function is intended to be used in connection 00086 // with the R__LOCKGUARD2 macro for local thread protection. Since "new" is 00087 // used the TStorage class has to be protected by gGlobalMutex. 00088 00089 TVirtualMutex *ret = new TMutex(recursive); 00090 return ret; 00091 }