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-17 22:34:01 -0600 (Mon, 17 Jan 2005) $ 00011 * Version: $Revision: 16635 $ 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 00049 #ifndef _VPR_RWMUTEX_POSIX_H_ 00050 #define _VPR_RWMUTEX_POSIX_H_ 00051 00052 #include <vpr/vprConfig.h> 00053 00054 #include <stdio.h> 00055 #include <pthread.h> 00056 #include <errno.h> 00057 00058 #include <vpr/Util/ReturnStatus.h> 00059 00060 00061 namespace vpr 00062 { 00063 00069 class RWMutexPosix 00070 { 00071 public: 00079 RWMutexPosix() : mLocked(0) 00080 { 00081 // Initialize the mutex. 00082 pthread_rwlock_init(&mRWMutex, NULL); 00083 } 00084 00093 ~RWMutexPosix() 00094 { 00095 // Destroy the mutex. 00096 if ( pthread_rwlock_destroy(&mRWMutex) == -1 ) 00097 { 00098 pthread_rwlock_unlock(&mRWMutex); 00099 pthread_rwlock_destroy(&mRWMutex); 00100 } 00101 } 00102 00115 vpr::ReturnStatus acquire() 00116 { 00117 return acquireWrite(); 00118 } 00119 00131 vpr::ReturnStatus acquireRead() 00132 { 00133 int retval = pthread_rwlock_rdlock(&mRWMutex); 00134 00135 // Locking succeeded. 00136 if ( retval == 0 ) 00137 { 00138 mLocked = 1; 00139 return vpr::ReturnStatus(); 00140 } 00141 #ifdef _DEBUG 00142 // This thread tried to lock the mutex twice and a deadlock condition 00143 // was reported. 00144 else if ( retval == EDEADLK ) 00145 { 00146 perror("Tried to lock mutex twice (RWMutexPosix.h:146)"); 00147 return vpr::ReturnStatus(vpr::ReturnStatus::Fail); 00148 } 00149 #endif 00150 // Some other error occurred. 00151 else 00152 { 00153 return vpr::ReturnStatus(vpr::ReturnStatus::Fail); 00154 } 00155 } 00156 00167 vpr::ReturnStatus acquireWrite() 00168 { 00169 int retval = pthread_rwlock_wrlock(&mRWMutex); 00170 00171 // Locking succeeded. 00172 if ( retval == 0 ) 00173 { 00174 mLocked = 1; 00175 return vpr::ReturnStatus(); 00176 } 00177 #ifdef _DEBUG 00178 // This thread tried to lock the mutex twice and a deadlock condition 00179 // was reported. 00180 else if ( retval == EDEADLK ) 00181 { 00182 perror("Tried to lock mutex twice (RWMutexPosix.h:184)"); 00183 return vpr::ReturnStatus(vpr::ReturnStatus::Fail); 00184 } 00185 #endif 00186 // Some other error occurred. 00187 else 00188 { 00189 return vpr::ReturnStatus(vpr::ReturnStatus::Fail); 00190 } 00191 } 00192 00206 vpr::ReturnStatus tryAcquire() 00207 { 00208 return tryAcquireWrite(); 00209 } 00210 00223 vpr::ReturnStatus tryAcquireRead() 00224 { 00225 if ( pthread_rwlock_tryrdlock(&mRWMutex) == 0 ) 00226 { 00227 mLocked = 1; 00228 return vpr::ReturnStatus(); 00229 } 00230 else 00231 { 00232 return vpr::ReturnStatus(vpr::ReturnStatus::Fail); 00233 } 00234 00235 } 00236 00249 vpr::ReturnStatus tryAcquireWrite() 00250 { 00251 if ( pthread_rwlock_trywrlock(&mRWMutex) == 0 ) 00252 { 00253 mLocked = 1; 00254 return vpr::ReturnStatus(); 00255 } 00256 else 00257 { 00258 return vpr::ReturnStatus(vpr::ReturnStatus::Fail); 00259 } 00260 00261 } 00262 00273 vpr::ReturnStatus release() 00274 { 00275 mLocked = 0; 00276 if ( pthread_rwlock_unlock(&mRWMutex) == 0 ) 00277 { 00278 return vpr::ReturnStatus(); 00279 } 00280 else 00281 { 00282 return vpr::ReturnStatus(vpr::ReturnStatus::Fail); 00283 } 00284 } 00285 00294 int test() 00295 { 00296 return mLocked; 00297 } 00298 00299 protected: 00300 pthread_rwlock_t mRWMutex; 00301 int mLocked; 00303 // = Prevent assignment and initialization. 00304 void operator= (const RWMutexPosix &) {;} 00305 RWMutexPosix (const RWMutexPosix &) {;} 00306 }; 00307 00308 } // End of vpr namespace 00309 00310 00311 #endif /* ifdef _VPR_RWMUTEX_POSIX_H_ */
1.5.1