From c9de0b512a7270adb783650fa520fcc26bfb40d4 Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Wed, 13 May 2026 17:41:36 -0500 Subject: [PATCH] Use shared_mutex for ReadDir state - Replace atomic shared pointer loads/stores with shared/unique locking. - Store the configured readdir mode directly as std::string. - Copy the active ReadDir implementation under lock before invoking it. --- src/fuse_readdir.cpp | 36 +++++++++++++++++++++--------------- src/fuse_readdir.hpp | 7 ++++--- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/fuse_readdir.cpp b/src/fuse_readdir.cpp index d506d3d8..f26c34f1 100644 --- a/src/fuse_readdir.cpp +++ b/src/fuse_readdir.cpp @@ -48,30 +48,29 @@ FUSE::ReadDir::ReadDir(const std::string_view s_) std::string FUSE::ReadDir::to_string() const { - std::shared_ptr str; + std::shared_lock lk(_mutex); - str = std::atomic_load(&_str); - - return (*str); + return _str; } int FUSE::ReadDir::from_string(const std::string_view str_) { - std::shared_ptr tmp_str; std::shared_ptr tmp_readdir; tmp_readdir = FUSE::ReadDirFactory::make(str_); if(!tmp_readdir) return -EINVAL; - tmp_str = std::make_shared(str_); - std::atomic_store(&_str,tmp_str); + { + std::unique_lock lk(_mutex); - if(_initialized == false) - return 0; + _str = str_; + if(_initialized == false) + return 0; - std::atomic_store(&_impl,tmp_readdir); + _impl = std::move(tmp_readdir); + } return 0; } @@ -114,7 +113,10 @@ FUSE::ReadDir::operator()(const fuse_req_ctx_t *ctx_, int rv; std::shared_ptr readdir; - readdir = std::atomic_load(&_impl); + { + std::shared_lock lk(_mutex); + readdir = _impl; + } if(!readdir) fatal::abort("readdir impl is null"); @@ -128,10 +130,14 @@ FUSE::ReadDir::operator()(const fuse_req_ctx_t *ctx_, void FUSE::ReadDir::initialize() { - std::shared_ptr str; + std::string str; - str = std::atomic_load(&_str); - _initialized = true; + { + std::unique_lock lk(_mutex); - from_string(*str); + str = _str; + _initialized = true; + } + + from_string(str); } diff --git a/src/fuse_readdir.hpp b/src/fuse_readdir.hpp index afcff6da..15dd0b8c 100644 --- a/src/fuse_readdir.hpp +++ b/src/fuse_readdir.hpp @@ -22,9 +22,9 @@ #include "tofrom_string.hpp" #include "fuse_readdir_base.hpp" -#include #include #include +#include namespace FUSE @@ -59,8 +59,9 @@ namespace FUSE void initialize(); private: - std::atomic _initialized = false; - std::shared_ptr _str; + bool _initialized = false; + mutable std::shared_mutex _mutex; + std::string _str; std::shared_ptr _impl; }; }