Skip to content

Commit

Permalink
fix: fixed the bug that method CurrentTaskFinished was executed mul…
Browse files Browse the repository at this point in the history
…tiple times by multiple threads (#1084)

* fixed the bug that method `CurrentTaskFinished` was executed multiple times by multiple threads (#1083) (#1082)

Signed-off-by: OsakaRuma <[email protected]>

* handling exception, deleted voice code

Signed-off-by: OsakaRuma <[email protected]>

* fixed typo of variable name

Signed-off-by: OsakaRuma <[email protected]>

* fixed typo in variable name

Signed-off-by: OsakaRuma <[email protected]>

* 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 <[email protected]>

---------

Signed-off-by: OsakaRuma <[email protected]>
  • Loading branch information
iamscottxu authored Sep 15, 2024
1 parent 58337a5 commit acc7149
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/Starward/Services/Download/InstallGameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,8 @@ public void ClearState()

protected void StartTask(InstallGameState state)
{
if (_concurrentExecuteThreadCount > 0) return;

if (state is InstallGameState.Download)
{
if (InstallTask is InstallGameTask.HardLink)
Expand Down Expand Up @@ -713,10 +715,26 @@ 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 tasks = new Task[Environment.ProcessorCount];
for (int i = 0; i < Environment.ProcessorCount; i++)
{
tasks[i] = ExecuteTaskItemAsync(_cancellationTokenSource.Token);
}
try
{
await Task.WhenAll(tasks).ConfigureAwait(false);
}
finally
{
CurrentTaskFinished();
}
}
}

Expand Down Expand Up @@ -1106,11 +1124,6 @@ protected async Task ExecuteTaskItemAsync(CancellationToken cancellationToken =
try
{
Interlocked.Increment(ref _concurrentExecuteThreadCount);
if (_concurrentExecuteThreadCount > Environment.ProcessorCount)
{
return;
}

while (_installItemQueue.TryDequeue(out InstallGameItem? item))
{
try
Expand Down Expand Up @@ -1161,10 +1174,6 @@ protected async Task ExecuteTaskItemAsync(CancellationToken cancellationToken =
finally
{
Interlocked.Decrement(ref _concurrentExecuteThreadCount);
if (_concurrentExecuteThreadCount == 0)
{
CurrentTaskFinished();
}
}
}

Expand Down

0 comments on commit acc7149

Please sign in to comment.