-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System.Threading; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace PolyShim.Tests.Net90; | ||
|
||
public class LockTests | ||
{ | ||
[Fact] | ||
public void Enter_Test() | ||
{ | ||
// Arrange | ||
var syncRoot = new Lock(); | ||
|
||
// Act & assert | ||
syncRoot.IsHeldByCurrentThread.Should().BeFalse(); | ||
syncRoot.Enter(); | ||
syncRoot.IsHeldByCurrentThread.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void TryEnter_Test() | ||
{ | ||
// Arrange | ||
var syncRoot = new Lock(); | ||
|
||
// Act & assert | ||
syncRoot.IsHeldByCurrentThread.Should().BeFalse(); | ||
syncRoot.TryEnter().Should().BeTrue(); | ||
syncRoot.IsHeldByCurrentThread.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void TryEnter_Timeout_Test() | ||
{ | ||
// Arrange | ||
var syncRoot = new Lock(); | ||
|
||
// Act & assert | ||
syncRoot.IsHeldByCurrentThread.Should().BeFalse(); | ||
syncRoot.TryEnter(100).Should().BeTrue(); | ||
syncRoot.IsHeldByCurrentThread.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void Exit_Test() | ||
{ | ||
// Arrange | ||
var syncRoot = new Lock(); | ||
|
||
// Act & assert | ||
syncRoot.IsHeldByCurrentThread.Should().BeFalse(); | ||
syncRoot.Enter(); | ||
syncRoot.IsHeldByCurrentThread.Should().BeTrue(); | ||
syncRoot.Exit(); | ||
syncRoot.IsHeldByCurrentThread.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void EnterScope_Test() | ||
{ | ||
// Arrange | ||
var syncRoot = new Lock(); | ||
|
||
// Act & assert | ||
syncRoot.IsHeldByCurrentThread.Should().BeFalse(); | ||
using (syncRoot.EnterScope()) | ||
{ | ||
syncRoot.IsHeldByCurrentThread.Should().BeTrue(); | ||
} | ||
syncRoot.IsHeldByCurrentThread.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void EnterScope_Layered_Test() | ||
{ | ||
// Arrange | ||
var syncRoot = new Lock(); | ||
|
||
// Act & assert | ||
syncRoot.IsHeldByCurrentThread.Should().BeFalse(); | ||
using (syncRoot.EnterScope()) | ||
{ | ||
syncRoot.IsHeldByCurrentThread.Should().BeTrue(); | ||
using (syncRoot.EnterScope()) | ||
{ | ||
syncRoot.IsHeldByCurrentThread.Should().BeTrue(); | ||
} | ||
syncRoot.IsHeldByCurrentThread.Should().BeTrue(); | ||
} | ||
syncRoot.IsHeldByCurrentThread.Should().BeFalse(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#if (NETCOREAPP && !NET9_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
#nullable enable | ||
#pragma warning disable CS9216 | ||
// ReSharper disable RedundantUsingDirective | ||
// ReSharper disable CheckNamespace | ||
// ReSharper disable InconsistentNaming | ||
// ReSharper disable PartialTypeWithSinglePart | ||
|
||
namespace System.Threading; | ||
|
||
internal partial class Lock | ||
{ | ||
#if (NETCOREAPP) || (NETFRAMEWORK && NET45_OR_GREATER) || (NETSTANDARD) | ||
public bool IsHeldByCurrentThread => Monitor.IsEntered(this); | ||
#endif | ||
|
||
public void Enter() => Monitor.Enter(this); | ||
|
||
public bool TryEnter() => Monitor.TryEnter(this); | ||
|
||
public bool TryEnter(TimeSpan timeout) => Monitor.TryEnter(this, timeout); | ||
|
||
public bool TryEnter(int millisecondsTimeout) => | ||
TryEnter(TimeSpan.FromMilliseconds(millisecondsTimeout)); | ||
|
||
public void Exit() => Monitor.Exit(this); | ||
|
||
public Scope EnterScope() | ||
{ | ||
Enter(); | ||
return new Scope(this); | ||
} | ||
} | ||
|
||
internal partial class Lock | ||
{ | ||
public readonly ref struct Scope(Lock owner) | ||
{ | ||
public void Dispose() => owner.Exit(); | ||
} | ||
} | ||
#endif |
This lock class is not hardened against thread aborts coming from framework versions preceding .NET 5.0.
Consider using Backport.System.Threading.Lock instead of this method for locking.