Skip to content

Commit c3e86cb

Browse files
Yosif-Smintconst-cloudinary
authored andcommitted
Use SemaphoreSlim instead of Mutex for async locking
1 parent de6307b commit c3e86cb

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

CloudinaryDotNet/FileDescription.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class FileDescription : IDisposable
4444

4545
private const int UnlimitedBuffer = int.MaxValue;
4646

47-
private readonly Mutex mutex = new ();
47+
private readonly SemaphoreSlim semaphoreSlim = new (1, 1);
4848

4949
private readonly object chunkLock = new ();
5050

@@ -290,7 +290,7 @@ public void AddChunk(Stream chunkStream, long startByte, long chunkSize, bool la
290290
public async Task<ChunkData> GetNextChunkAsync(CancellationToken? cancellationToken = null)
291291
{
292292
// lock this section, so we don't send the same chunk multiple times.
293-
mutex.WaitOne();
293+
await semaphoreSlim.WaitAsync().ConfigureAwait(false);
294294
try
295295
{
296296
Stream resultingStream;
@@ -338,7 +338,7 @@ public async Task<ChunkData> GetNextChunkAsync(CancellationToken? cancellationTo
338338
}
339339
finally
340340
{
341-
mutex.ReleaseMutex();
341+
semaphoreSlim.Release();
342342
}
343343
}
344344

@@ -426,7 +426,7 @@ protected virtual void Dispose(bool disposing)
426426
chunks?.Dispose();
427427
chunks = null;
428428

429-
mutex.Dispose();
429+
semaphoreSlim.Dispose();
430430
}
431431

432432
disposedValue = true;

0 commit comments

Comments
 (0)