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.js | changed |
kernel.js | changed |
package.json | changed |
portManager.js | changed |
scheduler.js | changed |
index.js | ||
---|---|---|
@@ -106,9 +106,9 @@ | ||
106 | 106 | let instance = this.scheduler.getInstance(id) |
107 | 107 | if (instance) { |
108 | 108 | return instance |
109 | 109 | } else { |
110 | - const resolve = this.scheduler.getLock(id) | |
110 | + const resolve = this.scheduler.lock(id) | |
111 | 111 | const instance = await this._loadInstance(id) |
112 | 112 | await instance.startup() |
113 | 113 | resolve(instance) |
114 | 114 | return instance |
@@ -125,13 +125,9 @@ | ||
125 | 125 | * @param {object} id.parent |
126 | 126 | * @returns {Promise} |
127 | 127 | */ |
128 | 128 | 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) | |
132 | 129 | const idHash = await this._getHashFromObj(id) |
133 | - // const code = message.data.byteLength ? message.data : undefined | |
134 | 130 | const state = { |
135 | 131 | nonce: [0], |
136 | 132 | ports: {}, |
137 | 133 | type: type |
kernel.js | ||
---|---|---|
@@ -161,10 +161,13 @@ | ||
161 | 161 | // incerment the nonce |
162 | 162 | nonce = new BN(nonce) |
163 | 163 | nonce.iaddn(1) |
164 | 164 | this.state.nonce = nonce.toArray() |
165 | - this.ports.removeSentPorts(message) | |
166 | 165 | |
166 | + if (message) { | |
167 | + this.ports.removeSentPorts(message) | |
168 | + } | |
169 | + | |
167 | 170 | return this.hypervisor.createInstance(type, message, id) |
168 | 171 | } |
169 | 172 | |
170 | 173 | /** |
package.json | ||
---|---|---|
@@ -33,8 +33,9 @@ | ||
33 | 33 | "binary-search-insert": "^1.0.3", |
34 | 34 | "bn.js": "^4.11.6", |
35 | 35 | "chunk": "0.0.2", |
36 | 36 | "ipld-graph-builder": "1.3.0", |
37 | + "lockmap": "0.0.0", | |
37 | 38 | "merkle-radix-tree": "0.0.4", |
38 | 39 | "primea-message": "0.0.2" |
39 | 40 | }, |
40 | 41 | "devDependencies": { |
portManager.js | ||
---|---|---|
@@ -63,11 +63,14 @@ | ||
63 | 63 | port.messages.forEach(message => { |
64 | 64 | message._fromPort = port |
65 | 65 | message.fromName = name |
66 | 66 | }) |
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 | + } | |
70 | 73 | } |
71 | 74 | } |
72 | 75 | |
73 | 76 | /** |
scheduler.js | ||
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | 1 | const binarySearchInsert = require('binary-search-insert') |
2 | +const LockMap = require('lockmap') | |
2 | 3 | |
3 | 4 | module.exports = class Scheduler { |
4 | 5 | /** |
5 | 6 | * The Sceduler manages the run cycle of the containes and figures out which |
@@ -7,27 +8,19 @@ | ||
7 | 8 | */ |
8 | 9 | constructor () { |
9 | 10 | this._waits = [] |
10 | 11 | this._running = new Set() |
11 | - this._loadingInstances = new Map() | |
12 | + this._loadingInstances = new LockMap() | |
12 | 13 | this.instances = new Map() |
13 | 14 | } |
14 | 15 | |
15 | 16 | /** |
16 | 17 | * locks the scheduler from clearing waits untill the lock is resolved |
17 | 18 | * @param {string} id |
18 | 19 | * @return {function} the resolve function to call once it to unlock |
19 | 20 | */ |
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) | |
30 | 23 | } |
31 | 24 | |
32 | 25 | /** |
33 | 26 | * updates an instance with a new tick count |
@@ -56,9 +49,9 @@ | ||
56 | 49 | * @param {string} id |
57 | 50 | * @return {object} |
58 | 51 | */ |
59 | 52 | getInstance (id) { |
60 | - return this.instances.get(id) || this._loadingInstances.get(id) | |
53 | + return this.instances.get(id) || this._loadingInstances.getLock(id) | |
61 | 54 | } |
62 | 55 | |
63 | 56 | /** |
64 | 57 | * deletes an instance from the scheduler |
Built with git-ssb-web