mixer/Mixer: avoid locking twice

This commit is contained in:
Max Kellermann 2022-08-18 17:36:36 +02:00
parent 29eb3e9ebc
commit 2d2df25d04

View File

@ -69,12 +69,15 @@ Mixer::_Close() noexcept
int int
Mixer::LockGetVolume() Mixer::LockGetVolume()
{ {
if (IsGlobal() && !failure)
LockOpen();
const std::scoped_lock lock{mutex}; const std::scoped_lock lock{mutex};
if (open) { if (!open) {
if (IsGlobal() && !failure)
_Open();
else
return -1;
}
try { try {
return GetVolume(); return GetVolume();
} catch (...) { } catch (...) {
@ -82,8 +85,6 @@ Mixer::LockGetVolume()
failure = std::current_exception(); failure = std::current_exception();
throw; throw;
} }
} else
return -1;
} }
void void
@ -91,13 +92,16 @@ Mixer::LockSetVolume(unsigned volume)
{ {
assert(volume <= 100); assert(volume <= 100);
if (IsGlobal() && !failure)
LockOpen();
const std::scoped_lock lock{mutex}; const std::scoped_lock lock{mutex};
if (open) if (!open) {
SetVolume(volume); if (failure)
else if (failure)
std::rethrow_exception(failure); std::rethrow_exception(failure);
else if (IsGlobal())
_Open();
else
return;
}
SetVolume(volume);
} }