mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-22 22:12:28 -05:00
Add blocking yield mode
This commit is contained in:
parent
c63747e61b
commit
d44b806b4f
4 changed files with 44 additions and 2 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
},
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue