From af74aa35d76435f5bd01aeec77f406376b3b8c28 Mon Sep 17 00:00:00 2001 From: Mark Cilia Vincenti Date: Mon, 6 May 2024 03:21:54 +0200 Subject: [PATCH] Clean up synchronization (#11458) --- .../Attachments/AttachmentExtractor.cs | 14 +++++--------- .../Subtitles/SubtitleEncoder.cs | 14 +++++--------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs b/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs index a11440ced..3b7745b6a 100644 --- a/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs +++ b/MediaBrowser.MediaEncoding/Attachments/AttachmentExtractor.cs @@ -247,7 +247,7 @@ namespace MediaBrowser.MediaEncoding.Attachments MediaSourceInfo mediaSource, CancellationToken cancellationToken) { - var outputFileLocks = new List>(); + var outputFileLocks = new List(); var extractableAttachmentIds = new List(); try @@ -256,16 +256,15 @@ namespace MediaBrowser.MediaEncoding.Attachments { var outputPath = GetAttachmentCachePath(mediaPath, mediaSource, attachment.Index); - var @outputFileLock = _semaphoreLocks.GetOrAdd(outputPath); - await @outputFileLock.SemaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false); + var releaser = await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false); if (File.Exists(outputPath)) { - @outputFileLock.Dispose(); + releaser.Dispose(); continue; } - outputFileLocks.Add(@outputFileLock); + outputFileLocks.Add(releaser); extractableAttachmentIds.Add(attachment.Index); } @@ -280,10 +279,7 @@ namespace MediaBrowser.MediaEncoding.Attachments } finally { - foreach (var @outputFileLock in outputFileLocks) - { - @outputFileLock.Dispose(); - } + outputFileLocks.ForEach(x => x.Dispose()); } } diff --git a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs index 21f4cb1cd..16837d98b 100644 --- a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs +++ b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs @@ -457,7 +457,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles /// Task. private async Task ExtractAllTextSubtitles(MediaSourceInfo mediaSource, CancellationToken cancellationToken) { - var locks = new List>(); + var locks = new List(); var extractableStreams = new List(); try @@ -469,16 +469,15 @@ namespace MediaBrowser.MediaEncoding.Subtitles { var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetTextSubtitleFormat(subtitleStream)); - var @lock = _semaphoreLocks.GetOrAdd(outputPath); - await @lock.SemaphoreSlim.WaitAsync(cancellationToken).ConfigureAwait(false); + var releaser = await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false); if (File.Exists(outputPath)) { - @lock.Dispose(); + releaser.Dispose(); continue; } - locks.Add(@lock); + locks.Add(releaser); extractableStreams.Add(subtitleStream); } @@ -493,10 +492,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles } finally { - foreach (var @lock in locks) - { - @lock.Dispose(); - } + locks.ForEach(x => x.Dispose()); } }