From 2b2ec4382a637811afeb5a4fa812052aef2779c6 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Tue, 27 Mar 2007 12:05:33 +0000 Subject: [PATCH] * Moved the AutoLocker class out of the kernel/utils/AutoLock.h header into its own shared/AutoLocker.h. It can be used by userland code too. * Removed headers/private/shared/ObjectLocker.h and replaced all uses of BObjectLocker by AutoLocker. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20432 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/kernel/util/AutoLock.h | 151 +---------------------- headers/private/shared/AutoLocker.h | 162 +++++++++++++++++++++++++ headers/private/shared/ObjectLocker.h | 100 --------------- src/kits/app/Application.cpp | 12 +- src/kits/app/Looper.cpp | 9 +- src/kits/app/Messenger.cpp | 5 +- src/kits/storage/DiskDeviceList.cpp | 16 ++- 7 files changed, 182 insertions(+), 273 deletions(-) create mode 100644 headers/private/shared/AutoLocker.h delete mode 100644 headers/private/shared/ObjectLocker.h diff --git a/headers/private/kernel/util/AutoLock.h b/headers/private/kernel/util/AutoLock.h index d99bec9425..58ee5e6dbb 100644 --- a/headers/private/kernel/util/AutoLock.h +++ b/headers/private/kernel/util/AutoLock.h @@ -7,160 +7,11 @@ #include +#include namespace BPrivate { -// AutoLockerStandardLocking -template -class AutoLockerStandardLocking { -public: - inline bool Lock(Lockable *lockable) - { - return lockable->Lock(); - } - - inline void Unlock(Lockable *lockable) - { - lockable->Unlock(); - } -}; - -// AutoLockerReadLocking -template -class AutoLockerReadLocking { -public: - inline bool Lock(Lockable *lockable) - { - return lockable->ReadLock(); - } - - inline void Unlock(Lockable *lockable) - { - lockable->ReadUnlock(); - } -}; - -// AutoLockerWriteLocking -template -class AutoLockerWriteLocking { -public: - inline bool Lock(Lockable *lockable) - { - return lockable->WriteLock(); - } - - inline void Unlock(Lockable *lockable) - { - lockable->WriteUnlock(); - } -}; - -// AutoLocker -template > -class AutoLocker { -private: - typedef AutoLocker ThisClass; -public: - inline AutoLocker() - : fLockable(NULL), - fLocked(false) - { - } - - inline AutoLocker(Lockable *lockable, bool alreadyLocked = false, - bool lockIfNotLocked = true) - : fLockable(lockable), - fLocked(fLockable && alreadyLocked) - { - if (!alreadyLocked && lockIfNotLocked) - Lock(); - } - - inline AutoLocker(Lockable &lockable, bool alreadyLocked = false, - bool lockIfNotLocked = true) - : fLockable(&lockable), - fLocked(fLockable && alreadyLocked) - { - if (!alreadyLocked && lockIfNotLocked) - Lock(); - } - - inline ~AutoLocker() - { - Unlock(); - } - - inline void SetTo(Lockable *lockable, bool alreadyLocked, - bool lockIfNotLocked = true) - { - Unlock(); - fLockable = lockable; - fLocked = alreadyLocked; - if (!alreadyLocked && lockIfNotLocked) - Lock(); - } - - inline void SetTo(Lockable &lockable, bool alreadyLocked, - 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; - } - - inline AutoLocker &operator=(Lockable *lockable) - { - SetTo(lockable); - return *this; - } - - inline AutoLocker &operator=(Lockable &lockable) - { - SetTo(&lockable); - return *this; - } - - inline bool IsLocked() const { return fLocked; } - - inline operator bool() const { return fLocked; } - -private: - Lockable *fLockable; - bool fLocked; - Locking fLocking; -}; - - -// #pragma mark - -// #pragma mark ----- instantiations ----- - // MutexLocking class MutexLocking { public: diff --git a/headers/private/shared/AutoLocker.h b/headers/private/shared/AutoLocker.h new file mode 100644 index 0000000000..a12c75c5fd --- /dev/null +++ b/headers/private/shared/AutoLocker.h @@ -0,0 +1,162 @@ +/* + * 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 + + +namespace BPrivate { + +// AutoLockerStandardLocking +template +class AutoLockerStandardLocking { +public: + inline bool Lock(Lockable *lockable) + { + return lockable->Lock(); + } + + inline void Unlock(Lockable *lockable) + { + lockable->Unlock(); + } +}; + +// AutoLockerReadLocking +template +class AutoLockerReadLocking { +public: + inline bool Lock(Lockable *lockable) + { + return lockable->ReadLock(); + } + + inline void Unlock(Lockable *lockable) + { + lockable->ReadUnlock(); + } +}; + +// AutoLockerWriteLocking +template +class AutoLockerWriteLocking { +public: + inline bool Lock(Lockable *lockable) + { + return lockable->WriteLock(); + } + + inline void Unlock(Lockable *lockable) + { + lockable->WriteUnlock(); + } +}; + +// AutoLocker +template > +class AutoLocker { +private: + typedef AutoLocker ThisClass; +public: + inline AutoLocker() + : fLockable(NULL), + fLocked(false) + { + } + + inline AutoLocker(Lockable *lockable, bool alreadyLocked = false, + bool lockIfNotLocked = true) + : fLockable(lockable), + fLocked(fLockable && alreadyLocked) + { + if (!alreadyLocked && lockIfNotLocked) + Lock(); + } + + inline AutoLocker(Lockable &lockable, bool alreadyLocked = false, + bool lockIfNotLocked = true) + : fLockable(&lockable), + fLocked(fLockable && alreadyLocked) + { + if (!alreadyLocked && lockIfNotLocked) + Lock(); + } + + inline ~AutoLocker() + { + Unlock(); + } + + inline void SetTo(Lockable *lockable, bool alreadyLocked, + bool lockIfNotLocked = true) + { + Unlock(); + fLockable = lockable; + fLocked = alreadyLocked; + if (!alreadyLocked && lockIfNotLocked) + Lock(); + } + + inline void SetTo(Lockable &lockable, bool alreadyLocked, + 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; + } + + inline AutoLocker &operator=(Lockable *lockable) + { + SetTo(lockable); + return *this; + } + + inline AutoLocker &operator=(Lockable &lockable) + { + SetTo(&lockable); + return *this; + } + + inline bool IsLocked() const { return fLocked; } + + inline operator bool() const { return fLocked; } + +private: + Lockable *fLockable; + bool fLocked; + Locking fLocking; +}; + + +} // namespace BPrivate + +using BPrivate::AutoLocker; + +#endif // _AUTO_LOCKER_H diff --git a/headers/private/shared/ObjectLocker.h b/headers/private/shared/ObjectLocker.h deleted file mode 100644 index f7dac2a97d..0000000000 --- a/headers/private/shared/ObjectLocker.h +++ /dev/null @@ -1,100 +0,0 @@ -//------------------------------------------------------------------------------ -// Copyright (c) 2001-2002, OpenBeOS -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -// -// File Name: ObjectLocker.h -// Author(s): Erik Jaesler (erik@cgsoftware.com) -// Description: A templatized version of BAutolock. Client class needs to -// supply: -// bool Lock() -- returns whether lock succeeded -// void Unlock() -- unlocks the class -//------------------------------------------------------------------------------ - -#ifndef OBJECTLOCKER_H -#define OBJECTLOCKER_H - -// Standard Includes ----------------------------------------------------------- - -// System Includes ------------------------------------------------------------- - -// Project Includes ------------------------------------------------------------ - -// Local Includes -------------------------------------------------------------- - -// Local Defines --------------------------------------------------------------- - -// Globals --------------------------------------------------------------------- - -namespace BPrivate { - -template -class BObjectLocker -{ - public: - inline BObjectLocker(T* looper); - inline BObjectLocker(T& locker); - - inline ~BObjectLocker(); - - inline bool IsLocked(void); - - private: - T* fLockClient; - bool fIsLocked; -}; - -template -BObjectLocker::BObjectLocker(T* client) - : fLockClient(client), fIsLocked(client->Lock()) -{ -} - -template -BObjectLocker::BObjectLocker(T& client) - : fLockClient(&client), fIsLocked(client.Lock()) -{ -} - -template -BObjectLocker::~BObjectLocker() -{ - if (fIsLocked) - { - fLockClient->Unlock(); - } -} - -template -bool BObjectLocker::IsLocked(void) -{ - return fIsLocked; -} - -} // namespace BPrivate - -#endif //OBJECTLOCKER_H - -/* - * $Log $ - * - * $Id $ - * - */ - diff --git a/src/kits/app/Application.cpp b/src/kits/app/Application.cpp index 8e4922739e..0f8cc9efda 100644 --- a/src/kits/app/Application.cpp +++ b/src/kits/app/Application.cpp @@ -11,10 +11,10 @@ #include #include +#include #include #include #include -#include #include #include #include @@ -810,7 +810,7 @@ BApplication::WindowAt(int32 index) const int32 BApplication::CountLoopers() const { - BObjectLocker ListLock(gLooperList); + AutoLocker ListLock(gLooperList); if (ListLock.IsLocked()) return gLooperList.CountLoopers(); @@ -823,7 +823,7 @@ BLooper * BApplication::LooperAt(int32 index) const { BLooper *looper = NULL; - BObjectLocker listLock(gLooperList); + AutoLocker listLock(gLooperList); if (listLock.IsLocked()) looper = gLooperList.LooperAt(index); @@ -848,7 +848,7 @@ BApplication::GetAppInfo(app_info *info) const BResources * BApplication::AppResources() { - BObjectLocker lock(sAppResourcesLock); + AutoLocker lock(sAppResourcesLock); // BApplication caches its resources, so check // if it already happened. @@ -1324,7 +1324,7 @@ BApplication::_WindowQuitLoop(bool quitFilePanels, bool force) { BList looperList; { - BObjectLocker listLock(gLooperList); + AutoLocker listLock(gLooperList); if (listLock.IsLocked()) { gLooperList.GetLooperList(&looperList); @@ -1469,7 +1469,7 @@ BApplication::_GetWindowList(BList *list, bool includeMenus) const // Windows are BLoopers, so we can just check each BLooper to see if it's // a BWindow (or BMenuWindow) - BObjectLocker listLock(gLooperList); + AutoLocker listLock(gLooperList); if (!listLock.IsLocked()) return B_ERROR; diff --git a/src/kits/app/Looper.cpp b/src/kits/app/Looper.cpp index 8c13ac17b0..60deb58749 100644 --- a/src/kits/app/Looper.cpp +++ b/src/kits/app/Looper.cpp @@ -12,10 +12,10 @@ /*! BLooper class spawns a thread that runs a message loop. */ #include +#include #include #include #include -#include #include #include @@ -53,7 +53,6 @@ static BLocker sDebugPrintLocker("BLooper debug print"); // Globals --------------------------------------------------------------------- using BPrivate::gDefaultTokens; using BPrivate::gLooperList; -using BPrivate::BObjectLocker; using BPrivate::BLooperList; port_id _get_looper_port_(const BLooper* looper); @@ -150,7 +149,7 @@ BLooper::~BLooper() // Clean up our filters SetCommonFilterList(NULL); - BObjectLocker ListLock(gLooperList); + AutoLocker ListLock(gLooperList); RemoveHandler(this); // Remove all the "child" handlers @@ -824,7 +823,7 @@ status_t BLooper::_PostMessage(BMessage *msg, BHandler *handler, BHandler *replyTo) { - BObjectLocker listLocker(gLooperList); + AutoLocker listLocker(gLooperList); if (!listLocker.IsLocked()) return B_ERROR; @@ -867,7 +866,7 @@ BLooper::_Lock(BLooper* looper, port_id port, bigtime_t timeout) sem_id sem; { - BObjectLocker ListLock(gLooperList); + AutoLocker ListLock(gLooperList); if (!ListLock.IsLocked()) return B_BAD_VALUE; diff --git a/src/kits/app/Messenger.cpp b/src/kits/app/Messenger.cpp index 1884a4734f..dc46576dc3 100644 --- a/src/kits/app/Messenger.cpp +++ b/src/kits/app/Messenger.cpp @@ -8,8 +8,8 @@ #include +#include #include -#include "ObjectLocker.h" #include "TokenSpace.h" #include @@ -36,7 +36,6 @@ using BPrivate::gDefaultTokens; using BPrivate::gLooperList; using BPrivate::BLooperList; -using BPrivate::BObjectLocker; enum { NOT_IMPLEMENTED = B_ERROR, @@ -114,7 +113,7 @@ BMessenger::BMessenger(const BHandler* handler, const BLooper* looper, } // set port, token,... if (error == B_OK) { - BObjectLocker locker(gLooperList); + AutoLocker locker(gLooperList); if (locker.IsLocked() && gLooperList.IsLooperValid(looper)) { fPort = looper->fMsgPort; fHandlerToken = (handler diff --git a/src/kits/storage/DiskDeviceList.cpp b/src/kits/storage/DiskDeviceList.cpp index cd65868a17..fd354978e6 100644 --- a/src/kits/storage/DiskDeviceList.cpp +++ b/src/kits/storage/DiskDeviceList.cpp @@ -6,21 +6,19 @@ * Ingo Weinhold, bonefish@users.sf.net */ - -#include #include + +#include +#include #include #include #include #include -#include #include #include using namespace std; -using BPrivate::BObjectLocker; - // constructor /*! \brief Creates an empty BDiskDeviceList object. */ @@ -48,7 +46,7 @@ BDiskDeviceList::~BDiskDeviceList() void BDiskDeviceList::MessageReceived(BMessage *message) { - BObjectLocker _(this); + AutoLocker _(this); switch (message->what) { case B_DEVICE_UPDATE: { @@ -110,7 +108,7 @@ void BDiskDeviceList::SetNextHandler(BHandler *handler) { if (!handler) { - BObjectLocker _(this); + AutoLocker _(this); if (fSubscribed) _StopWatching(); } @@ -136,7 +134,7 @@ status_t BDiskDeviceList::Fetch() { Unset(); - BObjectLocker _(this); + AutoLocker _(this); // register for notifications status_t error = B_OK; if (Looper()) @@ -172,7 +170,7 @@ BDiskDeviceList::Fetch() void BDiskDeviceList::Unset() { - BObjectLocker _(this); + AutoLocker _(this); // unsubscribe from notification services _StopWatching(); // empty the list