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.
This commit is contained in:
Antonio SJ Musumeci 2026-05-13 17:41:36 -05:00
parent acf0211571
commit c9de0b512a
2 changed files with 25 additions and 18 deletions

View file

@ -48,30 +48,29 @@ FUSE::ReadDir::ReadDir(const std::string_view s_)
std::string std::string
FUSE::ReadDir::to_string() const FUSE::ReadDir::to_string() const
{ {
std::shared_ptr<std::string> str; std::shared_lock<std::shared_mutex> lk(_mutex);
str = std::atomic_load(&_str); return _str;
return (*str);
} }
int int
FUSE::ReadDir::from_string(const std::string_view str_) FUSE::ReadDir::from_string(const std::string_view str_)
{ {
std::shared_ptr<std::string> tmp_str;
std::shared_ptr<FUSE::ReadDirBase> tmp_readdir; std::shared_ptr<FUSE::ReadDirBase> tmp_readdir;
tmp_readdir = FUSE::ReadDirFactory::make(str_); tmp_readdir = FUSE::ReadDirFactory::make(str_);
if(!tmp_readdir) if(!tmp_readdir)
return -EINVAL; return -EINVAL;
tmp_str = std::make_shared<std::string>(str_); {
std::atomic_store(&_str,tmp_str); std::unique_lock<std::shared_mutex> lk(_mutex);
if(_initialized == false) _str = str_;
return 0; if(_initialized == false)
return 0;
std::atomic_store(&_impl,tmp_readdir); _impl = std::move(tmp_readdir);
}
return 0; return 0;
} }
@ -114,7 +113,10 @@ FUSE::ReadDir::operator()(const fuse_req_ctx_t *ctx_,
int rv; int rv;
std::shared_ptr<FUSE::ReadDirBase> readdir; std::shared_ptr<FUSE::ReadDirBase> readdir;
readdir = std::atomic_load(&_impl); {
std::shared_lock<std::shared_mutex> lk(_mutex);
readdir = _impl;
}
if(!readdir) if(!readdir)
fatal::abort("readdir impl is null"); fatal::abort("readdir impl is null");
@ -128,10 +130,14 @@ FUSE::ReadDir::operator()(const fuse_req_ctx_t *ctx_,
void void
FUSE::ReadDir::initialize() FUSE::ReadDir::initialize()
{ {
std::shared_ptr<std::string> str; std::string str;
str = std::atomic_load(&_str); {
_initialized = true; std::unique_lock<std::shared_mutex> lk(_mutex);
from_string(*str); str = _str;
_initialized = true;
}
from_string(str);
} }

View file

@ -22,9 +22,9 @@
#include "tofrom_string.hpp" #include "tofrom_string.hpp"
#include "fuse_readdir_base.hpp" #include "fuse_readdir_base.hpp"
#include <atomic>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <shared_mutex>
namespace FUSE namespace FUSE
@ -59,8 +59,9 @@ namespace FUSE
void initialize(); void initialize();
private: private:
std::atomic<bool> _initialized = false; bool _initialized = false;
std::shared_ptr<std::string> _str; mutable std::shared_mutex _mutex;
std::string _str;
std::shared_ptr<FUSE::ReadDirBase> _impl; std::shared_ptr<FUSE::ReadDirBase> _impl;
}; };
} }