From 67a845b480eb31a6140359f76d964b1943cc7d40 Mon Sep 17 00:00:00 2001 From: Koundinya Veluri Date: Tue, 14 May 2024 11:15:43 -0700 Subject: [PATCH] Remove `RequiresPreviewFeatures` attribute on `Lock`, add test using the `lock` keyword - Also removed the `EnablePreviewFeatures` project properties that were added with `Lock` changes - Added some basic tests with `Lock` using the `lock` keyword --- .../System.Private.DisabledReflection.csproj | 2 - ...System.Private.Reflection.Execution.csproj | 2 - .../System.Private.StackTraceMetadata.csproj | 2 - .../src/System.Private.TypeLoader.csproj | 2 - .../System.Private.CoreLib.Shared.projitems | 2 - .../src/System/Threading/Lock.cs | 1 - .../System.Runtime/ref/System.Runtime.cs | 1 - .../System.Threading/tests/LockTests.cs | 39 +++++++++++++++++++ .../tests/System.Threading.Tests.csproj | 2 - 9 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/coreclr/nativeaot/System.Private.DisabledReflection/src/System.Private.DisabledReflection.csproj b/src/coreclr/nativeaot/System.Private.DisabledReflection/src/System.Private.DisabledReflection.csproj index dda9aca01512d7..365bf3356186c3 100644 --- a/src/coreclr/nativeaot/System.Private.DisabledReflection/src/System.Private.DisabledReflection.csproj +++ b/src/coreclr/nativeaot/System.Private.DisabledReflection/src/System.Private.DisabledReflection.csproj @@ -2,8 +2,6 @@ false - - true diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj index ee7ed1e9c86a7d..86fb64fa51852c 100644 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj +++ b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj @@ -8,8 +8,6 @@ $(CompilerCommonPath)\Internal\NativeFormat false - - true diff --git a/src/coreclr/nativeaot/System.Private.StackTraceMetadata/src/System.Private.StackTraceMetadata.csproj b/src/coreclr/nativeaot/System.Private.StackTraceMetadata/src/System.Private.StackTraceMetadata.csproj index 620a94d91c40ab..cc44031e9ac552 100644 --- a/src/coreclr/nativeaot/System.Private.StackTraceMetadata/src/System.Private.StackTraceMetadata.csproj +++ b/src/coreclr/nativeaot/System.Private.StackTraceMetadata/src/System.Private.StackTraceMetadata.csproj @@ -2,8 +2,6 @@ false - - true diff --git a/src/coreclr/nativeaot/System.Private.TypeLoader/src/System.Private.TypeLoader.csproj b/src/coreclr/nativeaot/System.Private.TypeLoader/src/System.Private.TypeLoader.csproj index 14eda37aa48e72..ee1285a4b9a0e8 100644 --- a/src/coreclr/nativeaot/System.Private.TypeLoader/src/System.Private.TypeLoader.csproj +++ b/src/coreclr/nativeaot/System.Private.TypeLoader/src/System.Private.TypeLoader.csproj @@ -8,8 +8,6 @@ GENERICS_FORCE_USG;$(DefineConstants) false - - true diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index d2a72950212df8..3edeed4661257f 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -4,8 +4,6 @@ c5ed3c1d-b572-46f1-8f96-522a85ce1179 System.Private.CoreLib.Strings true - - true diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs index 4572c9c310ecef..00b879b04c4894 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs @@ -16,7 +16,6 @@ namespace System.Threading /// that holds a lock may enter the lock repeatedly without exiting it, such as recursively, in which case the thread should /// eventually exit the lock the same number of times to fully exit the lock and allow other threads to enter the lock. /// - [Runtime.Versioning.RequiresPreviewFeatures] public sealed partial class Lock { private const short DefaultMaxSpinCount = 22; diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index a3f2a4b07e2f48..a701b670efb9a3 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -15158,7 +15158,6 @@ public enum LazyThreadSafetyMode PublicationOnly = 1, ExecutionAndPublication = 2, } - [System.Runtime.Versioning.RequiresPreviewFeaturesAttribute] public sealed partial class Lock { public Lock() { } diff --git a/src/libraries/System.Threading/tests/LockTests.cs b/src/libraries/System.Threading/tests/LockTests.cs index b4eab8787c8cac..e180131883780e 100644 --- a/src/libraries/System.Threading/tests/LockTests.cs +++ b/src/libraries/System.Threading/tests/LockTests.cs @@ -11,6 +11,37 @@ public static class LockTests { private const int FailTimeoutMilliseconds = 30000; +#pragma warning disable CS9216 // casting Lock to object + [Fact] + public static void LockStatementWithLockVsMonitor() + { + Lock lockObj = new(); + lock (lockObj) + { + Assert.True(lockObj.IsHeldByCurrentThread); + Assert.False(Monitor.IsEntered(lockObj)); + } + + lock ((object)lockObj) + { + Assert.False(lockObj.IsHeldByCurrentThread); + Assert.True(Monitor.IsEntered(lockObj)); + } + + LockOnTWhereTIsLock(lockObj); + + static void LockOnTWhereTIsLock(T lockObj) where T : class + { + Assert.IsType(lockObj); + lock (lockObj) + { + Assert.False(((Lock)(object)lockObj).IsHeldByCurrentThread); + Assert.True(Monitor.IsEntered(lockObj)); + } + } + } +#pragma warning restore CS9216 + // Attempts a single recursive acquisition/release cycle of a newly-created lock. [Fact] public static void BasicRecursion() @@ -27,6 +58,10 @@ public static void BasicRecursion() { Assert.True(lockObj.IsHeldByCurrentThread); } + lock (lockObj) + { + Assert.True(lockObj.IsHeldByCurrentThread); + } Assert.True(lockObj.IsHeldByCurrentThread); lockObj.Exit(); Assert.False(lockObj.IsHeldByCurrentThread); @@ -64,6 +99,10 @@ public static void IsHeldByCurrentThread() { Assert.True(lockObj.IsHeldByCurrentThread); } + lock (lockObj) + { + Assert.True(lockObj.IsHeldByCurrentThread); + } Assert.False(lockObj.IsHeldByCurrentThread); } diff --git a/src/libraries/System.Threading/tests/System.Threading.Tests.csproj b/src/libraries/System.Threading/tests/System.Threading.Tests.csproj index 768bb7b665d925..c552756fd17e3d 100644 --- a/src/libraries/System.Threading/tests/System.Threading.Tests.csproj +++ b/src/libraries/System.Threading/tests/System.Threading.Tests.csproj @@ -4,8 +4,6 @@ true $(NetCoreAppCurrent) true - - true true