mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 01:07:54 +00:00
Updates.
This commit is contained in:
84
ScopedLock.h
84
ScopedLock.h
@@ -9,44 +9,62 @@
|
||||
class ScopedLock
|
||||
{
|
||||
private:
|
||||
boost::recursive_mutex *mMutex; // parent object has greater scope, so guaranteed valid
|
||||
mutable bool mValid;
|
||||
boost::recursive_mutex *mMutex; // parent object has greater scope, so guaranteed valid
|
||||
mutable bool mValid;
|
||||
|
||||
ScopedLock(); // no implementation
|
||||
|
||||
ScopedLock(); // no implementation
|
||||
|
||||
public:
|
||||
ScopedLock(boost::recursive_mutex &mutex) : mMutex(&mutex), mValid(true)
|
||||
{
|
||||
mMutex->lock();
|
||||
}
|
||||
ScopedLock(boost::recursive_mutex &mutex) : mMutex(&mutex), mValid(true)
|
||||
{
|
||||
mMutex->lock();
|
||||
}
|
||||
|
||||
~ScopedLock()
|
||||
{
|
||||
if(mValid) mMutex->unlock();
|
||||
}
|
||||
~ScopedLock()
|
||||
{
|
||||
if(mValid) mMutex->unlock();
|
||||
}
|
||||
|
||||
ScopedLock(const ScopedLock &sl)
|
||||
{
|
||||
mMutex=sl.mMutex;
|
||||
if(sl.mValid)
|
||||
{
|
||||
mValid=true;
|
||||
sl.mValid=false;
|
||||
}
|
||||
else mValid=false;
|
||||
}
|
||||
ScopedLock(const ScopedLock &sl)
|
||||
{
|
||||
mMutex=sl.mMutex;
|
||||
if(sl.mValid)
|
||||
{
|
||||
mValid=true;
|
||||
sl.mValid=false;
|
||||
}
|
||||
else mValid=false;
|
||||
}
|
||||
|
||||
ScopedLock &operator=(const ScopedLock &sl)
|
||||
{ // we inherit any lock the other class member had
|
||||
if(mValid) mMutex->unlock();
|
||||
mMutex=sl.mMutex;
|
||||
if(sl.mValid)
|
||||
{
|
||||
mValid=true;
|
||||
sl.mValid=false;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
ScopedLock &operator=(const ScopedLock &sl)
|
||||
{ // we inherit any lock the other class member had
|
||||
if(mMutex!=sl.mMutex)
|
||||
{
|
||||
if(mValid) mMutex->unlock();
|
||||
mMutex=sl.mMutex;
|
||||
mMutex->lock();
|
||||
mValid=true;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
void unlock(void)
|
||||
{
|
||||
if(mValid)
|
||||
{
|
||||
mMutex->unlock();
|
||||
mValid=false;
|
||||
}
|
||||
}
|
||||
|
||||
void lock(void)
|
||||
{
|
||||
if(mValid)
|
||||
{
|
||||
mMutex->lock();
|
||||
mValid=true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user