2003-06-11 02:33:44 +04:00
|
|
|
// KDiskDeviceUtils.h
|
|
|
|
|
|
|
|
#ifndef _K_DISK_DEVICE_UTILS_H
|
|
|
|
#define _K_DISK_DEVICE_UTILS_H
|
|
|
|
|
2003-06-23 03:00:47 +04:00
|
|
|
#include <stdlib.h>
|
2003-06-11 02:33:44 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
|
|
|
|
namespace BPrivate {
|
|
|
|
namespace DiskDevice {
|
|
|
|
|
|
|
|
// helper functions
|
|
|
|
|
|
|
|
// set_string
|
|
|
|
static inline
|
|
|
|
status_t
|
|
|
|
set_string(char *&location, const char *newValue)
|
|
|
|
{
|
|
|
|
// unset old value
|
|
|
|
if (location) {
|
|
|
|
free(location);
|
|
|
|
location = NULL;
|
|
|
|
}
|
|
|
|
// set new value
|
|
|
|
status_t error = B_OK;
|
|
|
|
if (newValue) {
|
|
|
|
location = strdup(newValue);
|
|
|
|
if (!location)
|
|
|
|
error = B_NO_MEMORY;
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// locking
|
|
|
|
|
|
|
|
// 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:
|
2003-06-24 03:42:15 +04:00
|
|
|
inline AutoLocker(Lockable *lockable, bool alreadyLocked = false)
|
2003-06-11 02:33:44 +04:00
|
|
|
: fLockable(lockable),
|
2003-06-24 03:42:15 +04:00
|
|
|
fLocked(fLockable && alreadyLocked)
|
2003-06-11 02:33:44 +04:00
|
|
|
{
|
2003-06-24 03:42:15 +04:00
|
|
|
if (!fLocked)
|
|
|
|
_Lock();
|
2003-06-11 02:33:44 +04:00
|
|
|
}
|
|
|
|
|
2003-06-24 03:42:15 +04:00
|
|
|
inline AutoLocker(Lockable &lockable, bool alreadyLocked = false)
|
2003-06-11 02:33:44 +04:00
|
|
|
: fLockable(&lockable),
|
2003-06-24 03:42:15 +04:00
|
|
|
fLocked(fLockable && alreadyLocked)
|
2003-06-11 02:33:44 +04:00
|
|
|
{
|
2003-06-24 03:42:15 +04:00
|
|
|
if (!fLocked)
|
|
|
|
_Lock();
|
2003-06-11 02:33:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ~AutoLocker()
|
|
|
|
{
|
|
|
|
_Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline AutoLocker<Lockable, Locking> &operator=(Lockable *lockable)
|
|
|
|
{
|
|
|
|
_Unlock();
|
|
|
|
fLockable = lockable;
|
|
|
|
_Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline AutoLocker<Lockable, Locking> &operator=(Lockable &lockable)
|
|
|
|
{
|
|
|
|
_Unlock();
|
|
|
|
fLockable = &lockable;
|
|
|
|
_Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsLocked() const { return fLocked; }
|
|
|
|
|
|
|
|
inline operator bool() const { return fLocked; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
inline void _Lock()
|
|
|
|
{
|
|
|
|
if (fLockable)
|
|
|
|
fLocked = fLocking.Lock(fLockable);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void _Unlock()
|
|
|
|
{
|
|
|
|
if (fLockable && fLocked) {
|
|
|
|
fLocking.Unlock(fLockable);
|
|
|
|
fLocked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Lockable *fLockable;
|
|
|
|
bool fLocked;
|
|
|
|
Locking fLocking;
|
|
|
|
};
|
|
|
|
|
|
|
|
// instantiations
|
|
|
|
class KDiskDevice;
|
|
|
|
class KDiskDeviceManager;
|
2003-06-24 03:42:15 +04:00
|
|
|
class KPartition;
|
2003-06-11 02:33:44 +04:00
|
|
|
|
|
|
|
typedef AutoLocker<KDiskDevice, AutoLockerReadLocking<KDiskDevice> >
|
|
|
|
DeviceReadLocker;
|
|
|
|
typedef AutoLocker<KDiskDevice, AutoLockerWriteLocking<KDiskDevice> >
|
|
|
|
DeviceWriteLocker;
|
|
|
|
typedef AutoLocker<KDiskDeviceManager > ManagerLocker;
|
|
|
|
|
2003-06-24 03:42:15 +04:00
|
|
|
// AutoLockerPartitionRegistration
|
|
|
|
template<const int dummy = 0>
|
|
|
|
class AutoLockerPartitionRegistration {
|
|
|
|
public:
|
|
|
|
inline bool Lock(KPartition *partition)
|
|
|
|
{
|
|
|
|
partition->Register();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Unlock(KPartition *partition)
|
|
|
|
{
|
|
|
|
partition->Unregister();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef AutoLocker<KPartition, AutoLockerPartitionRegistration<> >
|
|
|
|
PartitionRegistrar;
|
|
|
|
|
2003-06-11 02:33:44 +04:00
|
|
|
} // namespace DiskDevice
|
|
|
|
} // namespace BPrivate
|
|
|
|
|
|
|
|
using BPrivate::DiskDevice::set_string;
|
|
|
|
using BPrivate::DiskDevice::AutoLocker;
|
|
|
|
using BPrivate::DiskDevice::DeviceReadLocker;
|
|
|
|
using BPrivate::DiskDevice::DeviceWriteLocker;
|
|
|
|
using BPrivate::DiskDevice::ManagerLocker;
|
2003-07-07 02:57:40 +04:00
|
|
|
using BPrivate::DiskDevice::PartitionRegistrar;
|
2003-06-11 02:33:44 +04:00
|
|
|
|
|
|
|
#endif // _K_DISK_DEVICE_H
|