From ace6934d795ad4b76a20c299e787a6e10bf6f29d Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Wed, 30 Sep 2009 01:00:23 +0000 Subject: [PATCH] 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 --- headers/os/support/Autolock.h | 76 ++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 20 deletions(-) diff --git a/headers/os/support/Autolock.h b/headers/os/support/Autolock.h index ac976a5bdf..1ae6e04759 100644 --- a/headers/os/support/Autolock.h +++ b/headers/os/support/Autolock.h @@ -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. */ #ifndef _SUPPORT_AUTOLOCK_H @@ -11,38 +11,50 @@ class BAutolock { - public: - inline BAutolock(BLooper *looper); - inline BAutolock(BLocker *locker); - inline BAutolock(BLocker &locker); - inline ~BAutolock(); +public: +inline BAutolock(BLooper* looper); +inline BAutolock(BLocker* locker); +inline BAutolock(BLocker& locker); +inline ~BAutolock(); - inline bool IsLocked(void); +inline bool IsLocked(); - private: - BLocker *fLocker; - BLooper *fLooper; - bool fIsLocked; +inline bool Lock(); +inline void Unlock(); + +private: + BLocker* fLocker; + BLooper* fLooper; + bool fIsLocked; }; inline BAutolock::BAutolock(BLooper *looper) - : fLocker(NULL), fLooper(looper), fIsLocked(looper->Lock()) + : + fLocker(NULL), + fLooper(looper), + fIsLocked(looper->Lock()) { } inline BAutolock::BAutolock(BLocker *locker) - : fLocker(locker), fLooper(NULL), fIsLocked(locker->Lock()) + : + fLocker(locker), + fLooper(NULL), + fIsLocked(locker->Lock()) { } inline 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 BAutolock::~BAutolock() { - if (fIsLocked) { - if (fLooper != NULL) - fLooper->Unlock(); - else - fLocker->Unlock(); - } + Unlock(); } @@ -65,4 +72,33 @@ BAutolock::IsLocked() 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