From cf3ee14a9d55b477772cc625662dca18c43dd44b Mon Sep 17 00:00:00 2001 From: Liryna Date: Sun, 13 Feb 2022 18:15:16 -0500 Subject: [PATCH] MemFS - Replace std::mutex by std::shared_mutex --- samples/dokan_memfs/filenode.cpp | 18 +++++++++--------- samples/dokan_memfs/filenode.h | 8 ++++---- samples/dokan_memfs/memfs_operations.cpp | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/samples/dokan_memfs/filenode.cpp b/samples/dokan_memfs/filenode.cpp index 748b94deb..99d51e1d5 100644 --- a/samples/dokan_memfs/filenode.cpp +++ b/samples/dokan_memfs/filenode.cpp @@ -44,7 +44,7 @@ filenode::filenode(const std::wstring& filename, bool is_directory, } DWORD filenode::read(LPVOID buffer, DWORD bufferlength, LONGLONG offset) { - std::lock_guard lock(_data_mutex); + std::shared_lock lock(_data_mutex); if (static_cast(offset + bufferlength) > _data.size()) bufferlength = (_data.size() > static_cast(offset)) ? static_cast(_data.size() - offset) @@ -60,7 +60,7 @@ DWORD filenode::write(LPCVOID buffer, DWORD number_of_bytes_to_write, LONGLONG offset) { if (!number_of_bytes_to_write) return 0; - std::lock_guard lock(_data_mutex); + std::unique_lock lock(_data_mutex); if (static_cast(offset + number_of_bytes_to_write) > _data.size()) _data.resize(static_cast(offset + number_of_bytes_to_write)); @@ -71,38 +71,38 @@ DWORD filenode::write(LPCVOID buffer, DWORD number_of_bytes_to_write, } const LONGLONG filenode::get_filesize() { - std::lock_guard lock(_data_mutex); + std::shared_lock lock(_data_mutex); return static_cast(_data.size()); } void filenode::set_endoffile(const LONGLONG& byte_offset) { - std::lock_guard lock(_data_mutex); + std::unique_lock lock(_data_mutex); _data.resize(static_cast(byte_offset)); } const std::wstring filenode::get_filename() { - std::lock_guard lock(_fileName_mutex); + std::shared_lock lock(_fileName_mutex); return _fileName; } void filenode::set_filename(const std::wstring& f) { - std::lock_guard lock(_fileName_mutex); + std::unique_lock lock(_fileName_mutex); _fileName = f; } void filenode::add_stream(const std::shared_ptr& stream) { - std::lock_guard lock(_data_mutex); + std::unique_lock lock(_data_mutex); _streams[stream->get_filename()] = stream; } void filenode::remove_stream(const std::shared_ptr& stream) { - std::lock_guard lock(_data_mutex); + std::unique_lock lock(_data_mutex); _streams.erase(stream->get_filename()); } std::unordered_map > filenode::get_streams() { - std::lock_guard lock(_data_mutex); + std::shared_lock lock(_data_mutex); return _streams; } } // namespace memfs \ No newline at end of file diff --git a/samples/dokan_memfs/filenode.h b/samples/dokan_memfs/filenode.h index afef17f2e..63b32389d 100644 --- a/samples/dokan_memfs/filenode.h +++ b/samples/dokan_memfs/filenode.h @@ -36,7 +36,7 @@ THE SOFTWARE. #include #include #include -#include +#include #include #include #include @@ -44,7 +44,7 @@ THE SOFTWARE. namespace memfs { // Safe class wrapping a Win32 Security Descriptor -struct security_informations : std::mutex { +struct security_informations : std::shared_mutex { std::unique_ptr descriptor = nullptr; DWORD descriptor_size = 0; @@ -121,12 +121,12 @@ class filenode { private: filenode() = default; - std::mutex _data_mutex; + std::shared_mutex _data_mutex; // _data_mutex need to be aquired std::vector _data; std::unordered_map > _streams; - std::mutex _fileName_mutex; + std::shared_mutex _fileName_mutex; // _fileName_mutex need to be aquired std::wstring _fileName; }; diff --git a/samples/dokan_memfs/memfs_operations.cpp b/samples/dokan_memfs/memfs_operations.cpp index db158a580..6bbcc1436 100644 --- a/samples/dokan_memfs/memfs_operations.cpp +++ b/samples/dokan_memfs/memfs_operations.cpp @@ -655,7 +655,7 @@ static NTSTATUS DOKAN_CALLBACK memfs_getfilesecurity( if (!f) return STATUS_OBJECT_NAME_NOT_FOUND; - std::lock_guard lockFile(f->security); + std::shared_lock lockFile(f->security); // This will make dokan library return a default security descriptor if (!f->security.descriptor) return STATUS_NOT_IMPLEMENTED; @@ -707,7 +707,7 @@ static NTSTATUS DOKAN_CALLBACK memfs_setfilesecurity( if (!f) return STATUS_OBJECT_NAME_NOT_FOUND; - std::lock_guard securityLock(f->security); + std::unique_lock securityLock(f->security); // SetPrivateObjectSecurity - ObjectsSecurityDescriptor // The memory for the security descriptor must be allocated from the process