Commit f25a4c630940b064038e561bbafd852d929317c8
implemented main loop
wanderer committed on 7/6/2017, 2:24:04 AMParent: 016c6df49758e5db4d0f3caaf955ad714e137afc
Files changed
kernel.js | changed |
kernel.js | ||
---|---|---|
@@ -34,37 +34,37 @@ | ||
34 | 34 | * @param {object} message |
35 | 35 | */ |
36 | 36 | queue (portName, message) { |
37 | 37 | this.ports.queue(portName, message) |
38 | - if (this.containerState !== 'running') { | |
39 | - this.containerState = 'running' | |
40 | - return this._runNextMessage() | |
41 | - } | |
38 | + return this._startMessageLoop() | |
42 | 39 | } |
43 | 40 | |
44 | - initialize (message) { | |
45 | - this.containerState = 'running' | |
46 | - return this.run(message, 'initialize') | |
41 | + async initialize (message) { | |
42 | + await this.run(message, 'initialize') | |
43 | + return this._startMessageLoop() | |
47 | 44 | } |
48 | 45 | |
49 | 46 | // waits for the next message |
50 | - async _runNextMessage () { | |
51 | - // check if the ports are saturated, if so we don't have to wait on the | |
52 | - // scheduler | |
53 | - const message = await this.ports.getNextMessage() | |
47 | + async _startMessageLoop () { | |
48 | + // this ensure we only every have one loop running at a time | |
49 | + if (this.containerState !== 'running') { | |
50 | + this.containerState = 'running' | |
54 | 51 | |
55 | - if (message) { | |
56 | - message.fromPort.messages.shift() | |
57 | - // if the message we recived had more ticks then we currently have the | |
58 | - // update it | |
59 | - if (message._fromTicks > this.ticks) { | |
60 | - this.ticks = message._fromTicks | |
61 | - this.hypervisor.scheduler.update(this) | |
52 | + while (1) { | |
53 | + let message = await this.ports.getNextMessage() | |
54 | + if (!message) break | |
55 | + | |
56 | + message.fromPort.messages.shift() | |
57 | + // if the message we recived had more ticks then we currently have the | |
58 | + // update it | |
59 | + if (message._fromTicks > this.ticks) { | |
60 | + this.ticks = message._fromTicks | |
61 | + this.hypervisor.scheduler.update(this) | |
62 | + } | |
63 | + // run the next message | |
64 | + await this.run(message) | |
62 | 65 | } |
63 | - // run the next message | |
64 | - return this.run(message) | |
65 | - } else { | |
66 | - // if no more messages then shut down | |
66 | + // no more messages; shut down | |
67 | 67 | this.hypervisor.scheduler.done(this.id) |
68 | 68 | } |
69 | 69 | } |
70 | 70 | |
@@ -99,13 +99,11 @@ | ||
99 | 99 | if (responsePort) { |
100 | 100 | this.send(responsePort, new Message({ |
101 | 101 | data: result |
102 | 102 | })) |
103 | - this.ports._unboundPorts.add(responsePort) | |
104 | 103 | } |
105 | 104 | |
106 | 105 | this.ports.clearUnboundedPorts() |
107 | - return this._runNextMessage() | |
108 | 106 | } |
109 | 107 | |
110 | 108 | getResponsePort (message) { |
111 | 109 | if (message.responsePort) { |
Built with git-ssb-web