Add blocking yield mode

This commit is contained in:
Tim Mickel 2016-06-20 16:44:53 -04:00
parent c63747e61b
commit d44b806b4f
4 changed files with 44 additions and 2 deletions

View file

@ -83,7 +83,7 @@ Scratch3OperatorsBlocks.prototype.random = function (args, util) {
// As a demo, this implementation of random returns after 1 second of yield.
// @todo Match Scratch 2.0 implementation with int-truncation.
// See: http://bit.ly/1Qc0GzC
util.yield();
util.yieldAndBlock();
setTimeout(function() {
var randomValue = (Math.random() * (args.TO - args.FROM)) + args.FROM;
util.report(randomValue);

View file

@ -74,6 +74,7 @@ var execute = function (sequencer, thread) {
var primitiveReportedValue = null;
primitiveReportedValue = blockFunction(argValues, {
yield: thread.yield.bind(thread),
yieldAndBlock: thread.yieldAndBlock.bind(thread),
done: function() {
sequencer.proceedThread(thread);
},

View file

@ -32,6 +32,13 @@ Sequencer.WORK_TIME = 10;
Sequencer.prototype.stepThreads = function (threads) {
// Start counting toward WORK_TIME
this.timer.start();
// Check for a blocking thread.
var blockingThread = this.getBlockingThread_(threads);
if (blockingThread) {
// Attempt to resolve any timeouts, but otherwise stop stepping.
blockingThread.resolveTimeouts();
return [];
}
// List of threads which have been killed by this step.
var inactiveThreads = [];
// If all of the threads are yielding, we should yield.
@ -63,6 +70,10 @@ Sequencer.prototype.stepThreads = function (threads) {
activeThread.status = Thread.STATUS_RUNNING;
// @todo Deal with the return value
}
// Has the thread gone into "blocking" mode? If so, stop stepping.
if (activeThread.status === Thread.STATUS_YIELD_BLOCK) {
return inactiveThreads;
}
if (activeThread.stack.length === 0 &&
activeThread.status === Thread.STATUS_DONE) {
// Finished with this thread - tell runtime to clean it up.
@ -78,6 +89,21 @@ Sequencer.prototype.stepThreads = function (threads) {
return inactiveThreads;
};
/**
* Return the thread blocking all other threads, if one exists.
* If not, return false.
* @param {Array.<Thread>} threads List of which threads to check.
* @return {?Thread} The blocking thread if one exists.
*/
Sequencer.prototype.getBlockingThread_ = function (threads) {
for (var i = 0; i < threads.length; i++) {
if (threads[i].status === Thread.STATUS_YIELD_BLOCK) {
return threads[i];
}
}
return false;
};
/**
* Step the requested thread
* @param {!Thread} thread Thread object to step

View file

@ -52,13 +52,21 @@ Thread.STATUS_RUNNING = 0;
*/
Thread.STATUS_YIELD = 1;
/**
* Thread status for a yielded thread that should block all other threads.
* This is desirable when an asynchronous capability should appear to take
* a synchronous amount of time (e.g., color touching color).
* @const
*/
Thread.STATUS_YIELD_BLOCK = 2;
/**
* Thread status for a finished/done thread.
* Thread is moved to this state when the interpreter
* can proceed with execution.
* @const
*/
Thread.STATUS_DONE = 2;
Thread.STATUS_DONE = 3;
/**
* Push stack and update stack frames appropriately.
@ -131,6 +139,13 @@ Thread.prototype.yield = function () {
this.status = Thread.STATUS_YIELD;
};
/**
* Yields the thread and blocks other threads until unyielded.
*/
Thread.prototype.yieldAndBlock = function () {
this.status = Thread.STATUS_YIELD_BLOCK;
};
/**
* Add an execution-synced timeouts for this thread.
* See also: util/yieldtimers.js:timeout