Files
haiku-beta6/headers/os/support/Autolock.h
T

69 lines
1.0 KiB
C++
Raw Normal View History

/*
* Copyright 2001-2007, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _SUPPORT_AUTOLOCK_H
#define _SUPPORT_AUTOLOCK_H
2002-07-09 12:24:59 +00:00
2003-05-14 17:21:46 +00:00
#include <Locker.h>
2002-07-09 12:24:59 +00:00
#include <Looper.h>
class BAutolock {
public:
inline BAutolock(BLooper *looper);
inline BAutolock(BLocker *locker);
inline BAutolock(BLocker &locker);
inline ~BAutolock();
inline bool IsLocked(void);
private:
BLocker *fLocker;
BLooper *fLooper;
bool fIsLocked;
2002-07-09 12:24:59 +00:00
};
inline
BAutolock::BAutolock(BLooper *looper)
: fLocker(NULL), fLooper(looper), fIsLocked(looper->Lock())
2002-07-09 12:24:59 +00:00
{
}
inline
BAutolock::BAutolock(BLocker *locker)
: fLocker(locker), fLooper(NULL), fIsLocked(locker->Lock())
2002-07-09 12:24:59 +00:00
{
}
inline
BAutolock::BAutolock(BLocker &locker)
: fLocker(&locker), fLooper(NULL), fIsLocked(locker.Lock())
2002-07-09 12:24:59 +00:00
{
}
inline
BAutolock::~BAutolock()
2002-07-09 12:24:59 +00:00
{
if (fIsLocked) {
if (fLooper != NULL)
fLooper->Unlock();
else
fLocker->Unlock();
2002-07-09 12:24:59 +00:00
}
}
inline bool
BAutolock::IsLocked()
2002-07-09 12:24:59 +00:00
{
return fIsLocked;
}
#endif // _SUPPORT_AUTOLOCK_H