Skip to content

Commit 6acde78

Browse files
Encourage use of .NET 9.0's System.Threading.Lock
1 parent b4f8b01 commit 6acde78

File tree

10 files changed

+22
-18
lines changed

10 files changed

+22
-18
lines changed

build-common/NHibernate.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<VersionPatch Condition="'$(VersionPatch)' == ''">0</VersionPatch>
77
<!-- Clear VersionSuffix for making release and set it to dev for making development builds -->
88
<VersionSuffix Condition="'$(VersionSuffix)' == ''">dev</VersionSuffix>
9-
<LangVersion Condition="'$(MSBuildProjectExtension)' != '.vbproj'">12.0</LangVersion>
9+
<LangVersion Condition="'$(MSBuildProjectExtension)' != '.vbproj'">preview</LangVersion>
1010

1111
<VersionPrefix Condition="'$(VersionPrefix)' == ''">$(NhVersion).$(VersionPatch)</VersionPrefix>
1212
<VersionSuffix Condition="'$(VersionSuffix)' != '' AND '$(BuildNumber)' != ''">$(VersionSuffix).$(BuildNumber)</VersionSuffix>

src/NHibernate.Test/NHSpecificTest/NH2030/Fixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Text;
@@ -15,7 +15,7 @@ public class Fixture
1515
[Test]
1616
public void GetTypeWithLenShouldBeThreadSafe()
1717
{
18-
object sync = new object();
18+
Lock sync = new Lock();
1919
List<Exception> exceptions = new List<Exception>();
2020

2121
ManualResetEvent startEvent = new ManualResetEvent(false);

src/NHibernate.Test/NHSpecificTest/NH2192/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected override void OnTearDown()
4545
[Test]
4646
public void HqlIsThreadsafe_UsingThreads()
4747
{
48-
object sync = new object();
48+
Lock sync = new Lock();
4949
List<int> results = new List<int>();
5050
List<Exception> exceptions = new List<Exception>();
5151

src/NHibernate/Cache/SyncCacheLock.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,23 @@ class SyncCacheLock : ICacheLock
1010

1111
class MonitorLock : IDisposable
1212
{
13-
private readonly object _lockObj;
14-
15-
public MonitorLock(object lockObj)
16-
{
17-
_lockObj = lockObj;
18-
}
13+
private readonly Lock _lockObj = new Lock();
1914

2015
public IDisposable Lock()
2116
{
22-
Monitor.Enter(_lockObj);
17+
_lockObj.Enter();
2318
return this;
2419
}
2520

2621
public void Dispose()
2722
{
28-
Monitor.Exit(_lockObj);
23+
_lockObj.Exit();
2924
}
3025
}
3126

3227
public SyncCacheLock()
3328
{
34-
_monitorLock = new MonitorLock(this);
29+
_monitorLock = new();
3530
}
3631

3732
public void Dispose()

src/NHibernate/Cache/Timestamper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23

34
namespace NHibernate.Cache
45
{
@@ -11,7 +12,7 @@ namespace NHibernate.Cache
1112
/// </remarks>
1213
public static class Timestamper
1314
{
14-
private static object lockObject = new object();
15+
private static Lock lockObject = new Lock();
1516

1617
// hibernate is using System.currentMilliSeconds which is calculated
1718
// from jan 1, 1970

src/NHibernate/Context/MapBasedSessionContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.Concurrent;
3+
using System.Threading;
34
using NHibernate.Engine;
45

56
namespace NHibernate.Context
@@ -9,7 +10,7 @@ public abstract class MapBasedSessionContext : CurrentSessionContext
910
private readonly ISessionFactoryImplementor _factory;
1011

1112
// Must be static, different instances of MapBasedSessionContext may have to yield the same map.
12-
private static readonly object _locker = new object();
13+
private static readonly Lock _locker = new Lock();
1314

1415
protected MapBasedSessionContext(ISessionFactoryImplementor factory)
1516
{

src/NHibernate/NHibernate.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
7474
</ItemGroup>
7575

76+
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">
77+
<PackageReference Include="Backport.System.Threading.Lock" Version="1.1.6" />
78+
</ItemGroup>
79+
7680
<ItemGroup>
7781
<Content Include="*.xsd">
7882
<PackagePath>./</PackagePath>

src/NHibernate/Stat/StatisticsImpl.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
using System.Text;
44
using NHibernate.Engine;
55
using System.Linq;
6+
using System.Threading;
67

78
namespace NHibernate.Stat
89
{
910
public class StatisticsImpl : IStatistics, IStatisticsImplementor
1011
{
11-
private readonly object _syncRoot = new object();
12+
private readonly Lock _syncRoot = new Lock();
1213

1314
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(StatisticsImpl));
1415
private readonly ISessionFactoryImplementor sessionFactory;

src/NHibernate/Util/SimpleMRUCache.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.Serialization;
3+
using System.Threading;
34

45
namespace NHibernate.Util
56
{
@@ -17,7 +18,7 @@ public class SimpleMRUCache : IDeserializationCallback
1718
{
1819
private const int DefaultStrongRefCount = 128;
1920

20-
private readonly object _syncRoot = new object();
21+
private readonly Lock _syncRoot = new Lock();
2122

2223
private readonly int strongReferenceCount;
2324

src/NHibernate/Util/SoftLimitMRUCache.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Runtime.Serialization;
4+
using System.Threading;
45

56
namespace NHibernate.Util
67
{
@@ -23,7 +24,7 @@ namespace NHibernate.Util
2324
public class SoftLimitMRUCache : IDeserializationCallback
2425
{
2526
private const int DefaultStrongRefCount = 128;
26-
private readonly object _syncRoot = new object();
27+
private readonly Lock _syncRoot = new Lock();
2728

2829
private readonly int strongReferenceCount;
2930

0 commit comments

Comments
 (0)