diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..3bf781337 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,7 @@ +[*.cs] +indent_style = space +indent_size = 4 +charset = utf-8 +csharp_space_after_keywords_in_control_flow_statements = true +csharp_space_between_method_declaration_parameter_list_parentheses = false +csharp_space_between_method_call_parameter_list_parentheses = false diff --git a/.gitignore b/.gitignore index 1dd4bff18..c14e39760 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # VisualStudio +.vs/ [Bb]in/ [Oo]bj/ @@ -21,6 +22,9 @@ packages/ # VisualStudio Code .vscode/ +# Vim +*.swp + # Unity [Ll]ibrary/ [Tt]emp/ diff --git a/doc/source/general/volumes.rst b/doc/source/general/volumes.rst index 1553e9c09..d41a28910 100644 --- a/doc/source/general/volumes.rst +++ b/doc/source/general/volumes.rst @@ -191,10 +191,11 @@ migration. As soon as you vessel leaves VAB/SPH and is being initialised on the launchpad (e.g. its status is PRELAUNCH) the assigned script will be copied to CPU's -local hard disk with the same name. If kOS is configured to start on the -archive, the file will not be copied locally automatically. This script will -be run as soon as CPU boots, e.g. as soon as you bring your CPU in physics -range or power on your CPU if it was turned off. You may get or set the name +local hard disk with the same name. This script will be run as soon as CPU boots, +e.g. as soon as you bring your CPU in physics range or power on your CPU +if it was turned off. If kOS is configured to start on the archive, +the file will not be copied locally automatically and booting will be delayed, +until connection to archive is established. You may get or set the name of the boot file using the :attr:`kOSProcessor:BOOTFILENAME` suffix. Important things to consider: diff --git a/src/kOS.Safe/Compilation/PseudoNull.cs b/src/kOS.Safe/Compilation/PseudoNull.cs index ef19c828b..5d89602e9 100644 --- a/src/kOS.Safe/Compilation/PseudoNull.cs +++ b/src/kOS.Safe/Compilation/PseudoNull.cs @@ -1,4 +1,4 @@ -using System; +using System; namespace kOS.Safe.Compilation { @@ -6,6 +6,8 @@ namespace kOS.Safe.Compilation // use this for a fake "type" to reperesent null: public class PseudoNull : IEquatable { + public static PseudoNull Instance { get; } = new PseudoNull(); + // all instances of PseudoNull should be considered identical: public override bool Equals(object o) { diff --git a/src/kOS.Safe/Execution/CPU.cs b/src/kOS.Safe/Execution/CPU.cs index 11f8aa681..bef797575 100644 --- a/src/kOS.Safe/Execution/CPU.cs +++ b/src/kOS.Safe/Execution/CPU.cs @@ -1,4 +1,4 @@ -using kOS.Safe.Binding; +using kOS.Safe.Binding; using kOS.Safe.Callback; using kOS.Safe.Compilation; using kOS.Safe.Encapsulation; @@ -16,33 +16,32 @@ namespace kOS.Safe.Execution { public class CPU : ICpu { - private readonly IStack stack; - private readonly VariableScope globalVariables; - - - private class YieldFinishedWithPriority + protected readonly IStack stack; + protected readonly VariableScope globalVariables; + + protected class YieldFinishedWithPriority { public YieldFinishedDetector detector; public InterruptPriority priority; } - private List yields; + protected List yields; - private double currentTime; - private readonly SafeSharedObjects shared; - private readonly List contexts; - private ProgramContext currentContext; - private VariableScope savedPointers; - private int instructionsPerUpdate; + protected double currentTime; + protected readonly SafeSharedObjects shared; + protected readonly List contexts; + protected ProgramContext currentContext; + protected VariableScope savedPointers; + protected int instructionsPerUpdate; public int InstructionsThisUpdate { get; private set; } // statistics - private double totalCompileTime; - private Stopwatch instructionWatch = new Stopwatch(); - private Stopwatch updateWatch = new Stopwatch(); - private Stopwatch executionWatch = new Stopwatch(); - private Stopwatch compileWatch = new Stopwatch(); - private readonly StringBuilder executeLog = new StringBuilder(); + protected double totalCompileTime; + protected Stopwatch instructionWatch = new Stopwatch(); + protected Stopwatch updateWatch = new Stopwatch(); + protected Stopwatch executionWatch = new Stopwatch(); + protected Stopwatch compileWatch = new Stopwatch(); + protected readonly StringBuilder executeLog = new StringBuilder(); private Dictionary executionStats = new Dictionary(); @@ -77,7 +76,7 @@ public int NextTriggerInstanceId /// The objects which have chosen to register themselves as IPopContextNotifyees /// to be told when popping a context (ending a program). /// - private List popContextNotifyees; + protected List popContextNotifyees; public CPU(SafeSharedObjects shared) { @@ -115,8 +114,12 @@ public void Boot() if (shared.Screen != null) { shared.Screen.ClearScreen(); - string bootMessage = string.Format("kOS Operating System\n" + "KerboScript v{0}\n(manual at {1})\n \n" + "Proceed.\n", - SafeHouse.Version, SafeHouse.DocumentationURL); + var bootMessage = string.Format( + "kOS Operating System\n" + + "KerboScript v{0}\n" + + "(manual at {1})\n", + SafeHouse.Version, + SafeHouse.DocumentationURL); List nags = Debug.GetPendingNags(); if (nags.Count > 0) { @@ -133,18 +136,19 @@ public void Boot() bootMessage += "##################################################\n"; shared.Processor.SetMode(Module.ProcessorModes.OFF); } + shared.Screen.Print(bootMessage); } - if (!shared.Processor.CheckCanBoot()) return; - VolumePath path = shared.Processor.BootFilePath; // Check to make sure the boot file name is valid, and then that the boot file exists. if (path == null) { SafeHouse.Logger.Log("Boot file name is empty, skipping boot script"); + + shared.Screen?.Print(" \n" + "Proceed.\n"); } - else + else if (shared.Processor.CheckCanBoot()) { // Boot is only called once right after turning the processor on, // the volume cannot yet have been changed from that set based on @@ -155,9 +159,23 @@ public void Boot() if (file == null) { SafeHouse.Logger.Log(string.Format("Boot file \"{0}\" is missing, skipping boot script", path)); + + shared.Screen?.Print(string.Format( + " \n" + + "Could not boot from {0}\n" + + "The file is missing\n" + + " \n" + + "Proceed.\n", + path)); } else { + shared.Screen?.Print(string.Format( + " \n" + + "Booting from {0}\n" + + " \n", + path)); + var bootContext = "program"; shared.ScriptHandler.ClearContext(bootContext); IProgramContext programContext = SwitchToProgramContext(); @@ -173,12 +191,35 @@ public void Boot() }; YieldProgram(YieldFinishedCompile.RunScript(new BootGlobalPath(bootCommand), 1, bootCommand, bootContext, options)); - } } + else //shared.Processor.CheckCanBoot() returned false + { + shared.Screen?.Print(string.Format( + " \n" + + "Waiting for connection to boot from {0}\n" + + " \n", + path)); + + var bootContext = "program"; + shared.ScriptHandler.ClearContext(bootContext); + IProgramContext programContext = SwitchToProgramContext(); + programContext.Silent = true; + + string bootCommand = string.Format("run \"{0}\".", path); + + var options = new CompilerOptions + { + LoadProgramsInSameAddressSpace = true, + FuncManager = shared.FunctionManager, + IsCalledFromRun = false + }; + + YieldProgram(YieldFinishedCompileBoot.RunScript(new BootGlobalPath(bootCommand), 1, bootCommand, bootContext, options)); + } } - private void PushInterpreterContext() + protected void PushInterpreterContext() { var interpreterContext = new ProgramContext(true); // initialize the context with an empty program @@ -186,7 +227,7 @@ private void PushInterpreterContext() PushContext(interpreterContext); } - private void PushContext(ProgramContext context) + protected void PushContext(ProgramContext context) { SafeHouse.Logger.Log("Pushing context staring with: " + context.GetCodeFragment(0).FirstOrDefault()); SaveAndClearPointers(); @@ -201,7 +242,7 @@ private void PushContext(ProgramContext context) } } - private void PopContext() + protected void PopContext() { SafeHouse.Logger.Log("Popping context " + contexts.Count); IsPoppingContext = true; @@ -267,7 +308,7 @@ public void RemovePopContextNotifyee(IPopContextNotifyee notifyee) popContextNotifyees.RemoveAll((item)=>(!item.IsAlive) || item.Target == notifyee); } - private void NotifyPopContextNotifyees(IProgramContext context) + protected void NotifyPopContextNotifyees(IProgramContext context) { // Notify them all: for (int i = 0; i < popContextNotifyees.Count; ++i) @@ -323,7 +364,7 @@ public object PopScopeStack(int howMany) return returnVal; } - private void PopFirstContext() + protected void PopFirstContext() { while (contexts.Count > 1) { @@ -400,7 +441,7 @@ public Opcode GetOpcodeAt(int instructionPtr) return currentContext.Program[instructionPtr]; } - private void SaveAndClearPointers() + protected void SaveAndClearPointers() { // Any global variable that ends in an asterisk (*) is a system pointer // that shouldn't be inherited by other program contexts. These sorts of @@ -422,7 +463,7 @@ private void SaveAndClearPointers() SafeHouse.Logger.Log(string.Format("Saving and removing {0} pointers", pointers.Count)); } - private void RestorePointers() + protected void RestorePointers() { // Pointer variables that were stashed by SaveAndClearPointers() get brought // back again by this method when returning to the previous programming @@ -522,7 +563,7 @@ public void YieldProgram(YieldFinishedDetector yieldTracker) yieldTracker.Begin(shared); } - private bool IsYielding() + protected bool IsYielding() { int numStillBlocking = 0; @@ -546,8 +587,8 @@ private bool IsYielding() } return (numStillBlocking > 0); } - - private void AbortAllYields() + + protected void AbortAllYields() { yields.Clear(); } @@ -615,7 +656,7 @@ public List GetCallTrace() /// /// /// - private Variable GetOrCreateVariable(string identifier) + protected Variable GetOrCreateVariable(string identifier) { Variable variable = GetVariable(identifier, false, true); if (variable == null) @@ -674,7 +715,7 @@ public string DumpStack() return stack.Dump(); } - private VariableScope GetCurrentScope() + protected VariableScope GetCurrentScope() { VariableScope currentScope = stack.GetCurrentScope(); if (currentScope == null) @@ -710,7 +751,7 @@ public List GetTriggerCallContexts(TriggerInfo trigger) /// Is it acceptable for the variable to /// not exist, in which case a null will be returned as the value. /// the value that was found - private Variable GetVariable(string identifier, bool barewordOkay = false, bool failOkay = false) + protected Variable GetVariable(string identifier, bool barewordOkay = false, bool failOkay = false) { identifier = identifier.ToLower(); Variable value = GetCurrentScope().GetNested(identifier); @@ -1265,7 +1306,7 @@ public void CancelCalledTriggers(TriggerInfo trigger) } } - public void KOSFixedUpdate(double deltaTime) + public virtual void KOSFixedUpdate(double deltaTime) { bool showStatistics = SafeHouse.Config.ShowStatistics; @@ -1339,7 +1380,7 @@ public void KOSFixedUpdate(double deltaTime) statBlock.EndOneUpdate(); } - private void PreUpdateBindings() + protected void PreUpdateBindings() { if (shared.BindingMgr != null) { @@ -1347,7 +1388,7 @@ private void PreUpdateBindings() } } - private void PostUpdateBindings() + protected void PostUpdateBindings() { if (shared.BindingMgr != null) { @@ -1355,7 +1396,7 @@ private void PostUpdateBindings() } } - private void ProcessTriggers() + protected void ProcessTriggers() { if (currentContext.ActiveTriggerCount() <= 0) return; int oldCount = currentContext.Program.Count; @@ -1377,7 +1418,7 @@ private void ProcessTriggers() // then leave it in the list to be added later once we return back to a lower priority that allows it.) if (currentContext.ContainsTrigger(trigger) && (trigger.Priority == InterruptPriority.NoChange || trigger.Priority > CurrentPriority)) { - if (trigger is NoDelegate) + if (trigger.EntryPoint < 0 /* NoDelegate */) { // Don't bother calling it. Just declare it to be "done" with its default value. trigger.FinishCallback(new ScalarIntValue(0)); @@ -1432,7 +1473,7 @@ private void ProcessTriggers() currentContext.InstructionPointer = currentInstructionPointer; } - private void ContinueExecution(bool doProfiling) + protected virtual void ContinueExecution(bool doProfiling) { var executeNext = true; int howManyNormalPriority = 0; @@ -1492,7 +1533,7 @@ private void ContinueExecution(bool doProfiling) SafeHouse.Logger.Log(executeLog.ToString()); } - private bool ExecuteInstruction(ProgramContext context, bool doProfiling) + protected virtual bool ExecuteInstruction(ProgramContext context, bool doProfiling) { Opcode opcode = context.Program[context.InstructionPointer]; if (SafeHouse.Config.DebugEachOpcode) @@ -1555,7 +1596,7 @@ private bool ExecuteInstruction(ProgramContext context, bool doProfiling) } } - private void SkipCurrentInstructionId() + protected void SkipCurrentInstructionId() { if (currentContext.InstructionPointer >= (currentContext.Program.Count - 1)) return; @@ -1664,21 +1705,27 @@ public void ResetStatistics() totalCompileTime = 0D; executionStats.Clear(); } - - private void PrintStatistics() + + protected void PrintStatistics() { shared.Screen.Print(StatisticsDump(false)); ResetStatistics(); } - - private void CalculateProfileResult() + + protected void CalculateProfileResult() { ProfileResult = currentContext.GetCodeFragment(0, currentContext.Program.Count - 1, true); // Prepend a header string consisting of the block of summary text: ProfileResult.Insert(0, StatisticsDump(true)); } + ~CPU() => Dispose(false); public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + protected virtual void Dispose(bool disposing) { while (contexts.Count > 0) { @@ -1699,9 +1746,9 @@ public void StopCompileStopwatch() compileWatch.Stop(); } - private class BootGlobalPath : InternalPath + protected class BootGlobalPath : InternalPath { - private string command; + protected string command; public BootGlobalPath(string command) : base() { @@ -1719,4 +1766,4 @@ public override string ToString() } } } -} \ No newline at end of file +} diff --git a/src/kOS.Safe/Execution/IStack.cs b/src/kOS.Safe/Execution/IStack.cs index f349030d5..c85e17f4a 100644 --- a/src/kOS.Safe/Execution/IStack.cs +++ b/src/kOS.Safe/Execution/IStack.cs @@ -1,10 +1,35 @@ -using System; +using System; using System.Collections.Generic; namespace kOS.Safe.Execution { public interface IStack { + /// + /// Current size of argument stack + /// + int ArgumentCount { get; } + /// + /// Current size of scope stack + /// + int ScopeCount { get; } + /// + /// All arguments on the stack (from top to bottom) + /// + IEnumerable Arguments { get; } + /// + /// All arguments on the stack from bottom to top + /// + IEnumerable ArgumentsFromBottom { get; } + /// + /// All scopes on the stack (from top to bottom) + /// + IEnumerable Scopes { get; } + /// + /// All scopes on the stack from bottom to top + /// + IEnumerable ScopesFromBottom { get; } + void PushArgument(object item); object PopArgument(); object PeekArgument(int digDepth); diff --git a/src/kOS.Safe/Execution/Stack.cs b/src/kOS.Safe/Execution/Stack.cs index f8c4176fb..0de459152 100644 --- a/src/kOS.Safe/Execution/Stack.cs +++ b/src/kOS.Safe/Execution/Stack.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Text; using kOS.Safe.Utilities; @@ -42,6 +42,59 @@ public class Stack : IStack private int triggerContextCount = 0; private int delayingTriggerContextCount = 0; + /// + /// Current size of argument stack + /// + public int ArgumentCount => argumentCount; + /// + /// Current size of scope stack + /// + public int ScopeCount => scopeCount; + /// + /// All arguments on the stack (from top to bottom) + /// + public IEnumerable Arguments + { + get + { + for (int i = argumentCount; i > 0;) + yield return argumentStack[--i]; + } + } + /// + /// All arguments on the stack from bottom to top + /// + public IEnumerable ArgumentsFromBottom + { + get + { + for (int i = 0; i < argumentCount;) + yield return argumentStack[i++]; + } + } + /// + /// All scopes on the stack (from top to bottom) + /// + public IEnumerable Scopes + { + get + { + for (int i = scopeCount; i > 0;) + yield return scopeStack[--i]; + } + } + /// + /// All scopes on the stack from bottom to top + /// + public IEnumerable ScopesFromBottom + { + get + { + for (int i = 0; i < scopeCount;) + yield return scopeStack[i++]; + } + } + /// /// Push to the argument stack. /// @@ -475,4 +528,4 @@ public bool HasDelayingTriggerContexts() return delayingTriggerContextCount > 0; } } -} \ No newline at end of file +} diff --git a/src/kOS.Safe/Execution/YieldFinishedCompile.cs b/src/kOS.Safe/Execution/YieldFinishedCompile.cs index adaf00314..6297cf680 100644 --- a/src/kOS.Safe/Execution/YieldFinishedCompile.cs +++ b/src/kOS.Safe/Execution/YieldFinishedCompile.cs @@ -1,4 +1,4 @@ -using kOS.Safe.Compilation; +using kOS.Safe.Compilation; using kOS.Safe.Encapsulation; using kOS.Safe.Persistence; using System.Collections.Generic; @@ -6,9 +6,9 @@ namespace kOS.Safe.Execution { - public class YieldFinishedCompile : YiedFinishedThreadedDetector + public class YieldFinishedCompile : YieldFinishedThreadedDetector { - private enum CompileMode + protected enum CompileMode { RUN = 0, LOAD = 1, @@ -28,9 +28,9 @@ private enum CompileMode private IProgramContext programContext; - private YieldFinishedCompile(GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions) + protected YieldFinishedCompile(CompileMode mode, GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions) { - compileMode = CompileMode.RUN; + compileMode = mode; path = scriptPath; startLineNum = lineNumber; content = fileContent; @@ -38,19 +38,20 @@ private YieldFinishedCompile(GlobalPath scriptPath, int lineNumber, string fileC options = compilerOptions; } - public override void ThreadInitialize(SafeSharedObjects shared) + protected override bool ThreadInitialize() { if (compileMode != CompileMode.FILE) programContext = shared.Cpu.SwitchToProgramContext(); // only switch the context if executing codeParts = new List(); + return true; } - public override void ThreadExecute() + protected override void ThreadExecute() { codeParts = shared.ScriptHandler.Compile(path, startLineNum, content, contextId, options); } - public override void ThreadFinish() + protected override void ThreadFinish() { switch (compileMode) { @@ -76,27 +77,17 @@ public override void ThreadFinish() shared.Cpu.StopCompileStopwatch(); } - public static YieldFinishedCompile RunScript(GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions) - { - var ret = new YieldFinishedCompile(scriptPath, lineNumber, fileContent, contextIdentifier, compilerOptions); - ret.compileMode = CompileMode.RUN; - return ret; - } + public static YieldFinishedCompile RunScript(GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions) => + new YieldFinishedCompile(CompileMode.RUN, scriptPath, lineNumber, fileContent, contextIdentifier, compilerOptions); - public static YieldFinishedCompile LoadScript(GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions) - { - var ret = new YieldFinishedCompile(scriptPath, lineNumber, fileContent, contextIdentifier, compilerOptions); - ret.compileMode = CompileMode.LOAD; - return ret; - } + public static YieldFinishedCompile LoadScript(GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions) => + new YieldFinishedCompile(CompileMode.LOAD, scriptPath, lineNumber, fileContent, contextIdentifier, compilerOptions); - public static YieldFinishedCompile CompileScriptToFile(GlobalPath scriptPath, int lineNumber, string fileContent, CompilerOptions compilerOptions, Volume storageVolume, GlobalPath storagePath) - { - var ret = new YieldFinishedCompile(scriptPath, lineNumber, fileContent, string.Empty, compilerOptions); - ret.compileMode = CompileMode.FILE; - ret.volume = storageVolume; - ret.outPath = storagePath; - return ret; - } + public static YieldFinishedCompile CompileScriptToFile(GlobalPath scriptPath, int lineNumber, string fileContent, CompilerOptions compilerOptions, Volume storageVolume, GlobalPath storagePath) => + new YieldFinishedCompile(CompileMode.FILE, scriptPath, lineNumber, fileContent, string.Empty, compilerOptions) + { + volume = storageVolume, + outPath = storagePath + }; } } \ No newline at end of file diff --git a/src/kOS.Safe/Execution/YieldFinishedCompileBoot.cs b/src/kOS.Safe/Execution/YieldFinishedCompileBoot.cs new file mode 100644 index 000000000..b781c3dcd --- /dev/null +++ b/src/kOS.Safe/Execution/YieldFinishedCompileBoot.cs @@ -0,0 +1,27 @@ +using kOS.Safe.Compilation; +using kOS.Safe.Encapsulation; +using kOS.Safe.Persistence; +using System.Collections.Generic; +using System.Threading; + +namespace kOS.Safe.Execution +{ + public class YieldFinishedCompileBoot : YieldFinishedCompile + { + protected YieldFinishedCompileBoot(CompileMode mode, GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions) : + base(mode, scriptPath, lineNumber, fileContent, contextIdentifier, compilerOptions) + { } + + protected override bool ThreadInitialize() + { + if (!shared.Processor.CheckCanBoot()) + return false; + shared.Screen?.Print("Booting ...\n"); + base.ThreadInitialize(); + return true; + } + + public static new YieldFinishedCompileBoot RunScript(GlobalPath scriptPath, int lineNumber, string fileContent, string contextIdentifier, CompilerOptions compilerOptions) => + new YieldFinishedCompileBoot(CompileMode.RUN, scriptPath, lineNumber, fileContent, contextIdentifier, compilerOptions); + } +} diff --git a/src/kOS.Safe/Execution/YiedFinishedThreadedDetector.cs b/src/kOS.Safe/Execution/YieldFinishedThreadedDetector.cs similarity index 75% rename from src/kOS.Safe/Execution/YiedFinishedThreadedDetector.cs rename to src/kOS.Safe/Execution/YieldFinishedThreadedDetector.cs index eca80cd3f..4b9c10448 100644 --- a/src/kOS.Safe/Execution/YiedFinishedThreadedDetector.cs +++ b/src/kOS.Safe/Execution/YieldFinishedThreadedDetector.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +6,7 @@ namespace kOS.Safe.Execution { - public abstract class YiedFinishedThreadedDetector : YieldFinishedDetector + public abstract class YieldFinishedThreadedDetector : YieldFinishedDetector { private ManualResetEvent childThreadEvent; private Thread childThread; @@ -23,15 +23,23 @@ public override void Begin(SafeSharedObjects sharedObj) childThreadEvent = new ManualResetEvent(false); - ThreadInitialize(sharedObj); + TryStartThread(); + } - childThread = new Thread(DoThread); - childThread.IsBackground = true; - childThread.Start(); + private void TryStartThread() + { + if (ThreadInitialize()) + { + childThread = new Thread(DoThread); + childThread.IsBackground = true; + childThread.Start(); + } } public override bool IsFinished() { + // Thread may not be started yet, but this distinguishes between finished and not-started + // in case IsFinished() gets called multiple times even after it returns true if (childThreadEvent.WaitOne(0)) { childThread.Join(); @@ -46,6 +54,9 @@ public override bool IsFinished() childException = ex; } } + // Remove the reference now, before we potentially (re)throw the exception + childThread = null; + // Note this is *deliberately* NOT an "else" of the above "if" even though // it looks like it should be. That is because the above IF clause can actually // alter this flag and if it does so it needs to fall through to here and do this. @@ -59,9 +70,12 @@ public override bool IsFinished() shared.Cpu.BreakExecution(false); throw childException; } - childThread = null; return true; } + // Try starting the thread now, if not started yet + if (childThread == null) + TryStartThread(); + return false; } @@ -82,8 +96,8 @@ private void DoThread() /// This method is executed before starting the child thread. It is called from the main thread and is not required /// to be thread safe with respect to KSP. /// - /// - public abstract void ThreadInitialize(SafeSharedObjects shared); + /// True if ready to start the thread (false if waiting for some condition, e.g. Processor.CheckCanBoot) + protected abstract bool ThreadInitialize(); /// /// @@ -96,12 +110,12 @@ private void DoThread() /// /// /// - public abstract void ThreadExecute(); + protected abstract void ThreadExecute(); /// /// This method is executed after the child thread is finished, when the CPU checks IsFinished. It is called from /// the main thread and is not required to be thread safe with respect to KSP. /// - public abstract void ThreadFinish(); + protected abstract void ThreadFinish(); } } diff --git a/src/kOS.Safe/kOS.Safe.csproj b/src/kOS.Safe/kOS.Safe.csproj index 1faed2dcd..bb4831140 100644 --- a/src/kOS.Safe/kOS.Safe.csproj +++ b/src/kOS.Safe/kOS.Safe.csproj @@ -181,8 +181,9 @@ - + + @@ -318,4 +319,4 @@ --> - + \ No newline at end of file