Skip to content

Commit

Permalink
[Add] Use the new Lock object (#139)
Browse files Browse the repository at this point in the history
Co-authored-by: Apollo3zehn <[email protected]>
  • Loading branch information
Apollo3zehn and Apollo3zehn authored Aug 8, 2024
1 parent 8c75990 commit 000411b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Nexus/Services/MemoryTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal class MemoryTracker : IMemoryTracker
private readonly DataOptions _dataOptions;
private readonly List<SemaphoreSlim> _retrySemaphores = [];
private readonly ILogger<IMemoryTracker> _logger;
private readonly Lock _lock = new();

public MemoryTracker(IOptions<DataOptions> dataOptions, ILogger<IMemoryTracker> logger)
{
Expand All @@ -59,7 +60,7 @@ public async Task<AllocationRegistration> RegisterAllocationAsync(long minimumBy
while (true)
{
// get exclusive access to _consumedBytes and _retrySemaphores
lock (this)
lock (_lock)
{
var fractionOfRemainingBytes = _consumedBytes >= _dataOptions.TotalBufferMemoryConsumption
? 0
Expand Down Expand Up @@ -107,7 +108,7 @@ public async Task<AllocationRegistration> RegisterAllocationAsync(long minimumBy
public void UnregisterAllocation(AllocationRegistration allocationRegistration)
{
// get exclusive access to _consumedBytes and _retrySemaphores
lock (this)
lock (_lock)
{
_logger.LogTrace("Release {ByteCount} bytes ({MegaByteCount} MB)", allocationRegistration.ActualByteCount, allocationRegistration.ActualByteCount / 1024 / 1024);
SetConsumedBytesAndTriggerWaitingTasks(-allocationRegistration.ActualByteCount);
Expand Down
2 changes: 1 addition & 1 deletion tests/Nexus.Tests/Other/OptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Other;

public class OptionsTests
{
private static readonly object _lock = new();
private static readonly Lock _lock = new();

[InlineData(GeneralOptions.Section, typeof(GeneralOptions))]
[InlineData(DataOptions.Section, typeof(DataOptions))]
Expand Down

0 comments on commit 000411b

Please sign in to comment.