MutexNSPR.h

Go to the documentation of this file.
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_MUTEX_NSPR_H_
00050 #define _VPR_MUTEX_NSPR_H_
00051 
00052 #include <vpr/vprConfig.h>
00053 
00054 #include <stdio.h>
00055 #include <stdlib.h>
00056 #include <prlock.h>
00057 
00058 #include <vpr/Util/Assert.h>
00059 #include <vpr/Util/ReturnStatus.h>
00060 
00061 
00062 namespace vpr
00063 {
00064 
00069 class VPR_CLASS_API MutexNSPR
00070 {
00071 public:
00078    MutexNSPR() : mLocked(0)
00079    {
00080       // ----- Allocate the mutex ----- //
00081       mMutex = PR_NewLock();
00082       vprASSERT(mMutex != NULL);
00083    }
00084 
00091    ~MutexNSPR()
00092    {
00093       PR_DestroyLock(mMutex);
00094    }
00095 
00107    vpr::ReturnStatus acquire()
00108    {
00109       PR_Lock(mMutex);
00110       mLocked = 1;
00111 
00112       return vpr::ReturnStatus();
00113    }
00114 
00128    vpr::ReturnStatus acquireRead()
00129    {
00130       return this->acquire();
00131    }
00132 
00146    vpr::ReturnStatus acquireWrite()
00147    {
00148       return this->acquire();
00149    }
00150 
00162    vpr::ReturnStatus tryAcquire()
00163    {
00164       // XXX: Possible race condition exists in this function implementation
00165       if ( mLocked == 0 )
00166       {
00167          this->acquire();
00168          return vpr::ReturnStatus();
00169       }
00170       else
00171       {
00172          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00173       }
00174    }
00175 
00188    vpr::ReturnStatus tryAcquireRead()
00189    {
00190       return this->tryAcquire();
00191    }
00192 
00205    vpr::ReturnStatus tryAcquireWrite()
00206    {
00207       return this->tryAcquire();
00208    }
00209 
00220    vpr::ReturnStatus release()
00221    {
00222       mLocked = 0;
00223       if ( PR_Unlock(mMutex) == PR_SUCCESS )
00224       {
00225          return vpr::ReturnStatus();
00226       }
00227       else
00228       {
00229          return vpr::ReturnStatus(vpr::ReturnStatus::Fail);
00230       }
00231    }
00232 
00241    int test() const
00242    {
00243       return mLocked;
00244    }
00245 
00246    // Allow the vpr::CondNSPR class to access the private and protected
00247    // members of this class.  Specifically, direct access is needed to the
00248    // mutex variable.
00249    friend class CondVarNSPR;
00250 
00251 protected:
00252    PRLock* mMutex;     
00253    int     mLocked;    
00255    // = Prevent assignment and initialization.
00256    MutexNSPR& operator=(const MutexNSPR& r)
00257    {
00258       mMutex = r.mMutex;
00259       mLocked = r.mLocked;
00260       return *this;
00261    }
00262 
00263    MutexNSPR(const MutexNSPR &)
00264    {
00265       /* Do nothing. */ ;
00266    }
00267 };
00268 
00269 } // End of vpr namespace
00270 
00271 
00272 #endif   /* ifdef _VPR_MUTEX_NSPR_H_ */

Generated on Thu Jan 4 10:52:10 2007 for VR Juggler Portable Runtime by  doxygen 1.5.1