Skip to content

Commit 1ee61b8

Browse files
authored
Remove RequiresPreviewFeatures attribute on Lock, add test using the lock keyword (#102222)
- Also removed the `EnablePreviewFeatures` project properties that were added with `Lock` changes - Added some basic tests with `Lock` using the `lock` keyword
1 parent 44d919d commit 1ee61b8

File tree

9 files changed

+39
-14
lines changed

9 files changed

+39
-14
lines changed

src/coreclr/nativeaot/System.Private.DisabledReflection/src/System.Private.DisabledReflection.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
<PropertyGroup>
33
<!-- we access a lot of internals of System.Private.CoreLib, disable compiling against the ref assembly for now so we don't need to update it -->
44
<CompileUsingReferenceAssemblies>false</CompileUsingReferenceAssemblies>
5-
<!-- CA2252: Opt in to preview features before using them (Lock) -->
6-
<EnablePreviewFeatures>true</EnablePreviewFeatures>
75
</PropertyGroup>
86
<ItemGroup>
97
<ProjectReference Include="..\..\System.Private.CoreLib\src\System.Private.CoreLib.csproj" />

src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<NativeFormatCommonPath>$(CompilerCommonPath)\Internal\NativeFormat</NativeFormatCommonPath>
99
<!-- we access a lot of internals of System.Private.CoreLib, disable compiling against the ref assembly for now so we don't need to update it -->
1010
<CompileUsingReferenceAssemblies>false</CompileUsingReferenceAssemblies>
11-
<!-- CA2252: Opt in to preview features before using them (Lock) -->
12-
<EnablePreviewFeatures>true</EnablePreviewFeatures>
1311
</PropertyGroup>
1412
<ItemGroup>
1513
<Compile Include="$(NativeFormatCommonPath)\NativeFormat.cs" />

src/coreclr/nativeaot/System.Private.StackTraceMetadata/src/System.Private.StackTraceMetadata.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
<PropertyGroup>
33
<!-- we access a lot of internals of System.Private.CoreLib, disable compiling against the ref assembly for now so we don't need to update it -->
44
<CompileUsingReferenceAssemblies>false</CompileUsingReferenceAssemblies>
5-
<!-- CA2252: Opt in to preview features before using them (Lock) -->
6-
<EnablePreviewFeatures>true</EnablePreviewFeatures>
75
</PropertyGroup>
86

97
<ItemGroup>

src/coreclr/nativeaot/System.Private.TypeLoader/src/System.Private.TypeLoader.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<DefineConstants Condition="'$(GENERICS_FORCE_USG)' != ''">GENERICS_FORCE_USG;$(DefineConstants)</DefineConstants>
99
<!-- we access a lot of internals of System.Private.CoreLib, disable compiling against the ref assembly for now so we don't need to update it -->
1010
<CompileUsingReferenceAssemblies>false</CompileUsingReferenceAssemblies>
11-
<!-- CA2252: Opt in to preview features before using them (Lock) -->
12-
<EnablePreviewFeatures>true</EnablePreviewFeatures>
1311
</PropertyGroup>
1412
<ItemGroup>
1513
<ProjectReference Include="..\..\System.Private.CoreLib\src\System.Private.CoreLib.csproj" />

src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
<SharedGUID>c5ed3c1d-b572-46f1-8f96-522a85ce1179</SharedGUID>
55
<StringResourcesName Condition="'$(StringResourcesName)' == ''">System.Private.CoreLib.Strings</StringResourcesName>
66
<GenerateResourcesSubstitutions>true</GenerateResourcesSubstitutions>
7-
<!-- CA2252: Opt in to preview features before using them (for System.Threading.Lock) -->
8-
<EnablePreviewFeatures>true</EnablePreviewFeatures>
97
</PropertyGroup>
108
<PropertyGroup Label="Configuration">
119
<Import_RootNamespace />

src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace System.Threading
1616
/// that holds a lock may enter the lock repeatedly without exiting it, such as recursively, in which case the thread should
1717
/// eventually exit the lock the same number of times to fully exit the lock and allow other threads to enter the lock.
1818
/// </remarks>
19-
[Runtime.Versioning.RequiresPreviewFeatures]
2019
public sealed partial class Lock
2120
{
2221
private const short DefaultMaxSpinCount = 22;

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15164,7 +15164,6 @@ public enum LazyThreadSafetyMode
1516415164
PublicationOnly = 1,
1516515165
ExecutionAndPublication = 2,
1516615166
}
15167-
[System.Runtime.Versioning.RequiresPreviewFeaturesAttribute]
1516815167
public sealed partial class Lock
1516915168
{
1517015169
public Lock() { }

src/libraries/System.Threading/tests/LockTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,37 @@ public static class LockTests
1111
{
1212
private const int FailTimeoutMilliseconds = 30000;
1313

14+
#pragma warning disable CS9216 // casting Lock to object
15+
[Fact]
16+
public static void LockStatementWithLockVsMonitor()
17+
{
18+
Lock lockObj = new();
19+
lock (lockObj)
20+
{
21+
Assert.True(lockObj.IsHeldByCurrentThread);
22+
Assert.False(Monitor.IsEntered(lockObj));
23+
}
24+
25+
lock ((object)lockObj)
26+
{
27+
Assert.False(lockObj.IsHeldByCurrentThread);
28+
Assert.True(Monitor.IsEntered(lockObj));
29+
}
30+
31+
LockOnTWhereTIsLock(lockObj);
32+
33+
static void LockOnTWhereTIsLock<T>(T lockObj) where T : class
34+
{
35+
Assert.IsType<Lock>(lockObj);
36+
lock (lockObj)
37+
{
38+
Assert.False(((Lock)(object)lockObj).IsHeldByCurrentThread);
39+
Assert.True(Monitor.IsEntered(lockObj));
40+
}
41+
}
42+
}
43+
#pragma warning restore CS9216
44+
1445
// Attempts a single recursive acquisition/release cycle of a newly-created lock.
1546
[Fact]
1647
public static void BasicRecursion()
@@ -27,6 +58,10 @@ public static void BasicRecursion()
2758
{
2859
Assert.True(lockObj.IsHeldByCurrentThread);
2960
}
61+
lock (lockObj)
62+
{
63+
Assert.True(lockObj.IsHeldByCurrentThread);
64+
}
3065
Assert.True(lockObj.IsHeldByCurrentThread);
3166
lockObj.Exit();
3267
Assert.False(lockObj.IsHeldByCurrentThread);
@@ -64,6 +99,10 @@ public static void IsHeldByCurrentThread()
6499
{
65100
Assert.True(lockObj.IsHeldByCurrentThread);
66101
}
102+
lock (lockObj)
103+
{
104+
Assert.True(lockObj.IsHeldByCurrentThread);
105+
}
67106
Assert.False(lockObj.IsHeldByCurrentThread);
68107
}
69108

src/libraries/System.Threading/tests/System.Threading.Tests.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
55
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
66
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7-
<!-- CA2252: Opt in to preview features before using them (Lock) -->
8-
<EnablePreviewFeatures>true</EnablePreviewFeatures>
97
</PropertyGroup>
108
<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
119
<XunitShowProgress>true</XunitShowProgress>

0 commit comments

Comments
 (0)