mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-28 23:19:03 -04:00
Add thread management and stepping to runtime
This commit is contained in:
parent
dbfb3356c6
commit
92dab97b84
5 changed files with 610 additions and 376 deletions
src/engine
|
@ -1,11 +1,17 @@
|
|||
var Timer = require('../util/Timer');
|
||||
|
||||
function Sequencer () {
|
||||
function Sequencer (runtime) {
|
||||
/**
|
||||
* A utility timer for timing thread sequencing.
|
||||
* @type {!Timer}
|
||||
*/
|
||||
this.timer = new Timer();
|
||||
|
||||
/**
|
||||
* Reference to the runtime owning this sequencer.
|
||||
* @type {!Runtime}
|
||||
*/
|
||||
this.runtime = runtime;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,11 +24,13 @@ Sequencer.WORK_TIME = 1000 / 60;
|
|||
|
||||
/**
|
||||
* Step through all threads in `this.threads`, running them in order.
|
||||
* @return {Array.<Thread>} New threads after this round of execution.
|
||||
* @return {Array.<Thread>} All threads which have finished in this iteration.
|
||||
*/
|
||||
Sequencer.prototype.stepThreads = function (threads) {
|
||||
// Start counting toward WORK_TIME
|
||||
this.timer.start();
|
||||
// List of threads which have been killed by this step.
|
||||
var inactiveThreads = [];
|
||||
// While there are still threads to run and we are within WORK_TIME,
|
||||
// continue executing threads.
|
||||
while (threads.length > 0 &&
|
||||
|
@ -35,12 +43,14 @@ Sequencer.prototype.stepThreads = function (threads) {
|
|||
this.stepThread(activeThread);
|
||||
if (activeThread.nextBlock !== null) {
|
||||
newThreads.push(activeThread);
|
||||
} else {
|
||||
inactiveThreads.push(activeThread);
|
||||
}
|
||||
}
|
||||
// Effectively filters out threads that have stopped.
|
||||
threads = newThreads;
|
||||
}
|
||||
return threads;
|
||||
return inactiveThreads;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -48,7 +58,10 @@ Sequencer.prototype.stepThreads = function (threads) {
|
|||
* @param {!Thread} thread Thread object to step
|
||||
*/
|
||||
Sequencer.prototype.stepThread = function (thread) {
|
||||
// @todo
|
||||
// @todo Actually run the blocks
|
||||
// Currently goes to the next block in the sequence.
|
||||
var nextBlock = this.runtime._getNextBlock(thread.nextBlock);
|
||||
thread.nextBlock = nextBlock;
|
||||
};
|
||||
|
||||
module.exports = Sequencer;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue