|
| 1 | +// Credit: Mark Cilia Vincenti, 2024 |
| 2 | +// Taken from: https://github.com/MarkCiliaVincenti/Backport.System.Threading.Lock |
| 3 | +// NuGet package: https://www.nuget.org/packages/Backport.System.Threading.Lock |
| 4 | + |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +#if NET9_0_OR_GREATER |
| 7 | +[assembly: TypeForwardedTo(typeof(System.Threading.Lock))] |
| 8 | +#else |
| 9 | +namespace System.Threading; |
| 10 | +/// <summary> |
| 11 | +/// A backport of .NET 9.0+'s System.Threading.Lock. |
| 12 | +/// </summary> |
| 13 | +public sealed class Lock |
| 14 | +{ |
| 15 | +#pragma warning disable CS9216 // A value of type 'System.Threading.Lock' converted to a different type will use likely unintended monitor-based locking in 'lock' statement. |
| 16 | + /// <summary> |
| 17 | + /// <inheritdoc cref="Monitor.Enter(object)"/> |
| 18 | + /// </summary> |
| 19 | + /// <exception cref="ArgumentNullException"/> |
| 20 | +#if !PARTIAL_SUPPORT |
| 21 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 22 | +#endif |
| 23 | + public void Enter() => Monitor.Enter(this); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// <inheritdoc cref="Monitor.TryEnter(object)"/> |
| 27 | + /// </summary> |
| 28 | + /// <returns> |
| 29 | + /// <inheritdoc cref="Monitor.TryEnter(object)"/> |
| 30 | + /// </returns> |
| 31 | + /// <exception cref="ArgumentNullException"/> |
| 32 | +#if !PARTIAL_SUPPORT |
| 33 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 34 | +#endif |
| 35 | + public bool TryEnter() => Monitor.TryEnter(this); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// <inheritdoc cref="Monitor.TryEnter(object, TimeSpan)"/> |
| 39 | + /// </summary> |
| 40 | + /// <returns> |
| 41 | + /// <inheritdoc cref="Monitor.TryEnter(object, TimeSpan)"/> |
| 42 | + /// </returns> |
| 43 | + /// <exception cref="ArgumentNullException"/> |
| 44 | + /// <exception cref="ArgumentOutOfRangeException"/> |
| 45 | +#if !PARTIAL_SUPPORT |
| 46 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 47 | +#endif |
| 48 | + public bool TryEnter(TimeSpan timeout) => Monitor.TryEnter(this, timeout); |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// <inheritdoc cref="Monitor.TryEnter(object, int)"/> |
| 52 | + /// </summary> |
| 53 | + /// <returns> |
| 54 | + /// <inheritdoc cref="Monitor.TryEnter(object, int)"/> |
| 55 | + /// </returns> |
| 56 | + /// <exception cref="ArgumentNullException"/> |
| 57 | + /// <exception cref="ArgumentOutOfRangeException"/> |
| 58 | +#if !PARTIAL_SUPPORT |
| 59 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 60 | +#endif |
| 61 | + public bool TryEnter(int millisecondsTimeout) => Monitor.TryEnter(this, millisecondsTimeout); |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// <inheritdoc cref="Monitor.Exit(object)"/> |
| 65 | + /// </summary> |
| 66 | + /// <exception cref="ArgumentNullException"/> |
| 67 | + /// <exception cref="SynchronizationLockException"/> |
| 68 | +#if !PARTIAL_SUPPORT |
| 69 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 70 | +#endif |
| 71 | + public void Exit() => Monitor.Exit(this); |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Determines whether the current thread holds this lock. |
| 75 | + /// </summary> |
| 76 | + /// <returns> |
| 77 | + /// true if the current thread holds this lock; otherwise, false. |
| 78 | + /// </returns> |
| 79 | + /// <exception cref="ArgumentNullException"/> |
| 80 | +#if !PARTIAL_SUPPORT |
| 81 | + public bool IsHeldByCurrentThread => Monitor.IsEntered(this); |
| 82 | +#else |
| 83 | + public bool IsHeldByCurrentThread => throw new NotSupportedException("IsHeldByCurrentThread is only supported on .NET Framework 4.5 or greater."); |
| 84 | +#endif |
| 85 | +#pragma warning restore CS9216 // A value of type 'System.Threading.Lock' converted to a different type will use likely unintended monitor-based locking in 'lock' statement. |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Enters the lock and returns a <see cref="Scope"/> that may be disposed to exit the lock. Once the method returns, |
| 89 | + /// the calling thread would be the only thread that holds the lock. This method is intended to be used along with a |
| 90 | + /// language construct that would automatically dispose the <see cref="Scope"/>, such as with the C# <code>using</code> |
| 91 | + /// statement. |
| 92 | + /// </summary> |
| 93 | + /// <returns> |
| 94 | + /// A <see cref="Scope"/> that may be disposed to exit the lock. |
| 95 | + /// </returns> |
| 96 | + /// <remarks> |
| 97 | + /// If the lock cannot be entered immediately, the calling thread waits for the lock to be exited. If the lock is |
| 98 | + /// already held by the calling thread, the lock is entered again. The calling thread should exit the lock, such as by |
| 99 | + /// disposing the returned <see cref="Scope"/>, as many times as it had entered the lock to fully exit the lock and |
| 100 | + /// allow other threads to enter the lock. |
| 101 | + /// </remarks> |
| 102 | +#if !PARTIAL_SUPPORT |
| 103 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 104 | +#endif |
| 105 | + public Scope EnterScope() |
| 106 | + { |
| 107 | + Enter(); |
| 108 | + return new Scope(this); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// A disposable structure that is returned by <see cref="EnterScope()"/>, which when disposed, exits the lock. |
| 113 | + /// </summary> |
| 114 | + public ref struct Scope(Lock @lock) |
| 115 | + { |
| 116 | + /// <summary> |
| 117 | + /// Exits the lock. |
| 118 | + /// </summary> |
| 119 | + /// <exception cref="SynchronizationLockException"/> |
| 120 | +#if !PARTIAL_SUPPORT |
| 121 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 122 | +#endif |
| 123 | + public readonly void Dispose() => @lock.Exit(); |
| 124 | + } |
| 125 | +} |
| 126 | +#endif |
0 commit comments