Skip to content

Commit

Permalink
Remove RequiresPreviewFeatures attribute on Lock, add test using …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
kouvel authored May 15, 2024
1 parent 44d919d commit 1ee61b8
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<PropertyGroup>
<!-- 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 -->
<CompileUsingReferenceAssemblies>false</CompileUsingReferenceAssemblies>
<!-- CA2252: Opt in to preview features before using them (Lock) -->
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.Private.CoreLib\src\System.Private.CoreLib.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
<NativeFormatCommonPath>$(CompilerCommonPath)\Internal\NativeFormat</NativeFormatCommonPath>
<!-- 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 -->
<CompileUsingReferenceAssemblies>false</CompileUsingReferenceAssemblies>
<!-- CA2252: Opt in to preview features before using them (Lock) -->
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(NativeFormatCommonPath)\NativeFormat.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<PropertyGroup>
<!-- 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 -->
<CompileUsingReferenceAssemblies>false</CompileUsingReferenceAssemblies>
<!-- CA2252: Opt in to preview features before using them (Lock) -->
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
<DefineConstants Condition="'$(GENERICS_FORCE_USG)' != ''">GENERICS_FORCE_USG;$(DefineConstants)</DefineConstants>
<!-- 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 -->
<CompileUsingReferenceAssemblies>false</CompileUsingReferenceAssemblies>
<!-- CA2252: Opt in to preview features before using them (Lock) -->
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.Private.CoreLib\src\System.Private.CoreLib.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<SharedGUID>c5ed3c1d-b572-46f1-8f96-522a85ce1179</SharedGUID>
<StringResourcesName Condition="'$(StringResourcesName)' == ''">System.Private.CoreLib.Strings</StringResourcesName>
<GenerateResourcesSubstitutions>true</GenerateResourcesSubstitutions>
<!-- CA2252: Opt in to preview features before using them (for System.Threading.Lock) -->
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
<PropertyGroup Label="Configuration">
<Import_RootNamespace />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </remarks>
[Runtime.Versioning.RequiresPreviewFeatures]
public sealed partial class Lock
{
private const short DefaultMaxSpinCount = 22;
Expand Down
1 change: 0 additions & 1 deletion src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15164,7 +15164,6 @@ public enum LazyThreadSafetyMode
PublicationOnly = 1,
ExecutionAndPublication = 2,
}
[System.Runtime.Versioning.RequiresPreviewFeaturesAttribute]
public sealed partial class Lock
{
public Lock() { }
Expand Down
39 changes: 39 additions & 0 deletions src/libraries/System.Threading/tests/LockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(T lockObj) where T : class
{
Assert.IsType<Lock>(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()
Expand All @@ -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);
Expand Down Expand Up @@ -64,6 +99,10 @@ public static void IsHeldByCurrentThread()
{
Assert.True(lockObj.IsHeldByCurrentThread);
}
lock (lockObj)
{
Assert.True(lockObj.IsHeldByCurrentThread);
}
Assert.False(lockObj.IsHeldByCurrentThread);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- CA2252: Opt in to preview features before using them (Lock) -->
<EnablePreviewFeatures>true</EnablePreviewFeatures>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetOS)' == 'browser'">
<XunitShowProgress>true</XunitShowProgress>
Expand Down

0 comments on commit 1ee61b8

Please sign in to comment.