* 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
This commit is contained in:
Ingo Weinhold
2007-03-27 12:05:33 +00:00
parent 30aaba68bc
commit 2b2ec4382a
7 changed files with 182 additions and 273 deletions
+1 -150
View File
@@ -7,160 +7,11 @@
#include <lock.h>
#include <shared/AutoLocker.h>
namespace BPrivate {
// AutoLockerStandardLocking
template<typename Lockable>
class AutoLockerStandardLocking {
public:
inline bool Lock(Lockable *lockable)
{
return lockable->Lock();
}
inline void Unlock(Lockable *lockable)
{
lockable->Unlock();
}
};
// AutoLockerReadLocking
template<typename Lockable>
class AutoLockerReadLocking {
public:
inline bool Lock(Lockable *lockable)
{
return lockable->ReadLock();
}
inline void Unlock(Lockable *lockable)
{
lockable->ReadUnlock();
}
};
// AutoLockerWriteLocking
template<typename Lockable>
class AutoLockerWriteLocking {
public:
inline bool Lock(Lockable *lockable)
{
return lockable->WriteLock();
}
inline void Unlock(Lockable *lockable)
{
lockable->WriteUnlock();
}
};
// AutoLocker
template<typename Lockable,
typename Locking = AutoLockerStandardLocking<Lockable> >
class AutoLocker {
private:
typedef AutoLocker<Lockable, Locking> 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<Lockable, Locking> &operator=(Lockable *lockable)
{
SetTo(lockable);
return *this;
}
inline AutoLocker<Lockable, Locking> &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:
+162
View File
@@ -0,0 +1,162 @@
/*
* Copyright 2005-2007, Ingo Weinhold, [email protected].
* All rights reserved. Distributed under the terms of the MIT License.
*/
#ifndef _AUTO_LOCKER_H
#define _AUTO_LOCKER_H
namespace BPrivate {
// AutoLockerStandardLocking
template<typename Lockable>
class AutoLockerStandardLocking {
public:
inline bool Lock(Lockable *lockable)
{
return lockable->Lock();
}
inline void Unlock(Lockable *lockable)
{
lockable->Unlock();
}
};
// AutoLockerReadLocking
template<typename Lockable>
class AutoLockerReadLocking {
public:
inline bool Lock(Lockable *lockable)
{
return lockable->ReadLock();
}
inline void Unlock(Lockable *lockable)
{
lockable->ReadUnlock();
}
};
// AutoLockerWriteLocking
template<typename Lockable>
class AutoLockerWriteLocking {
public:
inline bool Lock(Lockable *lockable)
{
return lockable->WriteLock();
}
inline void Unlock(Lockable *lockable)
{
lockable->WriteUnlock();
}
};
// AutoLocker
template<typename Lockable,
typename Locking = AutoLockerStandardLocking<Lockable> >
class AutoLocker {
private:
typedef AutoLocker<Lockable, Locking> 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<Lockable, Locking> &operator=(Lockable *lockable)
{
SetTo(lockable);
return *this;
}
inline AutoLocker<Lockable, Locking> &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
-100
View File
@@ -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 ([email protected])
// 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 T>
class BObjectLocker
{
public:
inline BObjectLocker(T* looper);
inline BObjectLocker(T& locker);
inline ~BObjectLocker();
inline bool IsLocked(void);
private:
T* fLockClient;
bool fIsLocked;
};
template<class T>
BObjectLocker<T>::BObjectLocker(T* client)
: fLockClient(client), fIsLocked(client->Lock())
{
}
template<class T>
BObjectLocker<T>::BObjectLocker(T& client)
: fLockClient(&client), fIsLocked(client.Lock())
{
}
template<class T>
BObjectLocker<T>::~BObjectLocker()
{
if (fIsLocked)
{
fLockClient->Unlock();
}
}
template<class T>
bool BObjectLocker<T>::IsLocked(void)
{
return fIsLocked;
}
} // namespace BPrivate
#endif //OBJECTLOCKER_H
/*
* $Log $
*
* $Id $
*
*/
+6 -6
View File
@@ -11,10 +11,10 @@
#include <AppMisc.h>
#include <AppServerLink.h>
#include <AutoLocker.h>
#include <DraggerPrivate.h>
#include <LooperList.h>
#include <MenuWindow.h>
#include <ObjectLocker.h>
#include <PortLink.h>
#include <RosterPrivate.h>
#include <ServerMemoryAllocator.h>
@@ -810,7 +810,7 @@ BApplication::WindowAt(int32 index) const
int32
BApplication::CountLoopers() const
{
BObjectLocker<BLooperList> ListLock(gLooperList);
AutoLocker<BLooperList> ListLock(gLooperList);
if (ListLock.IsLocked())
return gLooperList.CountLoopers();
@@ -823,7 +823,7 @@ BLooper *
BApplication::LooperAt(int32 index) const
{
BLooper *looper = NULL;
BObjectLocker<BLooperList> listLock(gLooperList);
AutoLocker<BLooperList> listLock(gLooperList);
if (listLock.IsLocked())
looper = gLooperList.LooperAt(index);
@@ -848,7 +848,7 @@ BApplication::GetAppInfo(app_info *info) const
BResources *
BApplication::AppResources()
{
BObjectLocker<BLocker> lock(sAppResourcesLock);
AutoLocker<BLocker> 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<BLooperList> listLock(gLooperList);
AutoLocker<BLooperList> 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<BLooperList> listLock(gLooperList);
AutoLocker<BLooperList> listLock(gLooperList);
if (!listLock.IsLocked())
return B_ERROR;
+4 -5
View File
@@ -12,10 +12,10 @@
/*! BLooper class spawns a thread that runs a message loop. */
#include <AppMisc.h>
#include <AutoLocker.h>
#include <DirectMessageTarget.h>
#include <LooperList.h>
#include <MessagePrivate.h>
#include <ObjectLocker.h>
#include <TokenSpace.h>
#include <Autolock.h>
@@ -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<BLooperList> ListLock(gLooperList);
AutoLocker<BLooperList> ListLock(gLooperList);
RemoveHandler(this);
// Remove all the "child" handlers
@@ -824,7 +823,7 @@ status_t
BLooper::_PostMessage(BMessage *msg, BHandler *handler,
BHandler *replyTo)
{
BObjectLocker<BLooperList> listLocker(gLooperList);
AutoLocker<BLooperList> 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<BLooperList> ListLock(gLooperList);
AutoLocker<BLooperList> ListLock(gLooperList);
if (!ListLock.IsLocked())
return B_BAD_VALUE;
+2 -3
View File
@@ -8,8 +8,8 @@
#include <AppMisc.h>
#include <AutoLocker.h>
#include <MessageUtils.h>
#include "ObjectLocker.h"
#include "TokenSpace.h"
#include <Application.h>
@@ -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<BLooperList> locker(gLooperList);
AutoLocker<BLooperList> locker(gLooperList);
if (locker.IsLocked() && gLooperList.IsLooperValid(looper)) {
fPort = looper->fMsgPort;
fHandlerToken = (handler
+7 -9
View File
@@ -6,21 +6,19 @@
* Ingo Weinhold, [email protected]
*/
#include <DiskDevice.h>
#include <DiskDeviceList.h>
#include <AutoLocker.h>
#include <DiskDevice.h>
#include <DiskDevicePrivate.h>
#include <DiskDeviceRoster.h>
#include <Locker.h>
#include <Looper.h>
#include <ObjectLocker.h>
#include <Partition.h>
#include <new>
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<BDiskDeviceList> _(this);
AutoLocker<BDiskDeviceList> _(this);
switch (message->what) {
case B_DEVICE_UPDATE:
{
@@ -110,7 +108,7 @@ void
BDiskDeviceList::SetNextHandler(BHandler *handler)
{
if (!handler) {
BObjectLocker<BDiskDeviceList> _(this);
AutoLocker<BDiskDeviceList> _(this);
if (fSubscribed)
_StopWatching();
}
@@ -136,7 +134,7 @@ status_t
BDiskDeviceList::Fetch()
{
Unset();
BObjectLocker<BDiskDeviceList> _(this);
AutoLocker<BDiskDeviceList> _(this);
// register for notifications
status_t error = B_OK;
if (Looper())
@@ -172,7 +170,7 @@ BDiskDeviceList::Fetch()
void
BDiskDeviceList::Unset()
{
BObjectLocker<BDiskDeviceList> _(this);
AutoLocker<BDiskDeviceList> _(this);
// unsubscribe from notification services
_StopWatching();
// empty the list