Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encourage use of .NET 9.0's System.Threading.Lock #3601

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build-common/NHibernate.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<VersionPatch Condition="'$(VersionPatch)' == ''">0</VersionPatch>
<!-- Clear VersionSuffix for making release and set it to dev for making development builds -->
<VersionSuffix Condition="'$(VersionSuffix)' == ''">dev</VersionSuffix>
<LangVersion Condition="'$(MSBuildProjectExtension)' != '.vbproj'">12.0</LangVersion>
<LangVersion Condition="'$(MSBuildProjectExtension)' != '.vbproj'">13.0</LangVersion>

<VersionPrefix Condition="'$(VersionPrefix)' == ''">$(NhVersion).$(VersionPatch)</VersionPrefix>
<VersionSuffix Condition="'$(VersionSuffix)' != '' AND '$(BuildNumber)' != ''">$(VersionSuffix).$(BuildNumber)</VersionSuffix>
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/NH2030/Fixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
Expand Down
21 changes: 8 additions & 13 deletions src/NHibernate/Cache/SyncCacheLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,27 @@ namespace NHibernate.Cache
{
class SyncCacheLock : ICacheLock
{
private readonly MonitorLock _monitorLock;
private readonly InternalLock _internalLock;

class MonitorLock : IDisposable
class InternalLock : IDisposable
{
private readonly object _lockObj;

public MonitorLock(object lockObj)
{
_lockObj = lockObj;
}
private readonly Lock _lockObj = new Lock();

public IDisposable Lock()
{
Monitor.Enter(_lockObj);
_lockObj.Enter();
return this;
}

public void Dispose()
{
Monitor.Exit(_lockObj);
_lockObj.Exit();
}
}

public SyncCacheLock()
{
_monitorLock = new MonitorLock(this);
_internalLock = new();
}

public void Dispose()
Expand All @@ -40,12 +35,12 @@ public void Dispose()

public IDisposable ReadLock()
{
return _monitorLock.Lock();
return _internalLock.Lock();
}

public IDisposable WriteLock()
{
return _monitorLock.Lock();
return _internalLock.Lock();
}

public Task<IDisposable> ReadLockAsync()
Expand Down
3 changes: 2 additions & 1 deletion src/NHibernate/Cache/Timestamper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;

namespace NHibernate.Cache
{
Expand All @@ -11,7 +12,7 @@ namespace NHibernate.Cache
/// </remarks>
public static class Timestamper
{
private static object lockObject = new object();
private static Lock lockObject = LockFactory.Create();

// hibernate is using System.currentMilliSeconds which is calculated
// from jan 1, 1970
Expand Down
3 changes: 2 additions & 1 deletion src/NHibernate/Context/MapBasedSessionContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Threading;
using NHibernate.Engine;

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

// Must be static, different instances of MapBasedSessionContext may have to yield the same map.
private static readonly object _locker = new object();
private static readonly Lock _locker = LockFactory.Create();

protected MapBasedSessionContext(ISessionFactoryImplementor factory)
{
Expand Down
10 changes: 10 additions & 0 deletions src/NHibernate/NHibernate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Backport.System.Threading.Lock" Version="3.1.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>analyzers</IncludeAssets>
</PackageReference>
<Using Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="System.Threading.Lock" />
<Using Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" Alias="Lock" Include="Backport.System.Threading.Lock" />
<Using Alias="LockFactory" Include="Backport.System.Threading.LockFactory" />
</ItemGroup>

<ItemGroup>
<Content Include="*.xsd">
<PackagePath>./</PackagePath>
Expand Down
3 changes: 2 additions & 1 deletion src/NHibernate/Stat/StatisticsImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
using System.Text;
using NHibernate.Engine;
using System.Linq;
using System.Threading;

namespace NHibernate.Stat
{
public class StatisticsImpl : IStatistics, IStatisticsImplementor
{
private readonly object _syncRoot = new object();
private readonly Lock _syncRoot = LockFactory.Create();

private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(StatisticsImpl));
private readonly ISessionFactoryImplementor sessionFactory;
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Util/AsyncReaderWriterLock.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Threading;
using System.Threading.Tasks;

Expand Down
3 changes: 2 additions & 1 deletion src/NHibernate/Util/SimpleMRUCache.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.Serialization;
using System.Threading;

namespace NHibernate.Util
{
Expand All @@ -17,7 +18,7 @@ public class SimpleMRUCache : IDeserializationCallback
{
private const int DefaultStrongRefCount = 128;

private readonly object _syncRoot = new object();
private readonly Lock _syncRoot = LockFactory.Create();

private readonly int strongReferenceCount;

Expand Down
3 changes: 2 additions & 1 deletion src/NHibernate/Util/SoftLimitMRUCache.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Runtime.Serialization;
using System.Threading;

namespace NHibernate.Util
{
Expand All @@ -23,7 +24,7 @@ namespace NHibernate.Util
public class SoftLimitMRUCache : IDeserializationCallback
{
private const int DefaultStrongRefCount = 128;
private readonly object _syncRoot = new object();
private readonly Lock _syncRoot = LockFactory.Create();

private readonly int strongReferenceCount;

Expand Down
Loading