|
1 | 1 | // Copyright (c) usercode |
2 | | -// https://github.com/usercode/ImageWizard |
| 2 | +// https://github.com/usercode/AsyncLock |
3 | 3 | // MIT License |
4 | 4 |
|
5 | | -using System; |
6 | | -using System.Collections.Generic; |
7 | | -using System.Linq; |
8 | | -using System.Text; |
9 | | -using System.Threading; |
10 | | -using System.Threading.Tasks; |
11 | | - |
12 | | -namespace ImageWizard.Core.Locking; |
| 5 | +namespace AsyncLock; |
13 | 6 |
|
14 | 7 | /// <summary> |
15 | 8 | /// AsyncLock |
@@ -126,65 +119,15 @@ public Task<AsyncLockReleaser> WriterLockAsync(CancellationToken cancellation = |
126 | 119 | } |
127 | 120 | } |
128 | 121 |
|
129 | | - internal Task<AsyncLockReleaser> SwitchToWriterLockAsync(AsyncLockReleaser releaser, CancellationToken cancellation = default) |
130 | | - { |
131 | | - if (cancellation.IsCancellationRequested) |
132 | | - { |
133 | | - return Task.FromCanceled<AsyncLockReleaser>(cancellation); |
134 | | - } |
135 | | - |
136 | | - lock (_syncObj) |
137 | | - { |
138 | | - if (releaser.Type == AsyncLockType.Write) |
139 | | - { |
140 | | - throw new Exception("There is already a writer lock."); |
141 | | - } |
142 | | - |
143 | | - var taskWriter = WriterLockAsync(cancellation); |
144 | | - |
145 | | - if (releaser.Type != null) |
146 | | - { |
147 | | - Release(releaser.Type.Value, sendReleasedEvent: false); |
148 | | - } |
149 | | - |
150 | | - return taskWriter; |
151 | | - } |
152 | | - } |
153 | | - |
154 | | - internal Task<AsyncLockReleaser> SwitchToReaderLockAsync(AsyncLockReleaser releaser, bool skipWaitingWriters = false, CancellationToken cancellation = default) |
155 | | - { |
156 | | - if (cancellation.IsCancellationRequested) |
157 | | - { |
158 | | - return Task.FromCanceled<AsyncLockReleaser>(cancellation); |
159 | | - } |
160 | | - |
161 | | - lock (_syncObj) |
162 | | - { |
163 | | - if (releaser.Type == AsyncLockType.Read) |
164 | | - { |
165 | | - throw new Exception("There is already a reader lock."); |
166 | | - } |
167 | | - |
168 | | - var taskReader = ReaderLockAsync(cancellation); |
169 | | - |
170 | | - if (releaser.Type != null) |
171 | | - { |
172 | | - Release(releaser.Type.Value, skipWaitingWriters: skipWaitingWriters, sendReleasedEvent: false); |
173 | | - } |
174 | | - |
175 | | - return taskReader; |
176 | | - } |
177 | | - } |
178 | | - |
179 | | - internal void Release(AsyncLockType type, bool skipWaitingWriters = false, bool sendReleasedEvent = true) |
| 122 | + internal void Release(AsyncLockType type, bool sendReleasedEvent = true) |
180 | 123 | { |
181 | 124 | lock (_syncObj) |
182 | 125 | { |
183 | 126 | try |
184 | 127 | { |
185 | 128 | if (type == AsyncLockType.Write) |
186 | 129 | { |
187 | | - WriterRelease(skipWaitingWriters); |
| 130 | + WriterRelease(); |
188 | 131 | } |
189 | 132 | else |
190 | 133 | { |
@@ -212,15 +155,10 @@ private void ReaderRelease() |
212 | 155 | } |
213 | 156 | } |
214 | 157 |
|
215 | | - private void WriterRelease(bool skipWaitingWriters) |
| 158 | + private void WriterRelease() |
216 | 159 | { |
217 | | - _isWriterRunning = false; |
218 | | - |
219 | 160 | //start next writer lock? |
220 | | - if (skipWaitingWriters == false) |
221 | | - { |
222 | | - StartNextWaitingWriter(); |
223 | | - } |
| 161 | + StartNextWaitingWriter(); |
224 | 162 |
|
225 | 163 | //no running writer lock? |
226 | 164 | if (_isWriterRunning == false) |
|
0 commit comments