Files
xahaud/ScopedLock.h
JoelKatz 55b2111fd5 Updates. Create a class to hold an item in a SHAMap, separating the tag from
the data (it's not always the hash of the data). Make ScopedLock's recursive.
2011-11-15 10:49:14 -08:00

52 lines
1.0 KiB
C++

#ifndef __SCOPEDLOCKHOLDER__
#define __SCOPEDLOCKHOLDER__
#include "boost/thread/recursive_mutex.hpp"
// This is a returnable lock holder.
// I don't know why Boost doesn't provide a good way to do this.
class ScopedLock
{
private:
boost::recursive_mutex *mMutex; // parent object has greater scope, so guaranteed valid
mutable bool mValid;
ScopedLock(); // no implementation
public:
ScopedLock(boost::recursive_mutex &mutex) : mMutex(&mutex), mValid(true)
{
mMutex->lock();
}
~ScopedLock()
{
if(mValid) mMutex->unlock();
}
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;
}
}
};
#endif