AsyncLock.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Threading.Tasks;
  6. namespace System.Threading
  7. {
  8. public sealed class AsyncLock
  9. {
  10. private readonly object gate = new object();
  11. private readonly SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
  12. private readonly AsyncLocal<int> recursionCount = new AsyncLocal<int>();
  13. public Task<Releaser> LockAsync()
  14. {
  15. var shouldAcquire = false;
  16. lock (gate)
  17. {
  18. if (recursionCount.Value == 0)
  19. {
  20. shouldAcquire = true;
  21. recursionCount.Value = 1;
  22. }
  23. else
  24. {
  25. recursionCount.Value++;
  26. }
  27. }
  28. if (shouldAcquire)
  29. {
  30. return semaphore.WaitAsync().ContinueWith(_ => new Releaser(this));
  31. }
  32. return Task.FromResult(new Releaser(this));
  33. }
  34. private void Release()
  35. {
  36. lock (gate)
  37. {
  38. Debug.Assert(recursionCount.Value > 0);
  39. if (--recursionCount.Value == 0)
  40. {
  41. semaphore.Release();
  42. }
  43. }
  44. }
  45. public struct Releaser : IDisposable
  46. {
  47. private readonly AsyncLock parent;
  48. public Releaser(AsyncLock parent) => this.parent = parent;
  49. public void Dispose() => parent.Release();
  50. }
  51. }
  52. }