git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit e522215a494194cd9440b49cad344da47319e914

added lock map

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 8/16/2017, 8:25:55 PM
Parent: d4cfbd48b828917f70d5680efb0a6567637efcdb

Files changed

index.jschanged
kernel.jschanged
package.jsonchanged
portManager.jschanged
scheduler.jschanged
index.jsView
@@ -106,9 +106,9 @@
106106 let instance = this.scheduler.getInstance(id)
107107 if (instance) {
108108 return instance
109109 } else {
110- const resolve = this.scheduler.getLock(id)
110+ const resolve = this.scheduler.lock(id)
111111 const instance = await this._loadInstance(id)
112112 await instance.startup()
113113 resolve(instance)
114114 return instance
@@ -125,13 +125,9 @@
125125 * @param {object} id.parent
126126 * @returns {Promise}
127127 */
128128 async createInstance (type, message = new Message(), id = {nonce: 0, parent: null}) {
129- // create a lock to prevent the scheduler from reloving waits before the
130- // new container is loaded
131- // const unlock = this.scheduler.getLock(id)
132129 const idHash = await this._getHashFromObj(id)
133- // const code = message.data.byteLength ? message.data : undefined
134130 const state = {
135131 nonce: [0],
136132 ports: {},
137133 type: type
kernel.jsView
@@ -161,10 +161,13 @@
161161 // incerment the nonce
162162 nonce = new BN(nonce)
163163 nonce.iaddn(1)
164164 this.state.nonce = nonce.toArray()
165- this.ports.removeSentPorts(message)
166165
166+ if (message) {
167+ this.ports.removeSentPorts(message)
168+ }
169+
167170 return this.hypervisor.createInstance(type, message, id)
168171 }
169172
170173 /**
package.jsonView
@@ -33,8 +33,9 @@
3333 "binary-search-insert": "^1.0.3",
3434 "bn.js": "^4.11.6",
3535 "chunk": "0.0.2",
3636 "ipld-graph-builder": "1.3.0",
37+ "lockmap": "0.0.0",
3738 "merkle-radix-tree": "0.0.4",
3839 "primea-message": "0.0.2"
3940 },
4041 "devDependencies": {
portManager.jsView
@@ -63,11 +63,14 @@
6363 port.messages.forEach(message => {
6464 message._fromPort = port
6565 message.fromName = name
6666 })
67- destPort.destName = name
68- destPort.destId = this.id
69- delete destPort.destPort
67+
68+ if (destPort) {
69+ destPort.destName = name
70+ destPort.destId = this.id
71+ delete destPort.destPort
72+ }
7073 }
7174 }
7275
7376 /**
scheduler.jsView
@@ -1,5 +1,6 @@
11 const binarySearchInsert = require('binary-search-insert')
2+const LockMap = require('lockmap')
23
34 module.exports = class Scheduler {
45 /**
56 * The Sceduler manages the run cycle of the containes and figures out which
@@ -7,27 +8,19 @@
78 */
89 constructor () {
910 this._waits = []
1011 this._running = new Set()
11- this._loadingInstances = new Map()
12+ this._loadingInstances = new LockMap()
1213 this.instances = new Map()
1314 }
1415
1516 /**
1617 * locks the scheduler from clearing waits untill the lock is resolved
1718 * @param {string} id
1819 * @return {function} the resolve function to call once it to unlock
1920 */
20- getLock (id) {
21- let r
22- const promise = new Promise((resolve, reject) => {
23- r = resolve
24- })
25- promise.then(() => {
26- this._loadingInstances.delete(id)
27- })
28- this._loadingInstances.set(id, promise)
29- return r
21+ lock (id) {
22+ return this._loadingInstances.lock(id)
3023 }
3124
3225 /**
3326 * updates an instance with a new tick count
@@ -56,9 +49,9 @@
5649 * @param {string} id
5750 * @return {object}
5851 */
5952 getInstance (id) {
60- return this.instances.get(id) || this._loadingInstances.get(id)
53+ return this.instances.get(id) || this._loadingInstances.getLock(id)
6154 }
6255
6356 /**
6457 * deletes an instance from the scheduler

Built with git-ssb-web