Makefile 0000664 0001750 0001750 00000000142 13176107551 012365 0 ustar lhansen lhansen JSSHELL=~/m-i/js/src/build-debug/dist/bin/js default: $(JSSHELL) preload-wasm.js > load-wasm.js load-wasm.js 0000664 0001750 0001750 00000001003 13176107554 013147 0 ustar lhansen lhansen var WasmBytes = new Uint8Array([0,97,115,109,1,0,0,0,1,137,128,128,128,0,2,96,1,127,1,127,96,0,0,2,141,128,128,128,0,1,0,6,109,101,109,111,114,121,2,3,1,1,3,131,128,128,128,0,2,0,1,6,129,128,128,128,0,0,7,145,128,128,128,0,2,4,109,97,105,110,0,0,6,119,111,114,107,101,114,0,1,10,203,128,128,128,0,2,146,128,128,128,0,0,65,48,32,0,254,23,2,0,65,48,65,127,254,0,0,0,11,174,128,128,128,0,1,1,127,2,64,3,64,65,48,32,0,66,127,254,1,2,0,26,65,48,254,16,2,0,33,0,65,52,32,0,254,30,2,0,26,32,0,65,214,0,71,13,0,11,11,11]); preload-wasm.js 0000664 0001750 0001750 00000001632 13176107640 013662 0 ustar lhansen lhansen // Use this to generate load-wasm.js; see Makefile. let SYNC = '(i32.const 48)'; // where workers sleep and main writes let ACC = '(i32.const 52)'; // where workers accumulate values read let DONE = 86; // flag value let text = `(module (memory (import "" "memory") 1 1 shared) (func (export "main") (param $val i32) (result i32) (i32.atomic.store ${SYNC} (get_local $val)) (wake ${SYNC} (i32.const -1))) (func (export "worker") (local $prev i32) (loop $out $in (drop (i32.wait ${SYNC} (get_local $prev) (i64.const -1))) (set_local $prev (i32.atomic.load ${SYNC})) (drop (i32.atomic.rmw.add ${ACC} (get_local $prev))) (br_if $in (i32.ne (get_local $prev) (i32.const ${DONE}))))))`; let bin = new Uint8Array(wasmTextToBinary(text)); let s = "var WasmBytes = new Uint8Array(["; s += bin[0]; for ( let i=1; i < bin.length; i++ ) s += "," + bin[i]; s += "]);"; print(s); test-mem.js 0000664 0001750 0001750 00000003356 13176110467 013030 0 ustar lhansen lhansen // Simple test case with shared memory: // // - Each worker will create a wasm instance from the received module and // memory, and enter a wasm loop that sleeps on a location, reads that // location's new value after waking and adds it to a shared counter, and // repeats this until the value read is 86, after which they exit. // // - The main loop runs a wasm function on a timer that sets up the value and // signals the workers, before exiting to JS, which sleeps, and does it again. // The main wasm function returns the number of waiters woken, which we check. let mod = new WebAssembly.Module(WasmBytes); let mem = new WebAssembly.Memory({initial: 1, maximum: 1, shared: true}); let ins = new WebAssembly.Instance(mod, {"": {memory: mem}}); let main = ins.exports.main; let buf = new Int32Array(mem.buffer); let NUM = 10; // Workers let ACC = 52 / 4; // Accumulator location let DONE = 86; // Termination value let STEP = 250; // Timeout writeln("Fine so far"); for ( let i=1; i <= NUM; i++ ) { let w = new Worker("test-mem-worker.js"); w.onmessage = evHandler; w.postMessage([i, mod, mem]); } setTimeout(nextItem, STEP); let data = [1, 1, 2, 3, 5, 8, 13, 21, 34, DONE]; let dataptr = 0; function nextItem() { let expected = 0; for ( let i=0; i < dataptr; i++ ) expected += data[i]; expected *= NUM; let val = buf[ACC]; if (val != expected) writeln("ERROR! Expected: " + expected); writeln("Sum: " + val); let v = data[dataptr++]; let c = main(v); if (c != NUM) writeln("Bad wakeup count: " + c); if (v != DONE) setTimeout(nextItem, STEP); else writeln("Terminating"); } function evHandler(ev) { message(String(ev.data)); } function writeln(s) { message("Main: " + s); } test-mem-worker.js 0000664 0001750 0001750 00000000512 13176110362 014320 0 ustar lhansen lhansen var id; onmessage = function (ev) { let mod, mem; [id, mod, mem] = ev.data; let ins = new WebAssembly.Instance(mod, {"": {memory: mem}}); let worker = ins.exports.worker; writeln("Fine so far"); worker(); writeln("Terminating"); } function writeln(s) { postMessage("Worker " + id + ": " + s); } test-mem.html 0000664 0001750 0001750 00000000522 13176110411 013335 0 ustar lhansen lhansen