git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 5d73da450ff3f8fae6bf5844ed02da668a06e011

fix scheduler lock condition

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 9/9/2017, 9:44:41 PM
Parent: 2d384c9d532ddc23ea6089a835bb4b338df7bd85

Files changed

portManager.jschanged
scheduler.jschanged
tests/index.jschanged
portManager.jsView
@@ -193,42 +193,47 @@
193193 */
194194 async getNextMessage (ports = this.ports, timeout = Infinity) {
195195 let message = this._peekNextMessage(ports)
196196 let oldestTime = this.hypervisor.scheduler.leastNumberOfTicks()
197+ let saturated = false
197198
199+ const findOldestMessage = async () => {
200+ while (// end if we have a message older then slowest containers
201+ !((message && oldestTime >= message._fromTicks) ||
202+ // end if there are no messages and this container is the oldest contaner
203+ (!message && oldestTime === this.kernel.ticks))) {
204+ if (saturated) {
205+ break
206+ }
207+ let ticksToWait = message ? message._fromTicks : this.kernel.ticks
208+ // ticksToWait = ticksToWait > timeout ? timeout : ticksToWait
209+ await Promise.race([
210+ this.hypervisor.scheduler.wait(ticksToWait, this.id).then(() => {
211+ message = this._peekNextMessage(ports)
212+ }),
213+ this._olderMessage(message).then(m => {
214+ message = m
215+ })
216+ ])
217+ oldestTime = this.hypervisor.scheduler.leastNumberOfTicks()
218+ }
219+ }
220+
198221 await Promise.race([
199222 this._whenSaturated().then(() => {
200223 message = this._peekNextMessage(ports)
224+ saturated = true
201225 }),
202- new Promise(async (resolve, reject) => {
203- while (// end if we have a message older then slowest containers
204- !((message && oldestTime >= message._fromTicks) ||
205- // end if there are no messages and this container is the oldest contaner
206- (!message && oldestTime === this.kernel.ticks))) {
207- let ticksToWait = message ? message._fromTicks : this.kernel.ticks
208- // ticksToWait = ticksToWait > timeout ? timeout : ticksToWait
226+ findOldestMessage()
227+ ])
209228
210- await Promise.race([
211- this.hypervisor.scheduler.wait(ticksToWait, this.id).then(() => {
212- message = this._peekNextMessage(ports)
213- }),
214- this._olderMessage(message).then(m => {
215- message = m
216- })
217- ])
218-
219- oldestTime = this.hypervisor.scheduler.leastNumberOfTicks()
220- }
221- resolve()
222- })
223- ])
224229 return message
225230 }
226231
227232 // tests wether or not all the ports have a message
228233 _isSaturated (ports) {
229234 const values = Object.values(ports)
230- return values.length ? values.every(port => port.messages.length) : false
235+ return values.length === 0 ? true : values.every(port => port.messages.length !== 0)
231236 }
232237
233238 // returns a promise that resolve when the ports are saturated
234239 _whenSaturated () {
scheduler.jsView
@@ -27,8 +27,9 @@
2727 * updates an instance with a new tick count
2828 * @param {Object} instance - a container instance
2929 */
3030 update (instance) {
31+ this._waits = this._waits.filter(wait => wait.id !== instance.id)
3132 this._update(instance)
3233 this._running.add(instance.id)
3334 this._checkWaits()
3435 }
tests/index.jsView
@@ -397,9 +397,9 @@
397397 ports: [portRef4]
398398 })
399399
400400 this.kernel.incrementTicks(6)
401- return Promise.all([
401+ await Promise.all([
402402 this.kernel.send(creationPort, message1),
403403 this.kernel.send(creationPort, message2),
404404 this.kernel.send(portRef1, this.kernel.createMessage()),
405405 this.kernel.send(portRef3, this.kernel.createMessage()),
@@ -470,8 +470,9 @@
470470 }
471471 }))
472472
473473 hypervisor.pin(root)
474+ root = await hypervisor.getInstance(root.id)
474475
475476 const [portRef1, portRef2] = root.ports.createChannel()
476477 const [portRef3, portRef4] = root.ports.createChannel()
477478

Built with git-ssb-web