Skip to content

Commit

Permalink
MemFS - Replace std::mutex by std::shared_mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
Liryna committed Feb 13, 2022
1 parent 509ecab commit cf3ee14
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
18 changes: 9 additions & 9 deletions samples/dokan_memfs/filenode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock(_data_mutex);
std::shared_lock lock(_data_mutex);
if (static_cast<size_t>(offset + bufferlength) > _data.size())
bufferlength = (_data.size() > static_cast<size_t>(offset))
? static_cast<DWORD>(_data.size() - offset)
Expand All @@ -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<std::mutex> lock(_data_mutex);
std::unique_lock lock(_data_mutex);
if (static_cast<size_t>(offset + number_of_bytes_to_write) > _data.size())
_data.resize(static_cast<size_t>(offset + number_of_bytes_to_write));

Expand All @@ -71,38 +71,38 @@ DWORD filenode::write(LPCVOID buffer, DWORD number_of_bytes_to_write,
}

const LONGLONG filenode::get_filesize() {
std::lock_guard<std::mutex> lock(_data_mutex);
std::shared_lock lock(_data_mutex);
return static_cast<LONGLONG>(_data.size());
}

void filenode::set_endoffile(const LONGLONG& byte_offset) {
std::lock_guard<std::mutex> lock(_data_mutex);
std::unique_lock lock(_data_mutex);
_data.resize(static_cast<size_t>(byte_offset));
}

const std::wstring filenode::get_filename() {
std::lock_guard<std::mutex> lock(_fileName_mutex);
std::shared_lock lock(_fileName_mutex);
return _fileName;
}

void filenode::set_filename(const std::wstring& f) {
std::lock_guard<std::mutex> lock(_fileName_mutex);
std::unique_lock lock(_fileName_mutex);
_fileName = f;
}

void filenode::add_stream(const std::shared_ptr<filenode>& stream) {
std::lock_guard<std::mutex> lock(_data_mutex);
std::unique_lock lock(_data_mutex);
_streams[stream->get_filename()] = stream;
}

void filenode::remove_stream(const std::shared_ptr<filenode>& stream) {
std::lock_guard<std::mutex> lock(_data_mutex);
std::unique_lock lock(_data_mutex);
_streams.erase(stream->get_filename());
}

std::unordered_map<std::wstring, std::shared_ptr<filenode> >
filenode::get_streams() {
std::lock_guard<std::mutex> lock(_data_mutex);
std::shared_lock lock(_data_mutex);
return _streams;
}
} // namespace memfs
8 changes: 4 additions & 4 deletions samples/dokan_memfs/filenode.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ THE SOFTWARE.
#include <WinBase.h>
#include <atomic>
#include <filesystem>
#include <mutex>
#include <shared_mutex>
#include <sstream>
#include <string>
#include <unordered_map>

namespace memfs {

// Safe class wrapping a Win32 Security Descriptor
struct security_informations : std::mutex {
struct security_informations : std::shared_mutex {
std::unique_ptr<byte[]> descriptor = nullptr;
DWORD descriptor_size = 0;

Expand Down Expand Up @@ -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<uint8_t> _data;
std::unordered_map<std::wstring, std::shared_ptr<filenode> > _streams;

std::mutex _fileName_mutex;
std::shared_mutex _fileName_mutex;
// _fileName_mutex need to be aquired
std::wstring _fileName;
};
Expand Down
4 changes: 2 additions & 2 deletions samples/dokan_memfs/memfs_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ static NTSTATUS DOKAN_CALLBACK memfs_getfilesecurity(

if (!f) return STATUS_OBJECT_NAME_NOT_FOUND;

std::lock_guard<std::mutex> 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;
Expand Down Expand Up @@ -707,7 +707,7 @@ static NTSTATUS DOKAN_CALLBACK memfs_setfilesecurity(

if (!f) return STATUS_OBJECT_NAME_NOT_FOUND;

std::lock_guard<std::mutex> securityLock(f->security);
std::unique_lock securityLock(f->security);

// SetPrivateObjectSecurity - ObjectsSecurityDescriptor
// The memory for the security descriptor must be allocated from the process
Expand Down

0 comments on commit cf3ee14

Please sign in to comment.