Skip to content

Added threadpool for check version integrity #176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: experimental/improved-download-unpacking-installation
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,14 +16,16 @@ 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;
private readonly ILocalMetaData _localMetaData;

private OperationStatus _status;
bool _isCheckingHash;
bool _isCheckingSize;
private volatile bool _isCheckingHash;
private volatile bool _isCheckingSize;
private volatile int _currentThreadCount;

public CheckVersionIntegrityCommand(int versionId, AppContentSummary versionSummary,
ILocalDirectory localDirectory, ILocalMetaData localMetaData, bool isCheckingHash, bool isCheckingSize)
Expand Down Expand Up @@ -95,19 +98,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];
files = new FileIntegrity[_versionSummary.Files.Length];
int fileIndex = 0;
foreach (var file in _versionSummary.Files)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for can be reverted here, so only i needs to be passed to the thread (_versionSummary.Files is not modified anywhere so can be freely accessed from worker thread).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@genail you suggested using foreach here because it works faster.

{
while (true)
{
int tmp;
lock (this)
{
tmp = _currentThreadCount;
}

if (tmp < MaxThreadCount)
{
break;
}

Thread.Sleep(1);
}

lock (this)
{
_currentThreadCount++;
}

var fileInThread = file;
var threadFileIndex = fileIndex;
ThreadPool.QueueUserWorkItem(state =>
{
try
{
var fileIntegrity = CheckFile(fileInThread);
lock (this)
{
files[threadFileIndex] = fileIntegrity;
}
}
catch (Exception e)
{
DebugLogger.LogError(e.ToString());
throw;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't allow a thread to end up with an exception. A different solution is required here (like storing lastThreadException that is checked sometimes and rethrown is not null).

}
finally
{
lock (this)
{
_currentThreadCount--;
}
}
});

for (int i = 0; i < _versionSummary.Files.Length; i++)
_status.Progress.Value = (fileIndex + 1) / (double) _versionSummary.Files.Length;
fileIndex++;
}

while (true)
{
files[i] = CheckFile(_versionSummary.Files[i]);
lock (this)
{
if (_currentThreadCount == 0)
{
break;
}
}

_status.Progress.Value = (i + 1) / (double) _versionSummary.Files.Length;
Thread.Sleep(1000);
}

Results = new VersionIntegrity(files);
Expand Down