git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit c331410c6a6f9cd1457d0321b06291624febe799

Merge pull request #125 from primea/loop

Loop
wanderer authored on 7/6/2017, 8:16:25 PM
GitHub committed on 7/6/2017, 8:16:25 PM
Parent: 016c6df49758e5db4d0f3caaf955ad714e137afc
Parent: 381ecaab16e8fc78457b956f74843ae172ca686e

Files changed

index.jschanged
kernel.jschanged
package.jsonchanged
tests/index.jschanged
index.jsView
@@ -41,8 +41,19 @@
4141 return this.graph.get(this.state, `${port.destId}/ports/${port.destName}`)
4242 }
4343 }
4444
45+ async send (port, message) {
46+ if (port.destId) {
47+ const id = port.destId
48+ const instance = await this.getInstance(id)
49+ return instance.queue(port.destName, message)
50+ } else {
51+ // port is unbound
52+ port.destPort.messages.push(message)
53+ }
54+ }
55+
4556 // loads an instance of a container from the state
4657 async _loadInstance (id) {
4758 const state = await this.graph.get(this.state, id)
4859 const container = this._containerTypes[state.type]
kernel.jsView
@@ -34,37 +34,38 @@
3434 * @param {object} message
3535 */
3636 queue (portName, message) {
3737 this.ports.queue(portName, message)
38- if (this.containerState !== 'running') {
39- this.containerState = 'running'
40- return this._runNextMessage()
41- }
38+ return this._startMessageLoop()
4239 }
4340
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()
4744 }
4845
4946 // 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'
5451
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+ const message = await this.ports.getNextMessage()
54+ if (!message) break
55+
56+ // dequqe message
57+ message.fromPort.messages.shift()
58+ // if the message we recived had more ticks then we currently have the
59+ // update it
60+ if (message._fromTicks > this.ticks) {
61+ this.ticks = message._fromTicks
62+ this.hypervisor.scheduler.update(this)
63+ }
64+ // run the next message
65+ await this.run(message)
6266 }
63- // run the next message
64- return this.run(message)
65- } else {
66- // if no more messages then shut down
67+ // no more messages; shut down
6768 this.hypervisor.scheduler.done(this.id)
6869 }
6970 }
7071
@@ -80,9 +81,8 @@
8081 const responsePort = message.responsePort
8182 delete message.responsePort
8283
8384 this.ports.addReceivedPorts(message)
84- message._hops++
8585
8686 if (message.constructor === DeleteMessage) {
8787 this.ports._delete(message.fromName)
8888 } else {
@@ -99,13 +99,11 @@
9999 if (responsePort) {
100100 this.send(responsePort, new Message({
101101 data: result
102102 }))
103- this.ports._unboundPorts.add(responsePort)
104103 }
105104
106105 this.ports.clearUnboundedPorts()
107- return this._runNextMessage()
108106 }
109107
110108 getResponsePort (message) {
111109 if (message.responsePort) {
@@ -164,23 +162,16 @@
164162 * sends a message to a given port
165163 * @param {Object} portRef - the port
166164 * @param {Message} message - the message
167165 */
168- async send (port, message) {
166+ send (port, message) {
167+ message._hops++
169168 // set the port that the message came from
170169 message._fromTicks = this.ticks
171170 this.ports.removeSentPorts(message)
172171
173172 // if (this.currentMessage !== message && !message.responsePort) {
174173 // this.currentMessage._addSubMessage(message)
175174 // }
176-
177- if (port.destId) {
178- const id = port.destId
179- const instance = await this.hypervisor.getInstance(id)
180- return instance.queue(port.destName, message)
181- } else {
182- // port is unbound
183- port.destPort.messages.push(message)
184- }
175+ return this.hypervisor.send(port, message)
185176 }
186177 }
package.jsonView
@@ -1,7 +1,7 @@
11 {
22 "name": "primea-hypervisor",
3- "version": "0.0.1",
3+ "version": "0.0.2",
44 "description": "this is a JS implemention of the primea hypervisor",
55 "scripts": {
66 "coverage": "node --harmony ./node_modules/istanbul/lib/cli.js cover ./tests/index.js",
77 "coveralls": "npm run coverage && coveralls <coverage/lcov.info",
tests/index.jsView
@@ -21,9 +21,9 @@
2121 }
2222
2323 node.on('ready', () => {
2424 tape('basic', async t => {
25- t.plan(2)
25+ t.plan(3)
2626 let message
2727 const expectedState = {
2828 '/': 'zdpuB1wc9Pb6jUzfNt4nAxAEUxB7kNhg4vbq7YLcEyBUb6iAB'
2929 }
@@ -52,8 +52,9 @@
5252 rootContainer.send(portRef1, message)
5353
5454 const stateRoot = await hypervisor.createStateRoot(Infinity)
5555 t.deepEquals(stateRoot, expectedState, 'expected root!')
56+ t.equals(hypervisor.scheduler.oldest(), 0)
5657 })
5758
5859 tape('basic - do not store containers with no ports bound', async t => {
5960 t.plan(1)

Built with git-ssb-web