Skip to content

Commit 7c96988

Browse files
committed
True OS sleep on yield() and wait()
1 parent e85ec61 commit 7c96988

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

MiniScript-cpp/src/MiniScript/MiniscriptIntrinsics.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <cmath>
1515
#include <ctime>
1616
#include <algorithm>
17+
#include <chrono>
18+
#include <thread>
1719

1820
namespace MiniScript {
1921

@@ -855,16 +857,10 @@ namespace MiniScript {
855857
}
856858

857859
static IntrinsicResult intrinsic_wait(Context *context, IntrinsicResult partialResult) {
858-
double now = context->vm->RunTime();
859-
if (partialResult.Done()) {
860-
// Just starting our wait; calculate end time and return as partial result
861-
double interval = context->GetVar("seconds").DoubleValue();
862-
return IntrinsicResult(Value(now + interval), false);
863-
} else {
864-
// Continue until current time exceeds the time in the partial result
865-
if (now > partialResult.Result().DoubleValue()) return IntrinsicResult::Null;
866-
return partialResult;
867-
}
860+
double seconds = context->GetVar("seconds").DoubleValue();
861+
long nanoseconds = seconds * 1000000000;
862+
std::this_thread::sleep_for(std::chrono::nanoseconds(nanoseconds));
863+
return IntrinsicResult::Null;
868864
}
869865

870866
static IntrinsicResult intrinsic_yield(Context *context, IntrinsicResult partialResult) {

MiniScript-cpp/src/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <iostream>
1010
#include <fstream>
1111
#include <string>
12+
#include <chrono>
13+
#include <thread>
1214
#include "MiniScript/SimpleString.h"
1315
#include "MiniScript/UnicodeUtil.h"
1416
#include "MiniScript/UnitTest.h"
@@ -22,6 +24,9 @@
2224
#include "ShellIntrinsics.h"
2325
#include "DateTimeUtils.h" // TEMP for initial testing
2426

27+
// YIELD_NANOSECONDS: How many nano-seconds to sleep when yielding.
28+
#define YIELD_NANOSECONDS 10000000
29+
2530
using namespace MiniScript;
2631

2732
bool printHeaderInfo = true;
@@ -127,6 +132,7 @@ static int DoCommand(Interpreter &interp, String cmd) {
127132
while (!interp.Done()) {
128133
try {
129134
interp.RunUntilDone();
135+
std::this_thread::sleep_for(std::chrono::nanoseconds(YIELD_NANOSECONDS));
130136
} catch (MiniscriptException& mse) {
131137
std::cerr << "Runtime Exception: " << mse.message << std::endl;
132138
interp.vm->Stop();

0 commit comments

Comments
 (0)