Skip to content

Commit c8dcc2c

Browse files
committed
🧵 adapt Lock for .NET >= 9
1 parent af608c9 commit c8dcc2c

File tree

14 files changed

+72
-28
lines changed

14 files changed

+72
-28
lines changed

src/Cuemon.Core/Disposable.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ namespace Cuemon
88
/// <seealso cref="IDisposable" />
99
public abstract class Disposable : IDisposable
1010
{
11+
#if NET9_0_OR_GREATER
12+
private readonly System.Threading.Lock _lock = new();
13+
#else
1114
private readonly object _lock = new();
15+
#endif
1216

1317
/// <summary>
1418
/// Gets a value indicating whether this <see cref="Disposable"/> object is disposed.

src/Cuemon.Core/Runtime/Dependency.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ public abstract class Dependency : IDependency
1212
{
1313
private IEnumerable<IWatcher> _watchers;
1414
private readonly Func<EventHandler<WatcherEventArgs>, IEnumerable<IWatcher>> _watchersHandler;
15-
private readonly object _locker = new();
15+
#if NET9_0_OR_GREATER
16+
private readonly System.Threading.Lock _lock = new();
17+
#else
18+
private readonly object _lock = new();
19+
#endif
1620

1721
/// <summary>
1822
/// Initializes a new instance of the <see cref="Dependency" /> class.
@@ -101,7 +105,7 @@ protected virtual void OnWatcherChanged(object sender, WatcherEventArgs args)
101105
SetUtcLastModified(utcLastModified);
102106
if (BreakTieOnChanged && _watchers != null)
103107
{
104-
lock (_locker)
108+
lock (_lock)
105109
{
106110
if (_watchers != null)
107111
{

src/Cuemon.Core/Runtime/FileWatcher.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ namespace Cuemon.Runtime
1111
/// <seealso cref="Watcher" />
1212
public class FileWatcher : Watcher
1313
{
14-
private readonly object _locker = new();
14+
#if NET9_0_OR_GREATER
15+
private readonly System.Threading.Lock _lock = new();
16+
#else
17+
private readonly object _lock = new();
18+
#endif
1519

1620
/// <summary>
1721
/// Initializes a new instance of the <see cref="FileWatcher"/> class.
@@ -58,7 +62,7 @@ public FileWatcher(string path, bool readFile = false, Action<WatcherOptions> se
5862
/// </summary>
5963
protected override Task HandleSignalingAsync()
6064
{
61-
lock (_locker)
65+
lock (_lock)
6266
{
6367
var utcLastModified = File.GetLastWriteTimeUtc(Path);
6468
if (ReadFile)

src/Cuemon.Core/Runtime/Watcher.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ namespace Cuemon.Runtime
1010
/// </summary>
1111
public abstract class Watcher : Disposable, IWatcher
1212
{
13-
private readonly object _locker = new();
13+
#if NET9_0_OR_GREATER
14+
private readonly Lock _lock = new();
15+
#else
16+
private readonly object _lock = new();
17+
#endif
1418
private Timer _watcherTimer;
1519
private Timer _watcherPostponingTimer;
1620

@@ -68,7 +72,7 @@ protected Watcher(Action<WatcherOptions> setup)
6872
/// </summary>
6973
public void StartMonitoring()
7074
{
71-
lock (_locker)
75+
lock (_lock)
7276
{
7377
_watcherTimer ??= TimerFactory.CreateNonCapturingTimer(TimerInvoking, null, DueTime, Period);
7478
}
@@ -169,7 +173,7 @@ protected virtual void OnChangedRaised(WatcherEventArgs e)
169173
if (_watcherPostponingTimer != null) { return; } // we already have a postponed signaling
170174
if (DueTimeOnChanged != TimeSpan.Zero)
171175
{
172-
lock (_locker)
176+
lock (_lock)
173177
{
174178
_watcherPostponingTimer ??= TimerFactory.CreateNonCapturingTimer(PostponedHandleSignaling, e, DueTimeOnChanged, Timeout.InfiniteTimeSpan);
175179
}

src/Cuemon.Data/DatabaseWatcher.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ namespace Cuemon.Data
1414
/// <seealso cref="Watcher" />
1515
public class DatabaseWatcher : Watcher
1616
{
17-
private readonly object _locker = new();
17+
#if NET9_0_OR_GREATER
18+
private readonly System.Threading.Lock _lock = new();
19+
#else
20+
private readonly object _lock = new();
21+
#endif
1822

1923
/// <summary>
2024
/// Initializes a new instance of the <see cref="DatabaseWatcher"/> class.
@@ -54,7 +58,7 @@ public DatabaseWatcher(IDbConnection connection, Func<IDbConnection, IDataReader
5458
/// <returns>The task object representing the asynchronous operation.</returns>
5559
protected override Task HandleSignalingAsync()
5660
{
57-
lock (_locker)
61+
lock (_lock)
5862
{
5963
try
6064
{

src/Cuemon.Extensions.AspNetCore.Text.Json/Bootstrapper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using Cuemon.Extensions.AspNetCore.Text.Json.Converters;
1+
using System.Threading;
2+
using Cuemon.Extensions.AspNetCore.Text.Json.Converters;
23
using Cuemon.Extensions.Text.Json.Formatters;
34

45
namespace Cuemon.Extensions.AspNetCore.Text.Json
56
{
67
internal static class Bootstrapper
78
{
8-
private static readonly object PadLock = new();
9+
private static readonly Lock PadLock = new();
910
private static bool _initialized;
1011

1112
internal static void Initialize()

src/Cuemon.Extensions.AspNetCore.Xml/Bootstrapper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using Cuemon.Extensions.AspNetCore.Xml.Converters;
1+
using System.Threading;
2+
using Cuemon.Extensions.AspNetCore.Xml.Converters;
23
using Cuemon.Xml.Serialization.Formatters;
34

45
namespace Cuemon.Extensions.AspNetCore.Xml
56
{
67
internal static class Bootstrapper
78
{
8-
private static readonly object PadLock = new();
9+
private static readonly Lock PadLock = new();
910
private static bool _initialized;
1011

1112
internal static void Initialize()

src/Cuemon.Extensions.Net/Http/SlimHttpClientFactory.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public class SlimHttpClientFactory : IHttpClientFactory
2929
private readonly ConcurrentDictionary<string, Lazy<ActiveHandler>> _activeHandlers = new();
3030
private readonly ConcurrentQueue<ExpiredHandler> _expiredHandlers = new();
3131
private readonly Func<HttpClientHandler> _handlerFactory;
32-
private readonly object _locker = new();
32+
#if NET9_0_OR_GREATER
33+
private readonly Lock _lock = new();
34+
#else
35+
private readonly object _lock = new();
36+
#endif
3337
private readonly SlimHttpClientFactoryOptions _options;
3438
internal static readonly TimeSpan ExpirationTimerDueTime = TimeSpan.FromSeconds(15);
3539
private Timer _expirationTimer;
@@ -70,7 +74,7 @@ public HttpMessageHandler CreateHandler(string name)
7074

7175
private void StartExpirationTimer(string name)
7276
{
73-
lock (_locker)
77+
lock (_lock)
7478
{
7579
if (_expirationTimer == null)
7680
{
@@ -82,7 +86,7 @@ private void StartExpirationTimer(string name)
8286

8387
private void StopExpirationTimer()
8488
{
85-
lock (_locker)
89+
lock (_lock)
8690
{
8791
_expirationTimer.Dispose();
8892
_expirationTimer = null;
@@ -112,7 +116,7 @@ private void SetActiveHandlerToExpiredHandler(string name)
112116

113117
private void ExpiredHandlersSweep()
114118
{
115-
lock (_locker)
119+
lock (_lock)
116120
{
117121
var queueCount = _expiredHandlers.Count;
118122
Debug.WriteLine($"{nameof(ExpiredHandlersSweep)} has {queueCount} expired handlers to sweep.");

src/Cuemon.Extensions.Runtime.Caching/CacheEnumerableExtensions.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,11 @@ public static Func<T1, T2, T3, T4, T5, TResult> Memoize<TKey, T1, T2, T3, T4, T5
712712
};
713713
}
714714

715-
private static readonly object PadLock = new();
715+
#if NET9_0_OR_GREATER
716+
private readonly static System.Threading.Lock PadLock = new();
717+
#else
718+
private readonly static object PadLock = new();
719+
#endif
716720

717721
private static TResult Memoize<TKey, TTuple, TResult>(ICacheEnumerable<TKey> cache, string key, CacheInvalidation invalidation, FuncFactory<TTuple, TResult> valueFactory) where TTuple : MutableTuple
718722
{

src/Cuemon.Extensions.Text.Json/Formatters/JsonFormatterOptions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ namespace Cuemon.Extensions.Text.Json.Formatters
1616
/// </summary>
1717
public class JsonFormatterOptions : IContentNegotiation, IExceptionDescriptorOptions, IValidatableParameterObject
1818
{
19-
private readonly object _locker = new();
19+
#if NET9_0_OR_GREATER
20+
private readonly System.Threading.Lock _lock = new();
21+
#else
22+
private readonly object _lock = new();
23+
#endif
2024
private bool _refreshed;
2125

2226
/// <summary>
@@ -117,7 +121,7 @@ public JsonFormatterOptions()
117121

118122
internal JsonSerializerOptions RefreshWithConverterDependencies()
119123
{
120-
lock (_locker)
124+
lock (_lock)
121125
{
122126
if (!_refreshed)
123127
{

0 commit comments

Comments
 (0)