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 1 commit
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'">preview</LangVersion>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, it needs language version 14.0? We will likely wait for it before merging your proposal, if we accept it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not spotted what does require such a change. Why this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 13.0 as such, and comes with .NET 9.0, but for now you need to set the LangVersion to preview on .NET 9.0 in order to lock on a System.Threading.Lock object. The expectation is that this changes as soon as .NET 9.0 is released.


<VersionPrefix Condition="'$(VersionPrefix)' == ''">$(NhVersion).$(VersionPatch)</VersionPrefix>
<VersionSuffix Condition="'$(VersionSuffix)' != '' AND '$(BuildNumber)' != ''">$(VersionSuffix).$(BuildNumber)</VersionSuffix>
Expand Down
4 changes: 2 additions & 2 deletions 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 All @@ -15,7 +15,7 @@ public class Fixture
[Test]
public void GetTypeWithLenShouldBeThreadSafe()
{
object sync = new object();
Lock sync = new Lock();
MarkCiliaVincenti marked this conversation as resolved.
Show resolved Hide resolved
List<Exception> exceptions = new List<Exception>();

ManualResetEvent startEvent = new ManualResetEvent(false);
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/NH2192/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override void OnTearDown()
[Test]
public void HqlIsThreadsafe_UsingThreads()
{
object sync = new object();
Lock sync = new Lock();
MarkCiliaVincenti marked this conversation as resolved.
Show resolved Hide resolved
List<int> results = new List<int>();
List<Exception> exceptions = new List<Exception>();

Expand Down
13 changes: 4 additions & 9 deletions src/NHibernate/Cache/SyncCacheLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,23 @@ class SyncCacheLock : ICacheLock

class MonitorLock : IDisposable
MarkCiliaVincenti marked this conversation as resolved.
Show resolved Hide resolved
{
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);
_monitorLock = new();
}

public void Dispose()
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 = new Lock();

// 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 = new Lock();

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

<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))">
<PackageReference Include="Backport.System.Threading.Lock" Version="1.1.6" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, that is this Nuget package, coming from your repository.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is correct.

</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 = new Lock();

private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(StatisticsImpl));
private readonly ISessionFactoryImplementor sessionFactory;
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 = new Lock();

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 = new Lock();

private readonly int strongReferenceCount;

Expand Down