From 381afe2684b801a2f1cca62b011371bc5a8e0387 Mon Sep 17 00:00:00 2001 From: valery-labuzhsky Date: Tue, 13 Jun 2017 13:35:24 +0300 Subject: [PATCH] Made compile script on separate thread optional --- doc/source/structures/misc/config.rst | 16 ++++++++++ src/kOS.Safe/Encapsulation/IConfig.cs | 1 + .../Execution/YiedFinishedThreadedDetector.cs | 32 +++++++++++++++---- .../Execution/YieldFinishedCompile.cs | 7 +++- src/kOS/Module/kOSCustomParameters.cs | 4 +++ src/kOS/Suffixed/Config.cs | 2 ++ 6 files changed, 54 insertions(+), 8 deletions(-) diff --git a/doc/source/structures/misc/config.rst b/doc/source/structures/misc/config.rst index e9991454e..bed23119b 100644 --- a/doc/source/structures/misc/config.rst +++ b/doc/source/structures/misc/config.rst @@ -108,6 +108,10 @@ Configuration of kOS - :struct:`Boolean` - false - Unholy debug spam used by the kOS developers + * - :attr:`PAUSEONCOMPILE` + - :struct:`Boolean` + - false + - Pause when a program is being compiled .. attribute:: Config:IPU @@ -327,3 +331,15 @@ Configuration of kOS the kOS dev team. This change takes effect immediately. + +.. attribute:: Config:PAUSEONCOMPILE + + :access: Get/Set + :type: :struct:`Boolean` + + Configures the ``pauseOnCompile`` setting. + + If true, kOS will freeze a game any time it needs to compile a script. + Default value is false. + + This change takes effect immediately. diff --git a/src/kOS.Safe/Encapsulation/IConfig.cs b/src/kOS.Safe/Encapsulation/IConfig.cs index 99d321b58..0433c10e2 100644 --- a/src/kOS.Safe/Encapsulation/IConfig.cs +++ b/src/kOS.Safe/Encapsulation/IConfig.cs @@ -31,6 +31,7 @@ public interface IConfig: ISuffixed DateTime TimeStamp { get; } bool DebugEachOpcode { get; set; } + bool PauseOnCompile { get; set; } void SaveConfig(); IList GetConfigKeys(); diff --git a/src/kOS.Safe/Execution/YiedFinishedThreadedDetector.cs b/src/kOS.Safe/Execution/YiedFinishedThreadedDetector.cs index 2a9e2a40a..7ec4bd5e2 100644 --- a/src/kOS.Safe/Execution/YiedFinishedThreadedDetector.cs +++ b/src/kOS.Safe/Execution/YiedFinishedThreadedDetector.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading; namespace kOS.Safe.Execution @@ -25,16 +22,26 @@ public override void Begin(SafeSharedObjects sharedObj) ThreadInitialize(sharedObj); - childThread = new Thread(DoThread); - childThread.IsBackground = true; - childThread.Start(); + if (RunOnCaller()) + { + DoThread(); + } + else + { + childThread = new Thread(DoThread); + childThread.IsBackground = true; + childThread.Start(); + } } public override bool IsFinished() { if (childThreadEvent.WaitOne(0)) { - childThread.Join(); + if (childThread != null) + { + childThread.Join(); + } if (childException == null) { ThreadFinish(); @@ -93,5 +100,16 @@ private void DoThread() /// the main thread and is not required to be thread safe with respect to KSP. /// public abstract void ThreadFinish(); + + /// + /// Determines if it must execute DoThread() when Begin() is invoked using caller thread. + ///
+ /// This method method is used by descendants to tell whether they want to freeze a game. + ///
+ /// false to run in separate thread, true to freeze a game + protected virtual bool RunOnCaller() + { + return true; + } } } diff --git a/src/kOS.Safe/Execution/YieldFinishedCompile.cs b/src/kOS.Safe/Execution/YieldFinishedCompile.cs index 5b340f5e8..dbc7f99e2 100644 --- a/src/kOS.Safe/Execution/YieldFinishedCompile.cs +++ b/src/kOS.Safe/Execution/YieldFinishedCompile.cs @@ -1,8 +1,8 @@ using kOS.Safe.Compilation; using kOS.Safe.Encapsulation; using kOS.Safe.Persistence; +using kOS.Safe.Utilities; using System.Collections.Generic; -using System.Threading; namespace kOS.Safe.Execution { @@ -75,6 +75,11 @@ public override void ThreadFinish() } } + protected override bool RunOnCaller() + { + return SafeHouse.Config.PauseOnCompile; + } + public static YieldFinishedCompile RunScript(GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions) { var ret = new YieldFinishedCompile(scriptPath, lineNumber, fileContent, contextIdentifier, compilerOptions); diff --git a/src/kOS/Module/kOSCustomParameters.cs b/src/kOS/Module/kOSCustomParameters.cs index 5862142ca..7ed51b3f1 100644 --- a/src/kOS/Module/kOSCustomParameters.cs +++ b/src/kOS/Module/kOSCustomParameters.cs @@ -112,6 +112,10 @@ public int InstructionsPerUpdate "an opcode is executed in the virtual machine. Very laggy.")] public bool debugEachOpcode = false; + [GameParameters.CustomParameterUI("Pause when compile", + toolTip = "A game will be frozen while kOS compiles script.")] + public bool pauseOnCompile = false; + public override GameParameters.GameMode GameMode { get diff --git a/src/kOS/Suffixed/Config.cs b/src/kOS/Suffixed/Config.cs index e132a59e2..58708ab90 100644 --- a/src/kOS/Suffixed/Config.cs +++ b/src/kOS/Suffixed/Config.cs @@ -31,6 +31,7 @@ public class Config : Structure, IConfig public string TerminalFontName {get { return GetPropValue(PropId.TerminalFontName); } set { SetPropValue(PropId.TerminalFontName, value); } } public bool UseBlizzyToolbarOnly { get { return kOSCustomParameters.Instance.useBlizzyToolbarOnly; } set { kOSCustomParameters.Instance.useBlizzyToolbarOnly = value; } } public bool DebugEachOpcode { get { return kOSCustomParameters.Instance.debugEachOpcode; } set { kOSCustomParameters.Instance.debugEachOpcode = value; } } + public bool PauseOnCompile { get { return kOSCustomParameters.Instance.pauseOnCompile; } set { kOSCustomParameters.Instance.pauseOnCompile = value; } } // NOTE TO FUTURE MAINTAINERS: If it looks like overkill to use a double instead of a float for this next field, you're right. // But KSP seems to have a bug where single-precision floats don't get saved in the config XML file. Doubles seem to work, though. @@ -61,6 +62,7 @@ private void InitializeSuffixes() AddSuffix("BLIZZY", new SetSuffix(() => UseBlizzyToolbarOnly, value => UseBlizzyToolbarOnly = value)); AddSuffix("BRIGHTNESS", new ClampSetSuffix(() => TerminalBrightness, value => TerminalBrightness = value, 0f, 1f, 0.01f)); AddSuffix("DEFAULTFONTSIZE", new ClampSetSuffix(() => TerminalFontDefaultSize, value => TerminalFontDefaultSize = value, 6f, 30f, 1f)); + AddSuffix("PAUSEONCOMPILE", new SetSuffix(() => PauseOnCompile, value => PauseOnCompile = value)); } private void BuildValuesDictionary()