From da4298ae36beb9c54baf3e94dfdb3ca560507b9e Mon Sep 17 00:00:00 2001 From: Jakub Szczyrk Date: Mon, 23 Aug 2021 13:37:18 +0200 Subject: [PATCH 1/2] Added threadpool for check version integrity --- .../Commands/CheckVersionIntegrityCommand.cs | 75 +++++++++++++++++-- 1 file changed, 69 insertions(+), 6 deletions(-) diff --git a/Assets/PatchKit Patcher/Scripts/AppUpdater/Commands/CheckVersionIntegrityCommand.cs b/Assets/PatchKit Patcher/Scripts/AppUpdater/Commands/CheckVersionIntegrityCommand.cs index f6ebb4d0..a193ea3b 100644 --- a/Assets/PatchKit Patcher/Scripts/AppUpdater/Commands/CheckVersionIntegrityCommand.cs +++ b/Assets/PatchKit Patcher/Scripts/AppUpdater/Commands/CheckVersionIntegrityCommand.cs @@ -2,6 +2,7 @@ using System.IO; using System.Linq; using System.Diagnostics; +using System.Threading; using PatchKit.Api.Models.Main; using PatchKit.Unity.Patcher.AppData; using PatchKit.Unity.Patcher.AppData.Local; @@ -21,8 +22,9 @@ public class CheckVersionIntegrityCommand : BaseAppUpdaterCommand, ICheckVersion private readonly ILocalMetaData _localMetaData; private OperationStatus _status; - bool _isCheckingHash; - bool _isCheckingSize; + private volatile bool _isCheckingHash; + private volatile bool _isCheckingSize; + private volatile int _space; public CheckVersionIntegrityCommand(int versionId, AppContentSummary versionSummary, ILocalDirectory localDirectory, ILocalMetaData localMetaData, bool isCheckingHash, bool isCheckingSize) @@ -95,19 +97,80 @@ public override void Execute(CancellationToken cancellationToken) } } + private static volatile FileIntegrity[] files; + private void ExecuteInternal(CancellationToken cancellationToken) { DebugLogger.Log("Checking version integrity."); _status.IsActive.Value = true; - var files = new FileIntegrity[_versionSummary.Files.Length]; - - for (int i = 0; i < _versionSummary.Files.Length; i++) + files = new FileIntegrity[_versionSummary.Files.Length]; + int i = 0; + foreach (var file in _versionSummary.Files) { - files[i] = CheckFile(_versionSummary.Files[i]); + while (true) + { + int tmp; + lock (this) + { + tmp = _space; + } + + if (tmp < 16) + { + break; + } + + Thread.Sleep(1); + } + + lock (this) + { + _space++; + } + + var fileInThread = file; + var number = i; + ThreadPool.QueueUserWorkItem(state => + { + try + { + var fileIntegrity = CheckFile(fileInThread); + lock (this) + { + files[number] = fileIntegrity; + } + } + catch (Exception e) + { + DebugLogger.LogError(e.ToString()); + throw; + } + finally + { + lock (this) + { + _space--; + } + } + }); _status.Progress.Value = (i + 1) / (double) _versionSummary.Files.Length; + i++; + } + + while (true) + { + lock (this) + { + if (_space == 0) + { + break; + } + } + + Thread.Sleep(1000); } Results = new VersionIntegrity(files); From df8b38c1f2e2543176ce9bb6f13b1440d8f9ce2d Mon Sep 17 00:00:00 2001 From: Jakub Szczyrk Date: Wed, 22 Sep 2021 09:17:12 +0200 Subject: [PATCH 2/2] Refactoring --- .../Commands/CheckVersionIntegrityCommand.cs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/Assets/PatchKit Patcher/Scripts/AppUpdater/Commands/CheckVersionIntegrityCommand.cs b/Assets/PatchKit Patcher/Scripts/AppUpdater/Commands/CheckVersionIntegrityCommand.cs index a193ea3b..c08bd2c0 100644 --- a/Assets/PatchKit Patcher/Scripts/AppUpdater/Commands/CheckVersionIntegrityCommand.cs +++ b/Assets/PatchKit Patcher/Scripts/AppUpdater/Commands/CheckVersionIntegrityCommand.cs @@ -16,6 +16,7 @@ public class CheckVersionIntegrityCommand : BaseAppUpdaterCommand, ICheckVersion { private static readonly DebugLogger DebugLogger = new DebugLogger(typeof(CheckVersionIntegrityCommand)); + private const int MaxThreadCount = 16; private readonly int _versionId; private readonly AppContentSummary _versionSummary; private readonly ILocalDirectory _localDirectory; @@ -24,7 +25,7 @@ public class CheckVersionIntegrityCommand : BaseAppUpdaterCommand, ICheckVersion private OperationStatus _status; private volatile bool _isCheckingHash; private volatile bool _isCheckingSize; - private volatile int _space; + private volatile int _currentThreadCount; public CheckVersionIntegrityCommand(int versionId, AppContentSummary versionSummary, ILocalDirectory localDirectory, ILocalMetaData localMetaData, bool isCheckingHash, bool isCheckingSize) @@ -106,7 +107,7 @@ private void ExecuteInternal(CancellationToken cancellationToken) _status.IsActive.Value = true; files = new FileIntegrity[_versionSummary.Files.Length]; - int i = 0; + int fileIndex = 0; foreach (var file in _versionSummary.Files) { while (true) @@ -114,10 +115,10 @@ private void ExecuteInternal(CancellationToken cancellationToken) int tmp; lock (this) { - tmp = _space; + tmp = _currentThreadCount; } - if (tmp < 16) + if (tmp < MaxThreadCount) { break; } @@ -127,11 +128,11 @@ private void ExecuteInternal(CancellationToken cancellationToken) lock (this) { - _space++; + _currentThreadCount++; } var fileInThread = file; - var number = i; + var threadFileIndex = fileIndex; ThreadPool.QueueUserWorkItem(state => { try @@ -139,7 +140,7 @@ private void ExecuteInternal(CancellationToken cancellationToken) var fileIntegrity = CheckFile(fileInThread); lock (this) { - files[number] = fileIntegrity; + files[threadFileIndex] = fileIntegrity; } } catch (Exception e) @@ -151,20 +152,20 @@ private void ExecuteInternal(CancellationToken cancellationToken) { lock (this) { - _space--; + _currentThreadCount--; } } }); - _status.Progress.Value = (i + 1) / (double) _versionSummary.Files.Length; - i++; + _status.Progress.Value = (fileIndex + 1) / (double) _versionSummary.Files.Length; + fileIndex++; } while (true) { lock (this) { - if (_space == 0) + if (_currentThreadCount == 0) { break; }