Skip to content

Commit

Permalink
Use a better variable name for the volume semaphore.
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimian committed Feb 2, 2025
1 parent 70e5779 commit 17ceb47
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/TotalMixVC/Communicator/VolumeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public class VolumeManager(ISender sender) : IDisposable
/// </summary>
private const string DimAddress = "/1/mainDim";

private readonly SemaphoreSlim _volumeMutex = new(1);
/// <summary>
/// A write semaphore (mutex) for the volume value properties Volume, VolumeDecibels and Dim.
/// </summary>
private readonly SemaphoreSlim _volumeSemaphore = new(1);

/// <summary>
/// Gets or sets the implementation of ISender which is used to send messages to the device.
Expand Down Expand Up @@ -175,7 +178,7 @@ public async Task<bool> ReceiveVolumeAsync(
// Reset the volume back to an initial state so that the caller is forced to
// request device volume before continuing as this may have changed while the
// device was offline.
await _volumeMutex
await _volumeSemaphore
.WaitAsync(cancellationTokenSource?.Token ?? default)
.ConfigureAwait(false);
try
Expand All @@ -186,7 +189,7 @@ await _volumeMutex
}
finally
{
_volumeMutex.Release();
_volumeSemaphore.Release();
}

throw;
Expand Down Expand Up @@ -225,7 +228,7 @@ public async Task<bool> IncreaseVolumeAsync(bool fine = false)
return false;
}

await _volumeMutex.WaitAsync().ConfigureAwait(false);
await _volumeSemaphore.WaitAsync().ConfigureAwait(false);
try
{
// Calculate the new volume.
Expand Down Expand Up @@ -271,7 +274,7 @@ public async Task<bool> IncreaseVolumeAsync(bool fine = false)
}
finally
{
_volumeMutex.Release();
_volumeSemaphore.Release();
}
}

Expand All @@ -290,7 +293,7 @@ public async Task<bool> DecreaseVolumeAsync(bool fine = false)
return false;
}

await _volumeMutex.WaitAsync().ConfigureAwait(false);
await _volumeSemaphore.WaitAsync().ConfigureAwait(false);
try
{
// Calculate the new volume.
Expand Down Expand Up @@ -329,7 +332,7 @@ public async Task<bool> DecreaseVolumeAsync(bool fine = false)
}
finally
{
_volumeMutex.Release();
_volumeSemaphore.Release();
}
}

Expand All @@ -347,7 +350,7 @@ public async Task<bool> ToggloDimAsync()
return false;
}

await _volumeMutex.WaitAsync().ConfigureAwait(false);
await _volumeSemaphore.WaitAsync().ConfigureAwait(false);
try
{
// To toggle the dim function, we must simply send 1, not the actual 0 or 1 value.
Expand All @@ -357,7 +360,7 @@ public async Task<bool> ToggloDimAsync()
}
finally
{
_volumeMutex.Release();
_volumeSemaphore.Release();
}
}

Expand Down Expand Up @@ -408,7 +411,7 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_volumeMutex.Dispose();
_volumeSemaphore.Dispose();
}
}

Expand Down Expand Up @@ -446,7 +449,7 @@ m.Address is VolumeDecibelsAddress or VolumeAddress or DimAddress && m.Count is
)
)
{
await _volumeMutex.WaitAsync().ConfigureAwait(false);
await _volumeSemaphore.WaitAsync().ConfigureAwait(false);
try
{
if (message.Address == VolumeDecibelsAddress)
Expand All @@ -471,7 +474,7 @@ m.Address is VolumeDecibelsAddress or VolumeAddress or DimAddress && m.Count is
}
finally
{
_volumeMutex.Release();
_volumeSemaphore.Release();
}
}

Expand Down

0 comments on commit 17ceb47

Please sign in to comment.