Skip to content

Commit

Permalink
implemented main loop
Browse files Browse the repository at this point in the history
  • Loading branch information
wanderer committed Jul 6, 2017
1 parent 016c6df commit f25a4c6
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,35 @@ module.exports = class Kernel {
*/
queue (portName, message) {
this.ports.queue(portName, message)
if (this.containerState !== 'running') {
this.containerState = 'running'
return this._runNextMessage()
}
return this._startMessageLoop()
}

initialize (message) {
this.containerState = 'running'
return this.run(message, 'initialize')
async initialize (message) {
await this.run(message, 'initialize')
return this._startMessageLoop()
}

// waits for the next message
async _runNextMessage () {
// check if the ports are saturated, if so we don't have to wait on the
// scheduler
const message = await this.ports.getNextMessage()

if (message) {
message.fromPort.messages.shift()
// if the message we recived had more ticks then we currently have the
// update it
if (message._fromTicks > this.ticks) {
this.ticks = message._fromTicks
this.hypervisor.scheduler.update(this)
async _startMessageLoop () {
// this ensure we only every have one loop running at a time
if (this.containerState !== 'running') {
this.containerState = 'running'

while (1) {
let message = await this.ports.getNextMessage()
if (!message) break

message.fromPort.messages.shift()
// if the message we recived had more ticks then we currently have the
// update it
if (message._fromTicks > this.ticks) {
this.ticks = message._fromTicks
this.hypervisor.scheduler.update(this)
}
// run the next message
await this.run(message)
}
// run the next message
return this.run(message)
} else {
// if no more messages then shut down
// no more messages; shut down
this.hypervisor.scheduler.done(this.id)
}
}
Expand Down Expand Up @@ -100,11 +100,9 @@ module.exports = class Kernel {
this.send(responsePort, new Message({
data: result
}))
this.ports._unboundPorts.add(responsePort)
}

this.ports.clearUnboundedPorts()
return this._runNextMessage()
}

getResponsePort (message) {
Expand Down

0 comments on commit f25a4c6

Please sign in to comment.