2007-03-27 16:05:33 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2005-2007, Ingo Weinhold, bonefish@users.sf.net.
|
|
|
|
* All rights reserved. Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef _AUTO_LOCKER_H
|
|
|
|
#define _AUTO_LOCKER_H
|
|
|
|
|
|
|
|
|
2009-03-25 02:09:49 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
|
2007-03-27 16:05:33 +04:00
|
|
|
namespace BPrivate {
|
|
|
|
|
|
|
|
// AutoLockerStandardLocking
|
|
|
|
template<typename Lockable>
|
|
|
|
class AutoLockerStandardLocking {
|
|
|
|
public:
|
2009-05-13 11:46:14 +04:00
|
|
|
inline bool Lock(Lockable* lockable)
|
2007-03-27 16:05:33 +04:00
|
|
|
{
|
|
|
|
return lockable->Lock();
|
|
|
|
}
|
|
|
|
|
2009-05-13 11:46:14 +04:00
|
|
|
inline void Unlock(Lockable* lockable)
|
2007-03-27 16:05:33 +04:00
|
|
|
{
|
|
|
|
lockable->Unlock();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// AutoLockerReadLocking
|
|
|
|
template<typename Lockable>
|
|
|
|
class AutoLockerReadLocking {
|
|
|
|
public:
|
2009-05-13 11:46:14 +04:00
|
|
|
inline bool Lock(Lockable* lockable)
|
2007-03-27 16:05:33 +04:00
|
|
|
{
|
|
|
|
return lockable->ReadLock();
|
|
|
|
}
|
|
|
|
|
2009-05-13 11:46:14 +04:00
|
|
|
inline void Unlock(Lockable* lockable)
|
2007-03-27 16:05:33 +04:00
|
|
|
{
|
|
|
|
lockable->ReadUnlock();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// AutoLockerWriteLocking
|
|
|
|
template<typename Lockable>
|
|
|
|
class AutoLockerWriteLocking {
|
|
|
|
public:
|
2009-05-13 11:46:14 +04:00
|
|
|
inline bool Lock(Lockable* lockable)
|
2007-03-27 16:05:33 +04:00
|
|
|
{
|
|
|
|
return lockable->WriteLock();
|
|
|
|
}
|
|
|
|
|
2009-05-13 11:46:14 +04:00
|
|
|
inline void Unlock(Lockable* lockable)
|
2007-03-27 16:05:33 +04:00
|
|
|
{
|
|
|
|
lockable->WriteUnlock();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// AutoLocker
|
|
|
|
template<typename Lockable,
|
|
|
|
typename Locking = AutoLockerStandardLocking<Lockable> >
|
|
|
|
class AutoLocker {
|
|
|
|
private:
|
|
|
|
typedef AutoLocker<Lockable, Locking> ThisClass;
|
|
|
|
public:
|
|
|
|
inline AutoLocker()
|
2009-03-25 02:09:49 +03:00
|
|
|
:
|
|
|
|
fLockable(NULL),
|
|
|
|
fLocked(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline AutoLocker(const Locking& locking)
|
|
|
|
:
|
|
|
|
fLockable(NULL),
|
|
|
|
fLocking(locking),
|
|
|
|
fLocked(false)
|
2007-03-27 16:05:33 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-05-13 11:46:14 +04:00
|
|
|
inline AutoLocker(Lockable* lockable, bool alreadyLocked = false,
|
2007-03-27 16:05:33 +04:00
|
|
|
bool lockIfNotLocked = true)
|
2009-03-25 02:09:49 +03:00
|
|
|
:
|
|
|
|
fLockable(lockable),
|
|
|
|
fLocked(fLockable && alreadyLocked)
|
2007-03-27 16:05:33 +04:00
|
|
|
{
|
|
|
|
if (!alreadyLocked && lockIfNotLocked)
|
|
|
|
Lock();
|
|
|
|
}
|
|
|
|
|
2009-05-13 11:46:14 +04:00
|
|
|
inline AutoLocker(Lockable& lockable, bool alreadyLocked = false,
|
2007-03-27 16:05:33 +04:00
|
|
|
bool lockIfNotLocked = true)
|
2009-03-25 02:09:49 +03:00
|
|
|
:
|
|
|
|
fLockable(&lockable),
|
|
|
|
fLocked(fLockable && alreadyLocked)
|
2007-03-27 16:05:33 +04:00
|
|
|
{
|
|
|
|
if (!alreadyLocked && lockIfNotLocked)
|
|
|
|
Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ~AutoLocker()
|
|
|
|
{
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
2009-05-13 11:46:14 +04:00
|
|
|
inline void SetTo(Lockable* lockable, bool alreadyLocked,
|
2007-03-27 16:05:33 +04:00
|
|
|
bool lockIfNotLocked = true)
|
|
|
|
{
|
|
|
|
Unlock();
|
|
|
|
fLockable = lockable;
|
2007-09-01 22:00:22 +04:00
|
|
|
fLocked = (lockable && alreadyLocked);
|
2007-03-27 16:05:33 +04:00
|
|
|
if (!alreadyLocked && lockIfNotLocked)
|
|
|
|
Lock();
|
|
|
|
}
|
|
|
|
|
2009-05-13 11:46:14 +04:00
|
|
|
inline void SetTo(Lockable& lockable, bool alreadyLocked,
|
2007-03-27 16:05:33 +04:00
|
|
|
bool lockIfNotLocked = true)
|
|
|
|
{
|
|
|
|
SetTo(&lockable, alreadyLocked, lockIfNotLocked);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unset()
|
|
|
|
{
|
|
|
|
Unlock();
|
|
|
|
Detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Lock()
|
|
|
|
{
|
|
|
|
if (fLockable && !fLocked)
|
|
|
|
fLocked = fLocking.Lock(fLockable);
|
|
|
|
return fLocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock()
|
|
|
|
{
|
|
|
|
if (fLockable && fLocked) {
|
|
|
|
fLocking.Unlock(fLockable);
|
|
|
|
fLocked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Detach()
|
|
|
|
{
|
|
|
|
fLockable = NULL;
|
|
|
|
fLocked = false;
|
|
|
|
}
|
|
|
|
|
2009-05-13 11:46:14 +04:00
|
|
|
inline AutoLocker<Lockable, Locking>& operator=(Lockable* lockable)
|
2007-03-27 16:05:33 +04:00
|
|
|
{
|
|
|
|
SetTo(lockable);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2009-05-13 11:46:14 +04:00
|
|
|
inline AutoLocker<Lockable, Locking>& operator=(Lockable& lockable)
|
2007-03-27 16:05:33 +04:00
|
|
|
{
|
|
|
|
SetTo(&lockable);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsLocked() const { return fLocked; }
|
|
|
|
|
|
|
|
inline operator bool() const { return fLocked; }
|
|
|
|
|
2007-10-01 22:39:02 +04:00
|
|
|
protected:
|
2009-05-13 11:46:14 +04:00
|
|
|
Lockable* fLockable;
|
2007-03-27 16:05:33 +04:00
|
|
|
Locking fLocking;
|
2009-03-25 02:09:49 +03:00
|
|
|
bool fLocked;
|
2007-03-27 16:05:33 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace BPrivate
|
|
|
|
|
2013-09-10 02:36:54 +04:00
|
|
|
using ::BPrivate::AutoLocker;
|
|
|
|
using ::BPrivate::AutoLockerReadLocking;
|
|
|
|
using ::BPrivate::AutoLockerWriteLocking;
|
2007-03-27 16:05:33 +04:00
|
|
|
|
|
|
|
#endif // _AUTO_LOCKER_H
|