Move threads list to the Runtime

This commit is contained in:
Tim Mickel 2016-04-26 15:51:14 -04:00
parent b6186a44f3
commit dbfb3356c6
2 changed files with 26 additions and 15 deletions

View file

@ -10,11 +10,27 @@ function Runtime () {
EventEmitter.call(this); EventEmitter.call(this);
// State for the runtime // State for the runtime
/** @type {Object.<string, Object>} */ /**
* All blocks in the workspace.
* Keys are block IDs, values are metadata about the block.
* @type {Object.<string, Object>}
*/
this.blocks = {}; this.blocks = {};
/** @type {Array.<String>} */
/**
* All stacks in the workspace.
* A list of block IDs that represent stacks (first block in stack).
* @type {Array.<String>}
*/
this.stacks = []; this.stacks = [];
/**
* A list of threads that are currently running in the VM.
* Threads are added when execution starts and pruned when execution ends.
* @type {Array.<Thread>}
*/
this.threads = [];
/** @type {!Sequencer} */ /** @type {!Sequencer} */
this.sequencer = new Sequencer(); this.sequencer = new Sequencer();
} }

View file

@ -1,13 +1,6 @@
var Timer = require('../util/Timer'); var Timer = require('../util/Timer');
function Sequencer () { function Sequencer () {
/**
* A list of threads that are currently running in the VM.
* Threads are added when execution starts and pruned when execution ends.
* @type {Array.<Thread>}
*/
this.threads = [];
/** /**
* A utility timer for timing thread sequencing. * A utility timer for timing thread sequencing.
* @type {!Timer} * @type {!Timer}
@ -25,34 +18,36 @@ Sequencer.WORK_TIME = 1000 / 60;
/** /**
* Step through all threads in `this.threads`, running them in order. * Step through all threads in `this.threads`, running them in order.
* @return {Array.<Thread>} New threads after this round of execution.
*/ */
Sequencer.prototype.stepThreads = function () { Sequencer.prototype.stepThreads = function (threads) {
// Start counting toward WORK_TIME // Start counting toward WORK_TIME
this.timer.start(); this.timer.start();
// While there are still threads to run and we are within WORK_TIME, // While there are still threads to run and we are within WORK_TIME,
// continue executing threads. // continue executing threads.
while (this.threads.length > 0 && while (threads.length > 0 &&
this.timer.timeElapsed() < Sequencer.WORK_TIME) { this.timer.timeElapsed() < Sequencer.WORK_TIME) {
// New threads at the end of the iteration. // New threads at the end of the iteration.
var newThreads = []; var newThreads = [];
// Attempt to run each thread one time // Attempt to run each thread one time
for (var i = 0; i < this.threads.length; i++) { for (var i = 0; i < threads.length; i++) {
var activeThread = this.threads[i]; var activeThread = threads[i];
this.stepThread(activeThread); this.stepThread(activeThread);
if (activeThread.nextBlock !== null) { if (activeThread.nextBlock !== null) {
newThreads.push(activeThread); newThreads.push(activeThread);
} }
} }
// Effectively filters out threads that have stopped. // Effectively filters out threads that have stopped.
this.threads = newThreads; threads = newThreads;
} }
return threads;
}; };
/** /**
* Step the requested thread * Step the requested thread
* @param {!Thread} thread Thread object to step * @param {!Thread} thread Thread object to step
*/ */
Sequencer.protoype.stepThread = function (thread) { Sequencer.prototype.stepThread = function (thread) {
// @todo // @todo
}; };