Skip to content

Commit

Permalink
Use .NET 9 Lock via PolyShim
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Sep 6, 2024
1 parent d30e953 commit cae7e9e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
9 changes: 5 additions & 4 deletions Gress/Completable/AutoResetProgressMuxer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;

namespace Gress.Completable;

Expand All @@ -8,7 +9,7 @@ namespace Gress.Completable;
/// </summary>
public partial class AutoResetProgressMuxer(ProgressMuxer muxer)
{
private readonly object _lock = new();
private readonly Lock _lock = new();
private readonly ProgressMuxer _muxer = muxer;

private int _pendingInputs;
Expand All @@ -25,7 +26,7 @@ public partial class AutoResetProgressMuxer(ProgressMuxer muxer)
/// </remarks>
public ICompletableProgress<Percentage> CreateInput(double weight = 1.0)
{
lock (_lock)
using (_lock.EnterScope())
{
var item = new Item(this, _muxer.CreateInput(weight));

Expand All @@ -52,15 +53,15 @@ public Item(AutoResetProgressMuxer parent, IProgress<Percentage> target)

public void Report(Percentage value)
{
lock (_parent._lock)
using (_parent._lock.EnterScope())
{
_target.Report(value);
}
}

public void ReportCompletion()
{
lock (_parent._lock)
using (_parent._lock.EnterScope())
{
if (--_parent._pendingInputs <= 0)
_parent._muxer.Reset();
Expand Down
2 changes: 1 addition & 1 deletion Gress/Gress.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.29.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
<PackageReference Include="PolyShim" Version="1.12.0" PrivateAssets="all" />
<PackageReference Include="PolyShim" Version="1.13.0" PrivateAssets="all" />
</ItemGroup>

</Project>
11 changes: 6 additions & 5 deletions Gress/ProgressCollector.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;

namespace Gress;

Expand All @@ -8,15 +9,15 @@ namespace Gress;
/// </summary>
public class ProgressCollector<T> : IProgress<T>
{
private readonly object _lock = new();
private readonly List<T> _reports = new();
private readonly Lock _lock = new();
private readonly List<T> _reports = [];

/// <summary>
/// Clears the reported progress updates.
/// </summary>
public void Reset()
{
lock (_lock)
using (_lock.EnterScope())
_reports.Clear();
}

Expand All @@ -25,14 +26,14 @@ public void Reset()
/// </summary>
public IReadOnlyList<T> GetValues()
{
lock (_lock)
using (_lock.EnterScope())
return _reports.ToArray();
}

/// <inheritdoc />
public void Report(T value)
{
lock (_lock)
using (_lock.EnterScope())
_reports.Add(value);
}
}
9 changes: 5 additions & 4 deletions Gress/ProgressExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Gress.Utils;

namespace Gress;
Expand Down Expand Up @@ -45,13 +46,13 @@ public static IProgress<T> WithDeduplication<T, TKey>(
IEqualityComparer<TKey>? comparer = null
)
{
var syncRoot = new object();
var syncRoot = new Lock();
var actualComparer = comparer ?? EqualityComparer<TKey>.Default;
var lastValueBox = new Box<TKey>();

return new DelegateProgress<T>(p =>
{
lock (syncRoot)
using (syncRoot.EnterScope())
{
var value = getKey(p);

Expand Down Expand Up @@ -85,13 +86,13 @@ public static IProgress<T> WithOrdering<T>(
IComparer<T>? comparer = null
)
{
var syncRoot = new object();
var syncRoot = new Lock();
var actualComparer = comparer ?? Comparer<T>.Default;
var lastValueBox = new Box<T>();

return new DelegateProgress<T>(p =>
{
lock (syncRoot)
using (syncRoot.EnterScope())
{
if (
lastValueBox.TryOpen(out var lastValue)
Expand Down
13 changes: 7 additions & 6 deletions Gress/ProgressMuxer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;

namespace Gress;

Expand All @@ -8,14 +9,14 @@ namespace Gress;
/// </summary>
public partial class ProgressMuxer(IProgress<Percentage> target)
{
private readonly object _lock = new();
private readonly HashSet<Input> _inputs = new();
private readonly Lock _lock = new();
private readonly HashSet<Input> _inputs = [];

private bool _anyInputReported;

private void ReportAggregatedProgress()
{
lock (_lock)
using (_lock.EnterScope())
{
var weightedSum = 0.0;
var weightedMax = 0.0;
Expand Down Expand Up @@ -43,7 +44,7 @@ public IProgress<Percentage> CreateInput(double weight = 1.0)
if (weight <= 0)
throw new ArgumentOutOfRangeException(nameof(weight), "Weight must be positive.");

lock (_lock)
using (_lock.EnterScope())
{
var input = new Input(this, weight);
_inputs.Add(input);
Expand All @@ -63,7 +64,7 @@ public IProgress<Percentage> CreateInput(double weight = 1.0)
/// </summary>
public void Reset()
{
lock (_lock)
using (_lock.EnterScope())
{
_inputs.Clear();
_anyInputReported = false;
Expand Down Expand Up @@ -91,7 +92,7 @@ public Input(ProgressMuxer parent, double weight)

public void Report(Percentage value)
{
lock (_parent._lock)
using (_parent._lock.EnterScope())
{
Progress = value;

Expand Down

0 comments on commit cae7e9e

Please sign in to comment.