From a97769a5d365f08a83ef1e6d3637b75769ee799f Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Tue, 15 Jul 2003 21:39:34 +0000 Subject: [PATCH] Added {Read,Write}Lock{Device,Partition}(). git-svn-id: file:///srv/svn/repos/haiku/trunk/current@3986 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../disk_device_manager/KDiskDeviceManager.h | 9 ++ .../KDiskDeviceManager.cpp | 88 +++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/headers/private/kernel/disk_device_manager/KDiskDeviceManager.h b/headers/private/kernel/disk_device_manager/KDiskDeviceManager.h index 5571c20023..80494ff951 100644 --- a/headers/private/kernel/disk_device_manager/KDiskDeviceManager.h +++ b/headers/private/kernel/disk_device_manager/KDiskDeviceManager.h @@ -50,6 +50,15 @@ public: KPartition *RegisterPartition(partition_id id, bool noShadow = false); KFileDiskDevice *RegisterFileDevice(const char *filePath); + KDiskDevice *ReadLockDevice(partition_id id, bool deviceOnly = true); + KDiskDevice *WriteLockDevice(partition_id id, bool deviceOnly = true); + // The device is also registered and must be unregistered by the + // caller. + KPartition *ReadLockPartition(partition_id id); + KPartition *WriteLockPartition(partition_id id); + // Both the device and the partition is also registered and must be + // unregistered by the caller. + partition_id CreateFileDevice(const char *filePath); status_t DeleteFileDevice(const char *filePath); status_t DeleteFileDevice(partition_id id); diff --git a/src/kernel/core/disk_device_manager/KDiskDeviceManager.cpp b/src/kernel/core/disk_device_manager/KDiskDeviceManager.cpp index 18497ea12a..8244481ba7 100644 --- a/src/kernel/core/disk_device_manager/KDiskDeviceManager.cpp +++ b/src/kernel/core/disk_device_manager/KDiskDeviceManager.cpp @@ -359,6 +359,94 @@ KDiskDeviceManager::RegisterFileDevice(const char *filePath) return NULL; } +// ReadLockDevice +KDiskDevice * +KDiskDeviceManager::ReadLockDevice(partition_id id, bool deviceOnly) +{ + // register device + KDiskDevice *device = RegisterDevice(id, deviceOnly); + if (!device) + return NULL; + // lock device + if (device->ReadLock()) + return device; + device->Unregister(); + return NULL; +} + +// WriteLockDevice +KDiskDevice * +KDiskDeviceManager::WriteLockDevice(partition_id id, bool deviceOnly) +{ + // register device + KDiskDevice *device = RegisterDevice(id, deviceOnly); + if (!device) + return NULL; + // lock device + if (device->WriteLock()) + return device; + device->Unregister(); + return NULL; +} + +// ReadLockPartition +KPartition * +KDiskDeviceManager::ReadLockPartition(partition_id id) +{ + // register partition + KPartition *partition = RegisterPartition(id); + if (!partition) + return NULL; + // get and register the device + KDiskDevice *device = NULL; + if (ManagerLocker locker = this) { + device = partition->Device(); + if (device) + device->Register(); + } + // lock the device + if (device->ReadLock()) { + // final check, if the partition still belongs to the device + if (partition->Device() == device) + return partition; + device->ReadUnlock(); + } + // cleanup on failure + if (device) + device->Unregister(); + partition->Unregister(); + return NULL; +} + +// WriteLockPartition +KPartition * +KDiskDeviceManager::WriteLockPartition(partition_id id) +{ + // register partition + KPartition *partition = RegisterPartition(id); + if (!partition) + return NULL; + // get and register the device + KDiskDevice *device = NULL; + if (ManagerLocker locker = this) { + device = partition->Device(); + if (device) + device->Register(); + } + // lock the device + if (device->WriteLock()) { + // final check, if the partition still belongs to the device + if (partition->Device() == device) + return partition; + device->WriteUnlock(); + } + // cleanup on failure + if (device) + device->Unregister(); + partition->Unregister(); + return NULL; +} + // CreateFileDevice partition_id KDiskDeviceManager::CreateFileDevice(const char *filePath)