00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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
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
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
00247
00248
00249 friend class CondVarNSPR;
00250
00251 protected:
00252 PRLock* mMutex;
00253 int mLocked;
00255
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 ;
00266 }
00267 };
00268
00269 }
00270
00271
00272 #endif