mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 11:05:54 +00:00
For some reason, Boost provides no good way to return a scoped lock.
It appears people usually fake it by not using RAII. This tiny bit of ugliness will make returned scope lock holders work.
This commit is contained in:
52
ScopedLock.h
Normal file
52
ScopedLock.h
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#ifndef __SCOPEDLOCKHOLDER__
|
||||||
|
#define __SCOPEDLOCKHOLDER__
|
||||||
|
|
||||||
|
#include "boost/thread/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::mutex *mMutex; // parent object has greater scope, so guaranteed valid
|
||||||
|
mutable bool mValid;
|
||||||
|
|
||||||
|
ScopedLock(); // no implementation
|
||||||
|
|
||||||
|
public:
|
||||||
|
ScopedLock(boost::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)
|
||||||
|
{
|
||||||
|
if(mValid) mMutex->unlock();
|
||||||
|
mValid=true;
|
||||||
|
sl.mValid=false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user