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