-
Notifications
You must be signed in to change notification settings - Fork 19
Added thread pool for installation #173
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
base: experimental/improved-download-unpacking-installation
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Threading; | ||
using PatchKit.Api.Models.Main; | ||
using PatchKit.Unity.Patcher.AppData; | ||
using PatchKit.Unity.Patcher.AppData.FileSystem; | ||
using PatchKit.Unity.Patcher.AppData.Local; | ||
using PatchKit.Unity.Patcher.AppUpdater.Status; | ||
using PatchKit.Unity.Patcher.Cancellation; | ||
using PatchKit.Unity.Patcher.Debug; | ||
using UnityEngine; | ||
|
||
namespace PatchKit.Unity.Patcher.AppUpdater.Commands | ||
{ | ||
|
@@ -31,6 +32,7 @@ public class InstallContentCommand : BaseAppUpdaterCommand, IInstallContentComma | |
private OperationStatus _copyFilesStatus; | ||
private OperationStatus _unarchivePackageStatus; | ||
private Pack1Meta _pack1Meta; | ||
private volatile int _space = 0; | ||
|
||
public InstallContentCommand(string packagePath, string packageMetaPath, string packagePassword, int versionId, | ||
AppContentSummary versionContentSummary, ILocalDirectory localData, ILocalMetaData localMetaData) | ||
|
@@ -143,7 +145,7 @@ public override void Execute(CancellationToken cancellationToken) | |
string nameHash; | ||
if (mapHashExtractedFiles.TryGetHash(filePath, out nameHash)) | ||
{ | ||
var sourceFile = new SourceFile(filePath, packageDir.Path, usedSuffix, nameHash, _versionContentSummary.Size); | ||
var sourceFile = new SourceFile(filePath, packageDir.Path, usedSuffix, nameHash, _versionContentSummary.Files[i].Size); | ||
|
||
if (unarchiver.HasErrors && !sourceFile.Exists()) // allow unexistent file only if does not have errors | ||
{ | ||
|
@@ -168,6 +170,18 @@ public override void Execute(CancellationToken cancellationToken) | |
_copyFilesStatus.Progress.Value = 1.0; | ||
_copyFilesStatus.IsActive.Value = false; | ||
}); | ||
while (true) | ||
{ | ||
lock (this) | ||
{ | ||
if (_space == 0) | ||
{ | ||
break; | ||
} | ||
UnityEngine.Debug.Log(_space); | ||
} | ||
Thread.Sleep(1000); | ||
} | ||
} | ||
|
||
private IUnarchiver CreateUnrachiver(string destinationDir, MapHashExtractedFiles mapHashExtractedFiles, out string usedSuffix) | ||
|
@@ -210,10 +224,46 @@ private void InstallFile(SourceFile sourceFile, CancellationToken cancellationTo | |
throw new FilePathTooLongException(string.Format("Cannot install file {0}, the destination path length has exceeded Windows path length limit (260).", destinationFilePath)); | ||
} | ||
#endif | ||
FileOperations.Move(sourceFile.FullHashPath, destinationFilePath, cancellationToken); | ||
_localMetaData.RegisterEntry(sourceFile.Name, _versionId, sourceFile.Size, isLastEntry); | ||
while (true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please try to modify this code so it uses semaphores - https://docs.microsoft.com/en-us/dotnet/api/system.threading.semaphore?view=net-5.0 |
||
{ | ||
int tmp; | ||
lock (this) | ||
{ | ||
tmp = _space; | ||
} | ||
if (tmp < 16) | ||
{ | ||
break; | ||
} | ||
Thread.Sleep(1); | ||
} | ||
lock (this) | ||
{ | ||
_space++; | ||
} | ||
|
||
ThreadPool.QueueUserWorkItem(state => | ||
{ | ||
try | ||
{ | ||
FileOperations.Move(sourceFile.FullHashPath, destinationFilePath, cancellationToken); | ||
} | ||
catch (Exception e) | ||
{ | ||
DebugLogger.LogError(e.ToString()); | ||
throw; | ||
} | ||
finally | ||
{ | ||
lock (this) | ||
{ | ||
_space--; | ||
} | ||
} | ||
}); | ||
_localMetaData.RegisterEntry(sourceFile.Name, _versionId, sourceFile.Size, isLastEntry); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to be done on thread as well since we cannot assume that thread succeeds and register the entry before it's indeed installed. |
||
} | ||
|
||
struct SourceFile | ||
{ | ||
public string Name { get; private set; } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change related to the thread pool support?
If not please move it to another PR.