Created
July 16, 2019 20:43
-
-
Save binji/63595127cb189b25eb355ce8db0e0bca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype HTML> | |
<script src="test.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let thread_count = 40; | |
let url = 'worker.js'; | |
let threads = []; | |
function post(thread, msg) { | |
const promise = new Promise((resolve, _) => { | |
thread.resolve = resolve; | |
}); | |
thread.worker.postMessage(msg); | |
return promise; | |
} | |
async function postAndWait(thread, msg) { | |
return await post(thread, msg); | |
} | |
async function broadcastAndWait(msg) { | |
await Promise.all(threads.map(thread => post(thread, msg))); | |
} | |
async function instance(name, bytes) { | |
const module = new WebAssembly.Module(bytes); | |
await broadcastAndWait({kind: 'instance', name, module}); | |
return new WebAssembly.Instance(module); | |
} | |
function spawn(id, name, args) { | |
post(threads[id], {kind: 'spawn', name, args}); | |
} | |
async function join(id) { | |
const response = await post(threads[id], {kind: 'join'}); | |
return response.result; | |
} | |
(async function() { | |
for (let id = 0; id < thread_count; ++id) { | |
const worker = new Worker(url); | |
const thread = {id, worker}; | |
worker.addEventListener('message', (event) => { | |
threads[event.data.id].resolve(event.data); | |
}); | |
threads.push(thread); | |
await postAndWait(thread, {kind: 'start', id}); | |
} | |
await instance( | |
'm', new Uint8Array([ | |
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, | |
0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, | |
0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, | |
0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b | |
])); | |
spawn(0, 'add', [3, 4]); | |
spawn(1, 'add', [10, 20]); | |
console.log(await join(0)); | |
console.log(await join(1)); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(func (export "add") (param i32 i32) (result i32) | |
local.get 0 | |
local.get 1 | |
i32.add) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let id = undefined; | |
let instance = null; | |
let result = null; | |
function ok() { | |
console.log(`worker ${id}: sending ok`); | |
postMessage({id}); | |
} | |
onmessage = function(event) { | |
const msg = event.data; | |
console.log(`worker ${id}: got message: ${JSON.stringify(msg)}`); | |
switch (msg.kind) { | |
case 'start': | |
id = msg.id; | |
ok(); | |
break; | |
case 'instance': | |
instance = new WebAssembly.Instance(msg.module); | |
ok(); | |
break; | |
case 'spawn': | |
result = instance.exports[msg.name](...msg.args); | |
break; | |
case 'join': | |
postMessage({id, result}); | |
break; | |
default: | |
console.log(`Unknown message: ${msg}`); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment