From 57da19b2042695cc2c686ce8ffa3ac49d4c84394 Mon Sep 17 00:00:00 2001 From: OsakaRuma Date: Sun, 8 Sep 2024 14:12:50 +0800 Subject: [PATCH 1/5] fixed the bug that method `CurrentTaskFinished` was executed multiple times by multiple threads (#1083) (#1082) Signed-off-by: OsakaRuma --- .../Services/Download/InstallGameService.cs | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/Starward/Services/Download/InstallGameService.cs b/src/Starward/Services/Download/InstallGameService.cs index dcfb4f570..e64dbfcd2 100644 --- a/src/Starward/Services/Download/InstallGameService.cs +++ b/src/Starward/Services/Download/InstallGameService.cs @@ -713,10 +713,20 @@ protected void StartTask(InstallGameState state) _finishBytes = 0; } State = state; - _cancellationTokenSource = new CancellationTokenSource(); - for (int i = 0; i < Environment.ProcessorCount; i++) + + _ = RunTasksAsync(); //不需要ConfigureAwait,因为返回值丢弃,且无需调用“.GetAwaiter().OnCompleted()” + return; + + async Task RunTasksAsync() { - _ = ExecuteTaskItemAsync(_cancellationTokenSource.Token).ConfigureAwait(false); + _cancellationTokenSource = new CancellationTokenSource(); + var tsaks = new Task[Environment.ProcessorCount]; + for (int i = 0; i < Environment.ProcessorCount; i++) + { + tsaks[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token); + } + await Task.WhenAll(tsaks).ConfigureAwait(false); + CurrentTaskFinished(); } } @@ -1105,8 +1115,8 @@ protected async Task ExecuteTaskItemAsync(CancellationToken cancellationToken = { try { - Interlocked.Increment(ref _concurrentExecuteThreadCount); - if (_concurrentExecuteThreadCount > Environment.ProcessorCount) + var concurrentExecuteThreadCount = Interlocked.Increment(ref _concurrentExecuteThreadCount); + if (concurrentExecuteThreadCount > Environment.ProcessorCount) { return; } @@ -1161,10 +1171,6 @@ protected async Task ExecuteTaskItemAsync(CancellationToken cancellationToken = finally { Interlocked.Decrement(ref _concurrentExecuteThreadCount); - if (_concurrentExecuteThreadCount == 0) - { - CurrentTaskFinished(); - } } } From e64c24c55e09fde6d2d2e766bf01373936b403c7 Mon Sep 17 00:00:00 2001 From: OsakaRuma Date: Sun, 8 Sep 2024 14:19:56 +0800 Subject: [PATCH 2/5] handling exception, deleted voice code Signed-off-by: OsakaRuma --- .../Services/Download/InstallGameService.cs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Starward/Services/Download/InstallGameService.cs b/src/Starward/Services/Download/InstallGameService.cs index e64dbfcd2..d44fb9a2f 100644 --- a/src/Starward/Services/Download/InstallGameService.cs +++ b/src/Starward/Services/Download/InstallGameService.cs @@ -627,6 +627,8 @@ public void ClearState() protected void StartTask(InstallGameState state) { + if (State != InstallGameState.None) return; + if (state is InstallGameState.Download) { if (InstallTask is InstallGameTask.HardLink) @@ -725,8 +727,14 @@ async Task RunTasksAsync() { tsaks[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token); } - await Task.WhenAll(tsaks).ConfigureAwait(false); - CurrentTaskFinished(); + try + { + await Task.WhenAll(tsaks).ConfigureAwait(false); + } + finally + { + CurrentTaskFinished(); + } } } @@ -1115,12 +1123,7 @@ protected async Task ExecuteTaskItemAsync(CancellationToken cancellationToken = { try { - var concurrentExecuteThreadCount = Interlocked.Increment(ref _concurrentExecuteThreadCount); - if (concurrentExecuteThreadCount > Environment.ProcessorCount) - { - return; - } - + Interlocked.Increment(ref _concurrentExecuteThreadCount); while (_installItemQueue.TryDequeue(out InstallGameItem? item)) { try From 631be21abbd16542255f91f231132407c0635c87 Mon Sep 17 00:00:00 2001 From: OsakaRuma Date: Sun, 8 Sep 2024 15:07:34 +0800 Subject: [PATCH 3/5] fixed typo of variable name Signed-off-by: OsakaRuma --- src/Starward/Services/Download/InstallGameService.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Starward/Services/Download/InstallGameService.cs b/src/Starward/Services/Download/InstallGameService.cs index d44fb9a2f..ac2dd9366 100644 --- a/src/Starward/Services/Download/InstallGameService.cs +++ b/src/Starward/Services/Download/InstallGameService.cs @@ -722,14 +722,14 @@ protected void StartTask(InstallGameState state) async Task RunTasksAsync() { _cancellationTokenSource = new CancellationTokenSource(); - var tsaks = new Task[Environment.ProcessorCount]; + var tasts = new Task[Environment.ProcessorCount]; for (int i = 0; i < Environment.ProcessorCount; i++) { - tsaks[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token); + tasts[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token); } try { - await Task.WhenAll(tsaks).ConfigureAwait(false); + await Task.WhenAll(tasts).ConfigureAwait(false); } finally { From cc0a99dd50317b02a07421e0b58e21fa8c8cbdb0 Mon Sep 17 00:00:00 2001 From: OsakaRuma Date: Sun, 8 Sep 2024 15:10:09 +0800 Subject: [PATCH 4/5] fixed typo in variable name Signed-off-by: OsakaRuma --- src/Starward/Services/Download/InstallGameService.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Starward/Services/Download/InstallGameService.cs b/src/Starward/Services/Download/InstallGameService.cs index ac2dd9366..4f85ef711 100644 --- a/src/Starward/Services/Download/InstallGameService.cs +++ b/src/Starward/Services/Download/InstallGameService.cs @@ -722,14 +722,14 @@ protected void StartTask(InstallGameState state) async Task RunTasksAsync() { _cancellationTokenSource = new CancellationTokenSource(); - var tasts = new Task[Environment.ProcessorCount]; + var tasks = new Task[Environment.ProcessorCount]; for (int i = 0; i < Environment.ProcessorCount; i++) { - tasts[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token); + tasks[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token); } try { - await Task.WhenAll(tasts).ConfigureAwait(false); + await Task.WhenAll(tasks).ConfigureAwait(false); } finally { From 9ad094458e2d1c51188151491c81ae62bbf72c17 Mon Sep 17 00:00:00 2001 From: OsakaRuma Date: Sun, 8 Sep 2024 22:56:56 +0800 Subject: [PATCH 5/5] fixed a bug where only the first step of the task could be run if there were multiple steps of the task when installing the game Signed-off-by: OsakaRuma --- src/Starward/Services/Download/InstallGameService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Starward/Services/Download/InstallGameService.cs b/src/Starward/Services/Download/InstallGameService.cs index 4f85ef711..8a8bd84a3 100644 --- a/src/Starward/Services/Download/InstallGameService.cs +++ b/src/Starward/Services/Download/InstallGameService.cs @@ -627,7 +627,7 @@ public void ClearState() protected void StartTask(InstallGameState state) { - if (State != InstallGameState.None) return; + if (_concurrentExecuteThreadCount > 0) return; if (state is InstallGameState.Download) {