While the AutoLocker is not public at least make BAutolock a bit more useful by

also providing Lock() and Unlock(). Applied public header style.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33367 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz
2009-09-30 01:00:23 +00:00
parent 290c903985
commit ace6934d79
+55 -19
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2007, Haiku, Inc. All Rights Reserved. * Copyright 2001-2009, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#ifndef _SUPPORT_AUTOLOCK_H #ifndef _SUPPORT_AUTOLOCK_H
@@ -11,38 +11,50 @@
class BAutolock { class BAutolock {
public: public:
inline BAutolock(BLooper *looper); inline BAutolock(BLooper* looper);
inline BAutolock(BLocker *locker); inline BAutolock(BLocker* locker);
inline BAutolock(BLocker &locker); inline BAutolock(BLocker& locker);
inline ~BAutolock(); inline ~BAutolock();
inline bool IsLocked(void); inline bool IsLocked();
private: inline bool Lock();
BLocker *fLocker; inline void Unlock();
BLooper *fLooper;
private:
BLocker* fLocker;
BLooper* fLooper;
bool fIsLocked; bool fIsLocked;
}; };
inline inline
BAutolock::BAutolock(BLooper *looper) BAutolock::BAutolock(BLooper *looper)
: fLocker(NULL), fLooper(looper), fIsLocked(looper->Lock()) :
fLocker(NULL),
fLooper(looper),
fIsLocked(looper->Lock())
{ {
} }
inline inline
BAutolock::BAutolock(BLocker *locker) BAutolock::BAutolock(BLocker *locker)
: fLocker(locker), fLooper(NULL), fIsLocked(locker->Lock()) :
fLocker(locker),
fLooper(NULL),
fIsLocked(locker->Lock())
{ {
} }
inline inline
BAutolock::BAutolock(BLocker &locker) BAutolock::BAutolock(BLocker &locker)
: fLocker(&locker), fLooper(NULL), fIsLocked(locker.Lock()) :
fLocker(&locker),
fLooper(NULL),
fIsLocked(locker.Lock())
{ {
} }
@@ -50,12 +62,7 @@ BAutolock::BAutolock(BLocker &locker)
inline inline
BAutolock::~BAutolock() BAutolock::~BAutolock()
{ {
if (fIsLocked) { Unlock();
if (fLooper != NULL)
fLooper->Unlock();
else
fLocker->Unlock();
}
} }
@@ -65,4 +72,33 @@ BAutolock::IsLocked()
return fIsLocked; return fIsLocked;
} }
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();
}
#endif // _SUPPORT_AUTOLOCK_H #endif // _SUPPORT_AUTOLOCK_H