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,21 +69,22 @@ 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) {
try { if (IsGlobal() && !failure)
return GetVolume(); _Open();
} catch (...) { else
_Close(); return -1;
failure = std::current_exception(); }
throw;
} try {
} else return GetVolume();
return -1; } catch (...) {
_Close();
failure = std::current_exception();
throw;
}
} }
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);
} }