00001 // @(#)root/thread:$Id: TPosixMutex.cxx 29797 2009-08-17 14:35:51Z rdm $ 00002 // Author: Fons Rademakers 25/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 // TPosixMutex // 00015 // // 00016 // This class provides an interface to the posix mutex routines. // 00017 // // 00018 ////////////////////////////////////////////////////////////////////////// 00019 00020 #include "TThread.h" 00021 #include "TPosixMutex.h" 00022 #include "PosixThreadInc.h" 00023 00024 ClassImp(TPosixMutex) 00025 00026 //______________________________________________________________________________ 00027 TPosixMutex::TPosixMutex(Bool_t recursive) : TMutexImp() 00028 { 00029 // Create a posix mutex lock. 00030 00031 if (recursive) { 00032 00033 int rc; 00034 pthread_mutexattr_t attr; 00035 00036 rc = pthread_mutexattr_init(&attr); 00037 00038 if (!rc) { 00039 rc = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 00040 if (!rc) { 00041 rc = pthread_mutex_init(&fMutex, &attr); 00042 if (rc) 00043 SysError("TPosixMutex", "pthread_mutex_init error"); 00044 } else 00045 SysError("TPosixMutex", "pthread_mutexattr_settype error"); 00046 } else 00047 SysError("TPosixMutex", "pthread_mutex_init error"); 00048 00049 pthread_mutexattr_destroy(&attr); 00050 00051 } else { 00052 00053 int rc = pthread_mutex_init(&fMutex, 0); 00054 if (rc) 00055 SysError("TPosixMutex", "pthread_mutex_init error"); 00056 00057 } 00058 } 00059 00060 //______________________________________________________________________________ 00061 TPosixMutex::~TPosixMutex() 00062 { 00063 // TMutex dtor. 00064 00065 int rc = pthread_mutex_destroy(&fMutex); 00066 if (rc) 00067 SysError("~TPosixMutex", "pthread_mutex_destroy error"); 00068 } 00069 00070 //______________________________________________________________________________ 00071 Int_t TPosixMutex::Lock() 00072 { 00073 // Lock the mutex. 00074 00075 return pthread_mutex_lock(&fMutex); 00076 } 00077 00078 //______________________________________________________________________________ 00079 Int_t TPosixMutex::TryLock() 00080 { 00081 // Try locking the mutex. Returns 0 if mutex can be locked. 00082 00083 return pthread_mutex_trylock(&fMutex); 00084 } 00085 00086 //______________________________________________________________________________ 00087 Int_t TPosixMutex::UnLock(void) 00088 { 00089 // Unlock the mutex. 00090 00091 return pthread_mutex_unlock(&fMutex); 00092 }