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:
4
src/input/cache/Item.cxx
vendored
4
src/input/cache/Item.cxx
vendored
@@ -37,14 +37,14 @@ InputCacheItem::~InputCacheItem() noexcept
|
||||
void
|
||||
InputCacheItem::AddLease(InputCacheLease &lease) noexcept
|
||||
{
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
leases.push_back(lease);
|
||||
}
|
||||
|
||||
void
|
||||
InputCacheItem::RemoveLease(InputCacheLease &lease) noexcept
|
||||
{
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
auto i = leases.iterator_to(lease);
|
||||
if (i == next_lease)
|
||||
++next_lease;
|
||||
|
||||
2
src/input/cache/Item.hxx
vendored
2
src/input/cache/Item.hxx
vendored
@@ -63,7 +63,7 @@ public:
|
||||
using BufferingInputStream::size;
|
||||
|
||||
bool IsInUse() const noexcept {
|
||||
const std::lock_guard<Mutex> lock(mutex);
|
||||
const std::scoped_lock<Mutex> lock(mutex);
|
||||
return !leases.empty();
|
||||
}
|
||||
|
||||
|
||||
8
src/input/cache/Stream.cxx
vendored
8
src/input/cache/Stream.cxx
vendored
@@ -36,7 +36,7 @@ CacheInputStream::Check()
|
||||
const ScopeUnlock unlock(mutex);
|
||||
|
||||
auto &i = GetCacheItem();
|
||||
const std::lock_guard<Mutex> protect(i.mutex);
|
||||
const std::scoped_lock<Mutex> protect(i.mutex);
|
||||
|
||||
i.Check();
|
||||
}
|
||||
@@ -60,7 +60,7 @@ CacheInputStream::IsAvailable() const noexcept
|
||||
const ScopeUnlock unlock(mutex);
|
||||
|
||||
auto &i = GetCacheItem();
|
||||
const std::lock_guard<Mutex> protect(i.mutex);
|
||||
const std::scoped_lock<Mutex> protect(i.mutex);
|
||||
|
||||
return i.IsAvailable(_offset);
|
||||
}
|
||||
@@ -76,7 +76,7 @@ CacheInputStream::Read(std::unique_lock<Mutex> &lock,
|
||||
|
||||
{
|
||||
const ScopeUnlock unlock(mutex);
|
||||
const std::lock_guard<Mutex> protect(i.mutex);
|
||||
const std::scoped_lock<Mutex> protect(i.mutex);
|
||||
|
||||
nbytes = i.Read(lock, _offset, ptr, read_size);
|
||||
}
|
||||
@@ -91,6 +91,6 @@ CacheInputStream::OnInputCacheAvailable() noexcept
|
||||
auto &i = GetCacheItem();
|
||||
const ScopeUnlock unlock(i.mutex);
|
||||
|
||||
const std::lock_guard<Mutex> protect(mutex);
|
||||
const std::scoped_lock<Mutex> protect(mutex);
|
||||
InvokeOnAvailable();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user