00001 /****************** <VPR heading BEGIN do not edit this line> ***************** 00002 * 00003 * VR Juggler Portable Runtime 00004 * 00005 * Original Authors: 00006 * Allen Bierbaum, Patrick Hartling, Kevin Meinert, Carolina Cruz-Neira 00007 * 00008 * ----------------------------------------------------------------- 00009 * File: $RCSfile$ 00010 * Date modified: $Date: 2005-01-01 11:48:13 -0600 (Sat, 01 Jan 2005) $ 00011 * Version: $Revision: 16520 $ 00012 * ----------------------------------------------------------------- 00013 * 00014 ****************** <VPR heading END do not edit this line> ******************/ 00015 00016 /*************** <auto-copyright.pl BEGIN do not edit this line> ************** 00017 * 00018 * VR Juggler is (C) Copyright 1998-2005 by Iowa State University 00019 * 00020 * Original Authors: 00021 * Allen Bierbaum, Christopher Just, 00022 * Patrick Hartling, Kevin Meinert, 00023 * Carolina Cruz-Neira, Albert Baker 00024 * 00025 * This library is free software; you can redistribute it and/or 00026 * modify it under the terms of the GNU Library General Public 00027 * License as published by the Free Software Foundation; either 00028 * version 2 of the License, or (at your option) any later version. 00029 * 00030 * This library is distributed in the hope that it will be useful, 00031 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00032 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00033 * Library General Public License for more details. 00034 * 00035 * You should have received a copy of the GNU Library General Public 00036 * License along with this library; if not, write to the 00037 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00038 * Boston, MA 02111-1307, USA. 00039 * 00040 *************** <auto-copyright.pl END do not edit this line> ***************/ 00041 00042 #include <vpr/vprConfig.h> 00043 00044 #include <vpr/md/POSIX/Sync/MutexPosix.h> 00045 00046 namespace vpr 00047 { 00048 00049 MutexPosix::MutexPosix() 00050 { 00051 // Initialize the mutex. 00052 #ifndef _DEBUG 00053 pthread_mutex_init(&mMutex, NULL); 00054 #else 00055 #ifdef VPR_OS_Linux 00056 // If Linux and debug, then use error checking mutex 00057 pthread_mutexattr_t mutex_attr; 00058 pthread_mutexattr_init(&mutex_attr); 00059 pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK_NP); 00060 pthread_mutex_init(&mMutex, &mutex_attr); 00061 pthread_mutexattr_destroy(&mutex_attr); 00062 #else 00063 pthread_mutex_init(&mMutex, NULL); 00064 #endif 00065 #endif 00066 } 00067 00068 // Tests the current lock status. 00069 int MutexPosix::test() const 00070 { 00071 int ret_val; 00072 00073 ret_val = pthread_mutex_trylock(const_cast<pthread_mutex_t*>(&mMutex)); 00074 00075 // If the return value from pthread_mutex_trylock() is 0, then this 00076 // process now has a lock on mutex. Therefore, no other process could 00077 // have held a lock on it, so unlock the mutex and return 0. 00078 if ( ret_val == 0 ) 00079 { 00080 pthread_mutex_unlock(const_cast<pthread_mutex_t*>(&mMutex)); 00081 ret_val = 0; 00082 } 00083 // The mutex is currently locked by some thread if ret_val is non-zero. 00084 else 00085 { 00086 ret_val = 1; 00087 } 00088 00089 return ret_val; 00090 } 00091 00092 } // End of vpr namespace
1.5.1