-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathAsyncMutex.cs
More file actions
62 lines (54 loc) · 2.36 KB
/
AsyncMutex.cs
File metadata and controls
62 lines (54 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Runtime.CompilerServices;
namespace CommunityToolkit.WinUI.Media;
/// <summary>
/// An <see langword="async"/> <see cref="AsyncMutex"/> implementation that can be easily used inside a <see langword="using"/> block
/// </summary>
#pragma warning disable CA1001 // Types that own disposable fields should be disposable
#pragma warning disable CsWinRT1028 // Partial not required for types never passed to native WinRT method.
internal sealed class AsyncMutex
#pragma warning restore CsWinRT1028
#pragma warning restore CA1001 // Types that own disposable fields should be disposable
{
/// <summary>
/// The underlying <see cref="SemaphoreSlim"/> instance in use
/// </summary>
private readonly SemaphoreSlim semaphore = new SemaphoreSlim(1);
/// <summary>
/// Acquires a lock for the current instance, that is automatically released outside the <see langword="using"/> block
/// </summary>
/// <returns>A <see cref="Task{T}"/> that returns an <see cref="IDisposable"/> instance to release the lock</returns>
public async Task<IDisposable> LockAsync()
{
await this.semaphore.WaitAsync().ConfigureAwait(false);
return new Lock(this.semaphore);
}
/// <summary>
/// Private class that implements the automatic release of the semaphore
/// </summary>
#pragma warning disable CsWinRT1028 // Partial not required for types never passed to native WinRT method.
private sealed class Lock : IDisposable
#pragma warning restore CsWinRT1028
{
/// <summary>
/// The <see cref="SemaphoreSlim"/> instance of the parent class
/// </summary>
private readonly SemaphoreSlim semaphore;
/// <summary>
/// Initializes a new instance of the <see cref="Lock"/> class.
/// </summary>
/// <param name="semaphore">The <see cref="SemaphoreSlim"/> instance of the parent class</param>
public Lock(SemaphoreSlim semaphore)
{
this.semaphore = semaphore;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IDisposable.Dispose()
{
this.semaphore.Release();
}
}
}