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 00042 #ifndef _VPR_MutexSGI_h_ 00043 #define _VPR_MutexSGI_h_ 00044 00045 #include <vpr/vprConfig.h> 00046 #include <string.h> 00047 #include <ulocks.h> 00048 #include <errno.h> 00049 #include <vpr/md/SPROC/SharedMem/MemPool.h> 00050 #include <vpr/Util/Assert.h> 00051 #include <vpr/Util/ReturnStatus.h> 00052 00053 00054 namespace vpr 00055 { 00056 00064 class MutexSGI 00065 { 00066 public: 00067 MutexSGI() : mMutex(NULL) 00068 { 00069 // BUG: Possible race condition here 00070 if ( mMutexPool == NULL ) 00071 { 00072 mMutexPool = new MemPoolSGI(65536, 32, 00073 "/var/tmp/memMutexPoolSGIXXXXXX"); 00074 mAttachedCounter = static_cast<int*>(mMutexPool->allocate(sizeof(int))); 00075 *mAttachedCounter = 0; 00076 } 00077 00078 // Track how many mutexes are allocated 00079 *mAttachedCounter = *mAttachedCounter + 1; 00080 // vprDEBUG << " vpr::MutexSGI::MutexSGI: mAttachedCounter: " 00081 // << *mAttachedCounter << endl << vprDEBUG_FLUSH; 00082 00083 // ----- Allocate the mutex ----- // 00084 mMutex = usnewlock(mMutexPool->getArena()); 00085 00086 if ( NULL == mMutex ) 00087 { 00088 std::cerr << "ERROR: Failed to allocate new mutex -- " 00089 << strerror(errno) << std::endl; 00090 vprASSERT(mMutex != NULL && "in vpr::MutexSGI::MutexSGI() mMutex is NULL"); 00091 } 00092 } 00093 00094 ~MutexSGI() 00095 { 00096 // ---- Delete the mutex --- // 00097 usfreelock(mMutex, mMutexPool->getArena()); 00098 00099 // ---- Deal with the pool --- // 00100 00101 // Track how many mutexes are allocated 00102 *mAttachedCounter = *mAttachedCounter - 1; 00103 00104 // vprDEBUG << "vpr::MutexSGI::~MutexSGI: mAttachedCounter: " 00105 // << *mAttachedCounter << endl << vprDEBUG_FLUSH; 00106 00107 if ( *mAttachedCounter == 0 ) 00108 { 00109 mMutexPool->deallocate(mAttachedCounter); 00110 mAttachedCounter = NULL; 00111 delete mMutexPool; 00112 mMutexPool = NULL; 00113 } 00114 } 00115 00126 vpr::ReturnStatus acquire() const 00127 { 00128 vprASSERT(mMutex != NULL && "in vpr::MutexSGI::aquire() mMutex is NULL"); 00129 if ( ussetlock(mMutex) == 1 ) 00130 { 00131 return vpr::ReturnStatus(); 00132 } 00133 else 00134 { 00135 return vpr::ReturnStatus(vpr::ReturnStatus::Fail); 00136 } 00137 } 00138 00152 vpr::ReturnStatus acquireRead() const 00153 { 00154 return this->acquire(); // No special "read" semaphore -- For now 00155 } 00156 00170 vpr::ReturnStatus acquireWrite() const 00171 { 00172 return this->acquire(); // No special "write" semaphore -- For now 00173 } 00174 00186 vpr::ReturnStatus tryAcquire() const 00187 { 00188 // Try 100 spins. 00189 if ( uscsetlock(mMutex, 100) == 1 ) 00190 { 00191 return vpr::ReturnStatus(); 00192 } 00193 else 00194 { 00195 return vpr::ReturnStatus(vpr::ReturnStatus::Fail); 00196 } 00197 } 00198 00210 vpr::ReturnStatus tryAcquireRead() const 00211 { 00212 return this->tryAcquire(); 00213 } 00214 00226 vpr::ReturnStatus tryAcquireWrite() const 00227 { 00228 return this->tryAcquire(); 00229 } 00230 00240 vpr::ReturnStatus release() const 00241 { 00242 if ( usunsetlock(mMutex) == 0 ) 00243 { 00244 return vpr::ReturnStatus(); 00245 } 00246 else 00247 { 00248 return vpr::ReturnStatus(vpr::ReturnStatus::Fail); 00249 } 00250 } 00251 00260 int test() const 00261 { 00262 return ustestlock(const_cast<ulock_t>(mMutex)); 00263 } 00264 00276 void dump(FILE* dest = stdout, 00277 const char* message = "\n------ Mutex Status -----\n") const 00278 { 00279 usdumplock(mMutex, dest, message); 00280 } 00281 00282 protected: 00283 ulock_t mMutex; 00284 00285 // = Prevent assignment and initialization. 00286 void operator= (const MutexSGI &) {;} 00287 MutexSGI (const MutexSGI &) {;} 00288 00289 static MemPoolSGI* mMutexPool; 00290 static int* mAttachedCounter; 00291 }; 00292 00293 } // End of vpr namespace 00294 00295 00296 #endif /* _VPR_MutexSGI_h_ */
1.5.1