You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
932 B
41 lines
932 B
8 months ago
|
let thread_id = null;
|
||
|
let buf_offset = null;
|
||
|
let exports = null;
|
||
|
let flags = null;
|
||
|
|
||
|
function done() {
|
||
|
postMessage('done');
|
||
|
}
|
||
|
|
||
|
async function init_wasm(tid, memory, offset, notify_flags, stack_base) {
|
||
|
thread_id = tid;
|
||
|
buf_offset = offset;
|
||
|
|
||
|
const result = await WebAssembly.instantiateStreaming(fetch('wasm/multi.wasm'), {
|
||
|
env: { 'memory': memory }
|
||
|
});
|
||
|
|
||
|
exports = result.instance.exports;
|
||
|
//console.log(tid, 'init');
|
||
|
exports.set_sp(stack_base - thread_id * 4096);
|
||
|
|
||
|
flags = notify_flags;
|
||
|
done();
|
||
|
}
|
||
|
|
||
|
function do_work(num, callback) {
|
||
|
//console.log(thread_id, 'work');
|
||
|
exports.write_a_number(buf_offset, thread_id, thread_id * num);
|
||
|
done();
|
||
|
}
|
||
|
|
||
|
onmessage = (e) => {
|
||
|
if (e.data.type === 0) {
|
||
|
init_wasm(e.data.thread_id, e.data.memory, e.data.buffer_offset, e.data.flags, e.data.stack_base);
|
||
|
} else if (e.data.type === 1) {
|
||
|
do_work(e.data.num);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|