From 7c96988709dad361c87bb651e7dfb7b2293847a8 Mon Sep 17 00:00:00 2001 From: Marc Gurevitx Date: Sat, 27 Jul 2024 22:38:47 +0300 Subject: [PATCH 1/2] True OS sleep on yield() and wait() --- .../src/MiniScript/MiniscriptIntrinsics.cpp | 16 ++++++---------- MiniScript-cpp/src/main.cpp | 6 ++++++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.cpp b/MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.cpp index 5bc7b48..220803f 100644 --- a/MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.cpp +++ b/MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.cpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include namespace MiniScript { @@ -855,16 +857,10 @@ namespace MiniScript { } static IntrinsicResult intrinsic_wait(Context *context, IntrinsicResult partialResult) { - double now = context->vm->RunTime(); - if (partialResult.Done()) { - // Just starting our wait; calculate end time and return as partial result - double interval = context->GetVar("seconds").DoubleValue(); - return IntrinsicResult(Value(now + interval), false); - } else { - // Continue until current time exceeds the time in the partial result - if (now > partialResult.Result().DoubleValue()) return IntrinsicResult::Null; - return partialResult; - } + double seconds = context->GetVar("seconds").DoubleValue(); + long nanoseconds = seconds * 1000000000; + std::this_thread::sleep_for(std::chrono::nanoseconds(nanoseconds)); + return IntrinsicResult::Null; } static IntrinsicResult intrinsic_yield(Context *context, IntrinsicResult partialResult) { diff --git a/MiniScript-cpp/src/main.cpp b/MiniScript-cpp/src/main.cpp index 2a27d00..5467c90 100644 --- a/MiniScript-cpp/src/main.cpp +++ b/MiniScript-cpp/src/main.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include "MiniScript/SimpleString.h" #include "MiniScript/UnicodeUtil.h" #include "MiniScript/UnitTest.h" @@ -22,6 +24,9 @@ #include "ShellIntrinsics.h" #include "DateTimeUtils.h" // TEMP for initial testing +// YIELD_NANOSECONDS: How many nano-seconds to sleep when yielding. +#define YIELD_NANOSECONDS 10000000 + using namespace MiniScript; bool printHeaderInfo = true; @@ -127,6 +132,7 @@ static int DoCommand(Interpreter &interp, String cmd) { while (!interp.Done()) { try { interp.RunUntilDone(); + std::this_thread::sleep_for(std::chrono::nanoseconds(YIELD_NANOSECONDS)); } catch (MiniscriptException& mse) { std::cerr << "Runtime Exception: " << mse.message << std::endl; interp.vm->Stop(); From f2b2e4257bc2ad070b2be537978d3d9ac3c90a9f Mon Sep 17 00:00:00 2001 From: Marc Gurevitx Date: Mon, 29 Jul 2024 00:31:12 +0300 Subject: [PATCH 2/2] Revert wait() to busy --- .../src/MiniScript/MiniscriptIntrinsics.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.cpp b/MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.cpp index 220803f..5bc7b48 100644 --- a/MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.cpp +++ b/MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.cpp @@ -14,8 +14,6 @@ #include #include #include -#include -#include namespace MiniScript { @@ -857,10 +855,16 @@ namespace MiniScript { } static IntrinsicResult intrinsic_wait(Context *context, IntrinsicResult partialResult) { - double seconds = context->GetVar("seconds").DoubleValue(); - long nanoseconds = seconds * 1000000000; - std::this_thread::sleep_for(std::chrono::nanoseconds(nanoseconds)); - return IntrinsicResult::Null; + double now = context->vm->RunTime(); + if (partialResult.Done()) { + // Just starting our wait; calculate end time and return as partial result + double interval = context->GetVar("seconds").DoubleValue(); + return IntrinsicResult(Value(now + interval), false); + } else { + // Continue until current time exceeds the time in the partial result + if (now > partialResult.Result().DoubleValue()) return IntrinsicResult::Null; + return partialResult; + } } static IntrinsicResult intrinsic_yield(Context *context, IntrinsicResult partialResult) {