| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Microsoft.Extensions.ObjectPool; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Runtime |
| | | 11 | | { |
| | | 12 | | internal class AsyncLock : IAsyncDisposable |
| | | 13 | | { |
| | 4 | 14 | | private static readonly ObjectPool<SemaphoreSlim> s_semaphorePool = (new DefaultObjectPoolProvider { MaximumReta |
| | 4 | 15 | | .Create(new SemaphoreSlimPooledObjectPolicy()); |
| | | 16 | | |
| | | 17 | | private AsyncLocal<SemaphoreSlim> _currentSemaphore; |
| | | 18 | | private SemaphoreSlim _topLevelSemaphore; |
| | | 19 | | private bool _isDisposed; |
| | | 20 | | |
| | 166 | 21 | | public AsyncLock() |
| | | 22 | | { |
| | 166 | 23 | | _topLevelSemaphore = s_semaphorePool.Get(); |
| | 166 | 24 | | _currentSemaphore = new AsyncLocal<SemaphoreSlim>(); |
| | 166 | 25 | | } |
| | | 26 | | |
| | | 27 | | public Task<IAsyncDisposable> TakeLockAsync() |
| | | 28 | | { |
| | 28 | 29 | | if (_isDisposed) |
| | 0 | 30 | | throw new ObjectDisposedException(nameof(AsyncLock)); |
| | | 31 | | |
| | 28 | 32 | | _currentSemaphore.Value = _currentSemaphore.Value ?? _topLevelSemaphore; |
| | 28 | 33 | | SemaphoreSlim currentSem = _currentSemaphore.Value; |
| | 28 | 34 | | var nextSem = s_semaphorePool.Get(); |
| | 28 | 35 | | _currentSemaphore.Value = nextSem; |
| | 28 | 36 | | var safeRelease = new SafeSemaphoreRelease(currentSem, nextSem, this); |
| | 28 | 37 | | return TakeLockCoreAsync(currentSem, safeRelease); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | private async Task<IAsyncDisposable> TakeLockCoreAsync(SemaphoreSlim currentSemaphore, SafeSemaphoreRelease safe |
| | | 41 | | { |
| | 28 | 42 | | await currentSemaphore.WaitAsync(); |
| | 28 | 43 | | return safeSemaphoreRelease; |
| | 28 | 44 | | } |
| | | 45 | | |
| | | 46 | | public IDisposable TakeLock() |
| | | 47 | | { |
| | 188 | 48 | | if (_isDisposed) |
| | 0 | 49 | | throw new ObjectDisposedException(nameof(AsyncLock)); |
| | | 50 | | |
| | 188 | 51 | | _currentSemaphore.Value = _currentSemaphore.Value ?? _topLevelSemaphore; |
| | 188 | 52 | | SemaphoreSlim currentSem = _currentSemaphore.Value; |
| | 188 | 53 | | currentSem.Wait(); |
| | 188 | 54 | | var nextSem = s_semaphorePool.Get(); |
| | 188 | 55 | | _currentSemaphore.Value = nextSem; |
| | 188 | 56 | | return new SafeSemaphoreRelease(currentSem, nextSem, this); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | public async ValueTask DisposeAsync() |
| | | 60 | | { |
| | 0 | 61 | | if (_isDisposed) |
| | 0 | 62 | | return; |
| | | 63 | | |
| | 0 | 64 | | _isDisposed = true; |
| | | 65 | | // Ensure the lock isn't held. If it is, wait for it to be released |
| | | 66 | | // before completing the dispose. |
| | 0 | 67 | | await _topLevelSemaphore.WaitAsync(); |
| | 0 | 68 | | _topLevelSemaphore.Release(); |
| | 0 | 69 | | s_semaphorePool.Return(_topLevelSemaphore); |
| | 0 | 70 | | _topLevelSemaphore = null; |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | private struct SafeSemaphoreRelease : IAsyncDisposable, IDisposable |
| | | 74 | | { |
| | | 75 | | private SemaphoreSlim _currentSemaphore; |
| | | 76 | | private SemaphoreSlim _nextSemaphore; |
| | | 77 | | private AsyncLock _asyncLock; |
| | | 78 | | |
| | | 79 | | public SafeSemaphoreRelease(SemaphoreSlim currentSemaphore, SemaphoreSlim nextSemaphore, AsyncLock asyncLock |
| | | 80 | | { |
| | 216 | 81 | | _currentSemaphore = currentSemaphore; |
| | 216 | 82 | | _nextSemaphore = nextSemaphore; |
| | 216 | 83 | | _asyncLock = asyncLock; |
| | 216 | 84 | | } |
| | | 85 | | |
| | | 86 | | public ValueTask DisposeAsync() |
| | | 87 | | { |
| | | 88 | | Debug.Assert(_nextSemaphore == _asyncLock._currentSemaphore.Value, "_nextSemaphore was expected to by th |
| | | 89 | | // Update _asyncLock._currentSemaphore in the calling ExecutionContext |
| | | 90 | | // and defer any awaits to DisposeCoreAsync(). If this isn't done, the |
| | | 91 | | // update will happen in a copy of the ExecutionContext and the caller |
| | | 92 | | // won't see the changes. |
| | 28 | 93 | | if (_currentSemaphore == _asyncLock._topLevelSemaphore) |
| | | 94 | | { |
| | 28 | 95 | | _asyncLock._currentSemaphore.Value = null; |
| | | 96 | | } |
| | | 97 | | else |
| | | 98 | | { |
| | 0 | 99 | | _asyncLock._currentSemaphore.Value = _currentSemaphore; |
| | | 100 | | } |
| | | 101 | | |
| | 28 | 102 | | return DisposeCoreAsync(); |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | private async ValueTask DisposeCoreAsync() |
| | | 106 | | { |
| | 28 | 107 | | await _nextSemaphore.WaitAsync(); |
| | 28 | 108 | | _currentSemaphore.Release(); |
| | 28 | 109 | | _nextSemaphore.Release(); |
| | 28 | 110 | | s_semaphorePool.Return(_nextSemaphore); |
| | 28 | 111 | | } |
| | | 112 | | |
| | | 113 | | public void Dispose() |
| | | 114 | | { |
| | | 115 | | Debug.Assert(_nextSemaphore == _asyncLock._currentSemaphore.Value, "_nextSemaphore was expected to by th |
| | 188 | 116 | | if (_currentSemaphore == _asyncLock._topLevelSemaphore) |
| | | 117 | | { |
| | 188 | 118 | | _asyncLock._currentSemaphore.Value = null; |
| | | 119 | | } |
| | | 120 | | else |
| | | 121 | | { |
| | 0 | 122 | | _asyncLock._currentSemaphore.Value = _currentSemaphore; |
| | | 123 | | } |
| | | 124 | | |
| | 188 | 125 | | _nextSemaphore.Wait(); |
| | 188 | 126 | | _currentSemaphore.Release(); |
| | 188 | 127 | | _nextSemaphore.Release(); |
| | 188 | 128 | | s_semaphorePool.Return(_nextSemaphore); |
| | 188 | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | private class SemaphoreSlimPooledObjectPolicy : PooledObjectPolicy<SemaphoreSlim> |
| | | 133 | | { |
| | | 134 | | public override SemaphoreSlim Create() |
| | | 135 | | { |
| | 170 | 136 | | return new SemaphoreSlim(1); |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | public override bool Return(SemaphoreSlim obj) |
| | | 140 | | { |
| | 216 | 141 | | if (obj.CurrentCount != 1) |
| | | 142 | | { |
| | | 143 | | Debug.Fail("Shouldn't be returning semaphore with a count != 1"); |
| | 0 | 144 | | return false; |
| | | 145 | | } |
| | | 146 | | |
| | 216 | 147 | | return true; |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | } |