treewide: replace lock_guard with scoped_lock
SonarLint reports the latter to be better: std::scoped_lock basically provides the same feature as std::lock_guard, but is more generic: It can lock several mutexes at the same time, with a deadlock prevention mechanism (see {rule:cpp:S5524}). The equivalent code to perform simultaneous locking with std::lock_guard is significantly more complex. Therefore, it is simpler to use std::scoped_lock all the time, even when locking only one mutex (there will be no performance impact). Signed-off-by: Rosen Penev <rosenp@gmail.com>
This commit is contained in:
@@ -194,7 +194,7 @@ CompositeStorage::~CompositeStorage() = default;
|
||||
Storage *
|
||||
CompositeStorage::GetMount(std::string_view uri) noexcept
|
||||
{
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
|
||||
auto result = FindStorage(uri);
|
||||
if (!result.uri.empty())
|
||||
@@ -207,7 +207,7 @@ CompositeStorage::GetMount(std::string_view uri) noexcept
|
||||
void
|
||||
CompositeStorage::Mount(const char *uri, std::unique_ptr<Storage> storage)
|
||||
{
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
|
||||
Directory &directory = root.Make(uri);
|
||||
assert(!directory.storage);
|
||||
@@ -217,7 +217,7 @@ CompositeStorage::Mount(const char *uri, std::unique_ptr<Storage> storage)
|
||||
bool
|
||||
CompositeStorage::Unmount(const char *uri)
|
||||
{
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
|
||||
return root.Unmount(uri);
|
||||
}
|
||||
@@ -246,7 +246,7 @@ CompositeStorage::FindStorage(std::string_view uri) const noexcept
|
||||
StorageFileInfo
|
||||
CompositeStorage::GetInfo(std::string_view uri, bool follow)
|
||||
{
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
|
||||
std::exception_ptr error;
|
||||
|
||||
@@ -272,7 +272,7 @@ CompositeStorage::GetInfo(std::string_view uri, bool follow)
|
||||
std::unique_ptr<StorageDirectoryReader>
|
||||
CompositeStorage::OpenDirectory(std::string_view uri)
|
||||
{
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
|
||||
auto f = FindStorage(uri);
|
||||
const Directory *directory = f.directory->Find(f.uri);
|
||||
@@ -299,7 +299,7 @@ CompositeStorage::OpenDirectory(std::string_view uri)
|
||||
std::string
|
||||
CompositeStorage::MapUTF8(std::string_view uri) const noexcept
|
||||
{
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
|
||||
auto f = FindStorage(uri);
|
||||
if (f.directory->storage == nullptr)
|
||||
@@ -311,7 +311,7 @@ CompositeStorage::MapUTF8(std::string_view uri) const noexcept
|
||||
AllocatedPath
|
||||
CompositeStorage::MapFS(std::string_view uri) const noexcept
|
||||
{
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
|
||||
auto f = FindStorage(uri);
|
||||
if (f.directory->storage == nullptr)
|
||||
@@ -323,7 +323,7 @@ CompositeStorage::MapFS(std::string_view uri) const noexcept
|
||||
std::string_view
|
||||
CompositeStorage::MapToRelativeUTF8(std::string_view uri) const noexcept
|
||||
{
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
|
||||
if (root.storage != nullptr) {
|
||||
auto result = root.storage->MapToRelativeUTF8(uri);
|
||||
|
@@ -115,7 +115,7 @@ public:
|
||||
*/
|
||||
template<typename T>
|
||||
void VisitMounts(T t) const {
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
std::string uri;
|
||||
VisitMounts(uri, root, t);
|
||||
}
|
||||
@@ -125,7 +125,7 @@ public:
|
||||
*/
|
||||
[[gnu::pure]] [[gnu::nonnull]]
|
||||
bool IsMounted(const char *storage_uri) const noexcept {
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
return IsMounted(root, storage_uri);
|
||||
}
|
||||
|
||||
|
@@ -131,7 +131,7 @@ protected:
|
||||
}
|
||||
|
||||
void LockSetDone() {
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
SetDone();
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ private:
|
||||
|
||||
/* virtual methods from CurlResponseHandler */
|
||||
void OnError(std::exception_ptr e) noexcept final {
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
postponed_error = std::move(e);
|
||||
SetDone();
|
||||
}
|
||||
|
@@ -139,7 +139,7 @@ private:
|
||||
void SetState(State _state) noexcept {
|
||||
assert(GetEventLoop().IsInside());
|
||||
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
state = _state;
|
||||
cond.notify_all();
|
||||
}
|
||||
@@ -147,7 +147,7 @@ private:
|
||||
void SetState(State _state, std::exception_ptr &&e) noexcept {
|
||||
assert(GetEventLoop().IsInside());
|
||||
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
state = _state;
|
||||
last_exception = std::move(e);
|
||||
cond.notify_all();
|
||||
|
@@ -102,7 +102,7 @@ GetInfo(SmbclientContext &ctx, Mutex &mutex, const char *path)
|
||||
struct stat st;
|
||||
|
||||
{
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
if (ctx.Stat(path, st) != 0)
|
||||
throw MakeErrno("Failed to access file");
|
||||
}
|
||||
@@ -137,7 +137,7 @@ SmbclientStorage::OpenDirectory(std::string_view uri_utf8)
|
||||
SMBCFILE *handle;
|
||||
|
||||
{
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
handle = ctx.OpenDirectory(mapped.c_str());
|
||||
}
|
||||
|
||||
@@ -158,14 +158,14 @@ SkipNameFS(PathTraitsFS::const_pointer name) noexcept
|
||||
|
||||
SmbclientDirectoryReader::~SmbclientDirectoryReader()
|
||||
{
|
||||
const std::lock_guard<Mutex> lock(storage.mutex);
|
||||
const std::scoped_lock<Mutex> lock(storage.mutex);
|
||||
storage.ctx.CloseDirectory(handle);
|
||||
}
|
||||
|
||||
const char *
|
||||
SmbclientDirectoryReader::Read() noexcept
|
||||
{
|
||||
const std::lock_guard<Mutex> protect(storage.mutex);
|
||||
const std::scoped_lock<Mutex> protect(storage.mutex);
|
||||
|
||||
while (auto e = storage.ctx.ReadDirectory(handle)) {
|
||||
name = e->name;
|
||||
|
@@ -159,7 +159,7 @@ UdisksStorage::SetMountPoint(Path mount_point)
|
||||
void
|
||||
UdisksStorage::LockSetMountPoint(Path mount_point)
|
||||
{
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
SetMountPoint(mount_point);
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ UdisksStorage::OnListReply(ODBus::Message reply) noexcept
|
||||
return;
|
||||
}
|
||||
} catch (...) {
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
mount_error = std::current_exception();
|
||||
want_mount = false;
|
||||
cond.notify_all();
|
||||
@@ -247,7 +247,7 @@ try {
|
||||
mount_request.Send(connection, *msg.Get(),
|
||||
[this](auto o) { return OnMountNotify(std::move(o)); });
|
||||
} catch (...) {
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
mount_error = std::current_exception();
|
||||
want_mount = false;
|
||||
cond.notify_all();
|
||||
@@ -266,7 +266,7 @@ try {
|
||||
const char *mount_path = i.GetString();
|
||||
LockSetMountPoint(Path::FromFS(mount_path));
|
||||
} catch (...) {
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
mount_error = std::current_exception();
|
||||
want_mount = false;
|
||||
cond.notify_all();
|
||||
@@ -304,7 +304,7 @@ try {
|
||||
mount_request.Send(connection, *msg.Get(),
|
||||
[this](auto u) { return OnUnmountNotify(std::move(u)); });
|
||||
} catch (...) {
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
mount_error = std::current_exception();
|
||||
mounted_storage.reset();
|
||||
cond.notify_all();
|
||||
@@ -316,12 +316,12 @@ try {
|
||||
using namespace ODBus;
|
||||
reply.CheckThrowError();
|
||||
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
mount_error = {};
|
||||
mounted_storage.reset();
|
||||
cond.notify_all();
|
||||
} catch (...) {
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
mount_error = std::current_exception();
|
||||
mounted_storage.reset();
|
||||
cond.notify_all();
|
||||
|
Reference in New Issue
Block a user