Skip to content
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

Made compile script on separate thread optional #2050

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions doc/source/structures/misc/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
1 change: 1 addition & 0 deletions src/kOS.Safe/Encapsulation/IConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public interface IConfig: ISuffixed
DateTime TimeStamp { get; }

bool DebugEachOpcode { get; set; }
bool PauseOnCompile { get; set; }

void SaveConfig();
IList<ConfigKey> GetConfigKeys();
Expand Down
32 changes: 25 additions & 7 deletions src/kOS.Safe/Execution/YiedFinishedThreadedDetector.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace kOS.Safe.Execution
Expand All @@ -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();
Expand Down Expand Up @@ -93,5 +100,16 @@ private void DoThread()
/// the main thread and is not required to be thread safe with respect to KSP.
/// </summary>
public abstract void ThreadFinish();

/// <summary>
/// Determines if it must execute DoThread() when Begin() is invoked using caller thread.
/// <br/>
/// This method method is used by descendants to tell whether they want to freeze a game.
/// </summary>
/// <returns>false to run in separate thread, true to freeze a game</returns>
protected virtual bool RunOnCaller()
{
return true;
}
}
}
7 changes: 6 additions & 1 deletion src/kOS.Safe/Execution/YieldFinishedCompile.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/kOS/Module/kOSCustomParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/kOS/Suffixed/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Config : Structure, IConfig
public string TerminalFontName {get { return GetPropValue<string>(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.
Expand Down Expand Up @@ -61,6 +62,7 @@ private void InitializeSuffixes()
AddSuffix("BLIZZY", new SetSuffix<BooleanValue>(() => UseBlizzyToolbarOnly, value => UseBlizzyToolbarOnly = value));
AddSuffix("BRIGHTNESS", new ClampSetSuffix<ScalarValue>(() => TerminalBrightness, value => TerminalBrightness = value, 0f, 1f, 0.01f));
AddSuffix("DEFAULTFONTSIZE", new ClampSetSuffix<ScalarValue>(() => TerminalFontDefaultSize, value => TerminalFontDefaultSize = value, 6f, 30f, 1f));
AddSuffix("PAUSEONCOMPILE", new SetSuffix<BooleanValue>(() => PauseOnCompile, value => PauseOnCompile = value));
}

private void BuildValuesDictionary()
Expand Down