From 633bbe6f774fe6af4e5dace4b32bc7da91e5d75d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 2 Sep 2021 09:01:12 -0500 Subject: atomically obtain or reject semaphore --- src/slskd/Application/SharedFileCache.cs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index e9ebeb66..a469de22 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -107,19 +107,17 @@ namespace slskd /// The operation context. public async Task FillAsync() { - if (SyncRoot.CurrentCount == 0) + // obtain the semaphore, or fail if it can't be obtained immediately, indicating that a scan is running. + if (!await SyncRoot.WaitAsync(millisecondsTimeout: 0)) { Log.Warning("Shared file scan rejected; scan is already in progress."); throw new ShareScanInProgressException("Shared files are already being scanned."); } - await Task.Yield(); - - await SyncRoot.WaitAsync(); - try { - // todo: don't do this, but rather build a new cache and then swap it in + await Task.Yield(); + ResetCache(); State.SetValue(state => state with { Filling = true, FillProgress = 0 }); -- cgit v1.2.3