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:
Rosen Penev
2021-11-11 16:19:32 -08:00
parent a8c77a6fba
commit 4e0e4c00bf
64 changed files with 196 additions and 196 deletions
+11 -11
View File
@@ -311,13 +311,13 @@ private:
gcc_pure
bool LockIsActive() const noexcept {
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
return active;
}
gcc_pure
bool LockIsActiveAndNotWaiting() const noexcept {
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
return active && !waiting;
}
@@ -383,7 +383,7 @@ private:
void LockCaughtError() noexcept {
period_buffer.Clear();
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
error = std::current_exception();
active = false;
waiting = false;
@@ -398,7 +398,7 @@ private:
*/
void OnSilenceTimer() noexcept {
{
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
assert(active);
waiting = false;
}
@@ -464,7 +464,7 @@ AlsaOutput::AlsaOutput(EventLoop &_loop, const ConfigBlock &block)
std::map<std::string, std::string>
AlsaOutput::GetAttributes() const noexcept
{
const std::lock_guard<Mutex> lock(attributes_mutex);
const std::scoped_lock<Mutex> lock(attributes_mutex);
return {
{"allowed_formats", Alsa::ToString(allowed_formats)},
@@ -478,11 +478,11 @@ void
AlsaOutput::SetAttribute(std::string &&name, std::string &&value)
{
if (name == "allowed_formats") {
const std::lock_guard<Mutex> lock(attributes_mutex);
const std::scoped_lock<Mutex> lock(attributes_mutex);
allowed_formats = Alsa::AllowedFormat::ParseList(value);
#ifdef ENABLE_DSD
} else if (name == "dop") {
const std::lock_guard<Mutex> lock(attributes_mutex);
const std::scoped_lock<Mutex> lock(attributes_mutex);
if (value == "0")
dop_setting = false;
else if (value == "1")
@@ -790,7 +790,7 @@ AlsaOutput::Open(AudioFormat &audio_format)
#endif
{
const std::lock_guard<Mutex> lock(attributes_mutex);
const std::scoped_lock<Mutex> lock(attributes_mutex);
#ifdef ENABLE_DSD
dop = dop_setting;
#endif
@@ -966,7 +966,7 @@ AlsaOutput::CopyRingToPeriodBuffer() noexcept
period_buffer.AppendBytes(nbytes);
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
/* notify the OutputThread that there is now
room in ring_buffer */
cond.notify_one();
@@ -1276,7 +1276,7 @@ try {
}
{
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
assert(active);
@@ -1316,7 +1316,7 @@ try {
smaller than the ALSA-PCM buffer */
{
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
waiting = true;
cond.notify_one();
}
+4 -4
View File
@@ -121,7 +121,7 @@ private:
void Disconnect() noexcept;
void Shutdown(const char *reason) noexcept {
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
error = std::make_exception_ptr(FormatRuntimeError("JACK connection shutdown: %s",
reason));
}
@@ -185,7 +185,7 @@ public:
private:
bool LockWasShutdown() const noexcept {
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
return !!error;
}
};
@@ -700,7 +700,7 @@ JackOutput::Play(const void *chunk, size_t size)
while (true) {
{
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
if (error)
std::rethrow_exception(error);
@@ -730,7 +730,7 @@ inline bool
JackOutput::Pause()
{
{
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
interrupted = false;
if (error)
std::rethrow_exception(error);
+2 -2
View File
@@ -47,7 +47,7 @@ HttpdClient::Close() noexcept
void
HttpdClient::LockClose() noexcept
{
const std::lock_guard<Mutex> protect(httpd.mutex);
const std::scoped_lock<Mutex> protect(httpd.mutex);
Close();
}
@@ -251,7 +251,7 @@ HttpdClient::GetBytesTillMetaData() const noexcept
inline bool
HttpdClient::TryWrite() noexcept
{
const std::lock_guard<Mutex> protect(httpd.mutex);
const std::scoped_lock<Mutex> protect(httpd.mutex);
assert(state == State::RESPONSE);
+1 -1
View File
@@ -202,7 +202,7 @@ public:
*/
gcc_pure
bool LockHasClients() const noexcept {
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
return HasClients();
}
@@ -104,7 +104,7 @@ HttpdOutput::OnDeferredBroadcast() noexcept
/* this method runs in the IOThread; it broadcasts pages from
our own queue to all clients */
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
while (!pages.empty()) {
PagePtr page = std::move(pages.front());
@@ -126,7 +126,7 @@ HttpdOutput::OnAccept(UniqueSocketDescriptor fd,
/* the listener socket has become readable - a client has
connected */
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
/* can we allow additional client */
if (open && (clients_max == 0 || clients.size() < clients_max))
@@ -186,7 +186,7 @@ HttpdOutput::Open(AudioFormat &audio_format)
assert(!open);
assert(clients.empty());
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
OpenEncoder(audio_format);
@@ -208,7 +208,7 @@ HttpdOutput::Close() noexcept
BlockingCall(GetEventLoop(), [this](){
defer_broadcast.Cancel();
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
open = false;
clients.clear_and_dispose(DeleteDisposer());
});
@@ -261,7 +261,7 @@ HttpdOutput::BroadcastPage(PagePtr page) noexcept
assert(page != nullptr);
{
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
pages.emplace(std::move(page));
}
@@ -281,7 +281,7 @@ HttpdOutput::BroadcastFromEncoder()
PagePtr page;
while ((page = ReadPage()) != nullptr) {
const std::lock_guard<Mutex> lock(mutex);
const std::scoped_lock<Mutex> lock(mutex);
pages.emplace(std::move(page));
empty = false;
}
@@ -373,7 +373,7 @@ HttpdOutput::SendTag(const Tag &tag)
metadata = icy_server_metadata_page(tag, &types[0]);
if (metadata != nullptr) {
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
for (auto &client : clients)
client.PushMetaData(metadata);
}
@@ -383,7 +383,7 @@ HttpdOutput::SendTag(const Tag &tag)
inline void
HttpdOutput::CancelAllClients() noexcept
{
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
while (!pages.empty()) {
PagePtr page = std::move(pages.front());
+2 -2
View File
@@ -398,7 +398,7 @@ SlesOutput::Cancel() noexcept
LogWarning(sles_domain,
"AndroidSimpleBufferQueue.Clear() failed");
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
n_queued = 0;
filled = 0;
}
@@ -423,7 +423,7 @@ SlesOutput::Pause()
inline void
SlesOutput::PlayedCallback()
{
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
assert(n_queued > 0);
--n_queued;
cond.notify_one();
+2 -2
View File
@@ -53,7 +53,7 @@ SnapcastClient::Close() noexcept
void
SnapcastClient::LockClose() noexcept
{
const std::lock_guard<Mutex> protect(output.mutex);
const std::scoped_lock<Mutex> protect(output.mutex);
Close();
}
@@ -70,7 +70,7 @@ SnapcastClient::Push(SnapcastChunkPtr chunk) noexcept
SnapcastChunkPtr
SnapcastClient::LockPopQueue() noexcept
{
const std::lock_guard<Mutex> protect(output.mutex);
const std::scoped_lock<Mutex> protect(output.mutex);
if (chunks.empty())
return nullptr;
+1 -1
View File
@@ -134,7 +134,7 @@ public:
*/
[[gnu::pure]]
bool LockHasClients() const noexcept {
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
return HasClients();
}
@@ -114,7 +114,7 @@ SnapcastOutput::OnAccept(UniqueSocketDescriptor fd,
/* the listener socket has become readable - a client has
connected */
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
/* can we allow additional client */
if (open)
@@ -152,7 +152,7 @@ SnapcastOutput::Open(AudioFormat &audio_format)
assert(!open);
assert(clients.empty());
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
OpenEncoder(audio_format);
@@ -174,7 +174,7 @@ SnapcastOutput::Close() noexcept
BlockingCall(GetEventLoop(), [this](){
inject_event.Cancel();
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
open = false;
clients.clear_and_dispose(DeleteDisposer{});
});
@@ -188,7 +188,7 @@ SnapcastOutput::Close() noexcept
void
SnapcastOutput::OnInject() noexcept
{
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
while (!chunks.empty()) {
const auto chunk = std::move(chunks.front());
@@ -296,7 +296,7 @@ SnapcastOutput::SendTag(const Tag &tag)
const ConstBuffer payload(json.data(), json.size());
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
// TODO: enqueue StreamTags, don't send directly
for (auto &client : clients)
client.SendStreamTags(payload.ToVoid());
@@ -344,7 +344,7 @@ SnapcastOutput::Play(const void *chunk, size_t size)
unflushed_input = 0;
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
if (chunks.empty())
inject_event.Schedule();
@@ -382,7 +382,7 @@ SnapcastOutput::Drain()
void
SnapcastOutput::Cancel() noexcept
{
const std::lock_guard<Mutex> protect(mutex);
const std::scoped_lock<Mutex> protect(mutex);
ClearQueue(chunks);