Skip to content

Commit 35ff5f2

Browse files
Cleanup and simplification.
1 parent 9369cd1 commit 35ff5f2

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

GlobalSuppressions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This file is used by Code Analysis to maintain SuppressMessage
2+
// attributes that are applied to this project.
3+
// Project-level suppressions either have no target or are given
4+
// a specific target and scoped to a namespace, type, member, etc.
5+
6+
using System.Diagnostics.CodeAnalysis;
7+
8+
[assembly: SuppressMessage("Usage", "CA1816:Dispose methods should call SuppressFinalize", Justification = "<Pending>", Scope = "member", Target = "~M:Open.Threading.LockBase`1.Dispose")]
9+
[assembly: SuppressMessage("Usage", "CA2219:Do not raise exceptions in finally clauses", Justification = "<Pending>", Scope = "member", Target = "~M:Open.Threading.ReadWriteHelper`1.Execute(`0,Open.Threading.LockType,System.Action{System.Threading.ReaderWriterLockSlim},System.Nullable{System.Int32},System.Boolean)~System.Boolean")]

Open.Threading.ReadWrite.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
66
<LangVersion>latest</LangVersion>
77
<Nullable>enable</Nullable>
8+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
89
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
910
<Authors>electricessence</Authors>
1011
<Description>
@@ -40,7 +41,7 @@
4041
</ItemGroup>
4142

4243
<ItemGroup>
43-
<PackageReference Include="Open.Disposable.ObjectPools" Version="2.5.0" />
44+
<PackageReference Include="Open.Disposable.ObjectPools" Version="2.6.0" />
4445
</ItemGroup>
4546

4647
</Project>

ReadWriteHelper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class ReadWriteHelper<TKey> : DeferredCleanupBase
1919

2020
class ReaderWriterLockTracker : DisposableBase
2121
{
22-
readonly HashSet<object> _registry = new HashSet<object>();
22+
readonly HashSet<object> _registry = new();
2323

2424
public ReaderWriterLockSlim? Lock;
2525

@@ -85,10 +85,10 @@ protected override void OnDispose()
8585
readonly ConcurrentQueueObjectPool<ReaderWriterLockTracker> LockPool;
8686

8787
readonly ConcurrentDictionary<TKey, ReaderWriterLockTracker> Locks
88-
= new ConcurrentDictionary<TKey, ReaderWriterLockTracker>();
88+
= new();
8989

9090
readonly ReaderWriterLockSlim CleanupManager
91-
= new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
91+
= new(LockRecursionPolicy.SupportsRecursion);
9292

9393
public ReadWriteHelper() : this(false)
9494
{
@@ -191,7 +191,7 @@ public LockRecursionPolicy RecursionPolicy
191191
try
192192
{
193193
// result.Lock will only be null if the tracker has been disposed.
194-
lockHeld = AcquireLock(result.Lock, type, millisecondsTimeout, throwsOnTimeout);
194+
lockHeld = ReadWriteHelper<TKey>.AcquireLock(result.Lock, type, millisecondsTimeout, throwsOnTimeout);
195195
}
196196
catch (LockRecursionException lrex)
197197
{
@@ -227,7 +227,7 @@ void Debug_TrackerDisposedWhileInUse(object sender, EventArgs e)
227227
// Debug.Fail("Attempting to dispose a tracker that is still availalbe in the pool.");
228228
}
229229

230-
private bool AcquireLock(ReaderWriterLockSlim? target, LockType type, int? millisecondsTimeout = null, bool throwsOnTimeout = false)
230+
private static bool AcquireLock(ReaderWriterLockSlim? target, LockType type, int? millisecondsTimeout = null, bool throwsOnTimeout = false)
231231
{
232232
if (target is null)
233233
throw new ArgumentNullException(nameof(target));
@@ -318,7 +318,7 @@ private bool Execute<T>(
318318
int? millisecondsTimeout = null,
319319
bool throwsOnTimeout = false)
320320
{
321-
T r = default;
321+
T r = default!;
322322

323323
var acquired = Execute(key, type, (rwlock) =>
324324
{

ReaderWriterLockSlimExensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ public static bool TryReadValue<T>(this ReaderWriterLockSlim target,
751751
ValidateMillisecondsTimeout(millisecondsTimeout);
752752
Contract.EndContractBlock();
753753

754-
T r = default;
754+
T r = default!;
755755
var lockHeld = target.Read(() => r = valueFactory(), millisecondsTimeout);
756756
result = r!;
757757
return lockHeld;
@@ -775,7 +775,7 @@ public static bool TryWriteValue<T>(this ReaderWriterLockSlim target,
775775
ValidateMillisecondsTimeout(millisecondsTimeout);
776776
Contract.EndContractBlock();
777777

778-
T r = default;
778+
T r = default!;
779779
var lockHeld = target.Write(() => r = valueFactory(), millisecondsTimeout);
780780
result = r!;
781781
return lockHeld;
@@ -795,7 +795,7 @@ public static T ReadValue<T>(this ReaderWriterLockSlim target,
795795
ValidateMillisecondsTimeout(millisecondsTimeout);
796796
Contract.EndContractBlock();
797797

798-
T result = default;
798+
T result = default!;
799799
target.Read(() => result = valueFactory(), millisecondsTimeout, true);
800800
return result!;
801801
}
@@ -814,7 +814,7 @@ public static T WriteValue<T>(this ReaderWriterLockSlim target,
814814
ValidateMillisecondsTimeout(millisecondsTimeout);
815815
Contract.EndContractBlock();
816816

817-
T result = default;
817+
T result = default!;
818818
target.Write(() => result = valueFactory(), millisecondsTimeout, true);
819819
return result!;
820820
}

UpgradableReadLock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace Open.Threading
3737
/// </summary>
3838
public sealed class UpgradableReadLock : LockBase<ReaderWriterLockSlim>
3939
{
40-
readonly object _sync = new object();
40+
readonly object _sync = new();
4141
WriteLock? _upgraded;
4242
public UpgradableReadLock(ReaderWriterLockSlim target, int? millisecondsTimeout = null, bool throwIfTimeout = true)
4343
: base(target, target.EnterUpgradeableReadLock(millisecondsTimeout, throwIfTimeout))

0 commit comments

Comments
 (0)