|
1 | 1 | // META: global=window,dedicatedworker,jsshell |
2 | 2 | // META: script=/wasm/jsapi/wasm-module-builder.js |
| 3 | +// META: script=/wasm/jsapi/memory/worker-path-helper.js |
3 | 4 |
|
4 | 5 | function createModule() { |
5 | 6 | const builder = new WasmModuleBuilder(); |
@@ -59,3 +60,132 @@ test(() => { |
59 | 60 | assert_equals( |
60 | 61 | result, 0, 'Notify should return 0 (number of waiters notified)'); |
61 | 62 | }, 'Notify on shared memory (0 waiters)'); |
| 63 | + |
| 64 | +function waitForWorker(worker) { |
| 65 | + const msg = worker.getMessage(); |
| 66 | + if (msg.type === 'error') { |
| 67 | + throw new Error('Worker error: ' + msg.message); |
| 68 | + } |
| 69 | + return msg.value; |
| 70 | +} |
| 71 | + |
| 72 | +function assert_within_timeout(start, seconds, message) { |
| 73 | + if (Date.now() - start > seconds * 1000) { |
| 74 | + throw new Error(message); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// Async tests using workers. |
| 79 | +if (typeof Worker !== 'undefined') { |
| 80 | + test(() => { |
| 81 | + const memory = |
| 82 | + new WebAssembly.Memory({initial: 1, maximum: 1, shared: true}); |
| 83 | + const view = new Int32Array(memory.buffer); |
| 84 | + const worker = new Worker(getWorkerPath('wait-notify-worker.js')); |
| 85 | + |
| 86 | + view[0] = 0; |
| 87 | + view[1] = 0; // ready index |
| 88 | + |
| 89 | + worker.postMessage({ |
| 90 | + module: module, |
| 91 | + memory: memory, |
| 92 | + address: 0, |
| 93 | + expected: 0, |
| 94 | + timeout: -1n, |
| 95 | + readyIndex: 1 |
| 96 | + }); |
| 97 | + |
| 98 | + while (Atomics.load(view, 1) === 0); |
| 99 | + |
| 100 | + const instance = createInstance(module, memory); |
| 101 | + let notifyResult; |
| 102 | + const start = Date.now(); |
| 103 | + while ((notifyResult = instance.exports.notify(0, 1)) === 0) { |
| 104 | + assert_within_timeout( |
| 105 | + start, 30, 'Worker should wake up within 30 seconds'); |
| 106 | + } |
| 107 | + |
| 108 | + assert_equals(notifyResult, 1, 'Notify should wake up 1 waiter'); |
| 109 | + const waitResult = waitForWorker(worker); |
| 110 | + assert_equals(waitResult, 0, 'Wait32 should return 0 (ok) when woken up'); |
| 111 | + worker.terminate(); |
| 112 | + }, 'Wait32 and Notify wake up 1 waiter'); |
| 113 | + |
| 114 | + test(() => { |
| 115 | + const memory = |
| 116 | + new WebAssembly.Memory({initial: 1, maximum: 1, shared: true}); |
| 117 | + const view = new Int32Array(memory.buffer); |
| 118 | + const worker1 = new Worker(getWorkerPath('wait-notify-worker.js')); |
| 119 | + const worker2 = new Worker(getWorkerPath('wait-notify-worker.js')); |
| 120 | + |
| 121 | + view[0] = 0; // address 0 |
| 122 | + view[1] = 0; // address 4 |
| 123 | + view[2] = 0; // ready index 1 |
| 124 | + view[3] = 0; // ready index 2 |
| 125 | + |
| 126 | + const msg = {module: module, memory: memory, timeout: -1n, expected: 0}; |
| 127 | + worker1.postMessage({...msg, address: 0, readyIndex: 2}); |
| 128 | + worker2.postMessage({...msg, address: 4, readyIndex: 3}); |
| 129 | + |
| 130 | + while (Atomics.load(view, 2) === 0); |
| 131 | + while (Atomics.load(view, 3) === 0); |
| 132 | + |
| 133 | + const instance = createInstance(module, memory); |
| 134 | + |
| 135 | + let notified1; |
| 136 | + let start = Date.now(); |
| 137 | + while ((notified1 = instance.exports.notify(0, 1)) === 0) { |
| 138 | + assert_within_timeout( |
| 139 | + start, 30, 'Worker 1 should wake up within 30 seconds'); |
| 140 | + } |
| 141 | + assert_equals(notified1, 1, 'Notify 1'); |
| 142 | + assert_equals(waitForWorker(worker1), 0); |
| 143 | + |
| 144 | + let notified2; |
| 145 | + start = Date.now(); |
| 146 | + while ((notified2 = instance.exports.notify(4, 1)) === 0) { |
| 147 | + assert_within_timeout( |
| 148 | + start, 30, 'Worker 2 should wake up within 30 seconds'); |
| 149 | + } |
| 150 | + assert_equals(notified2, 1, 'Notify 2'); |
| 151 | + assert_equals(waitForWorker(worker2), 0); |
| 152 | + |
| 153 | + worker1.terminate(); |
| 154 | + worker2.terminate(); |
| 155 | + }, 'Two waiters on different addresses woken up one after the other'); |
| 156 | + |
| 157 | + test(() => { |
| 158 | + const memory = |
| 159 | + new WebAssembly.Memory({initial: 1, maximum: 1, shared: true}); |
| 160 | + const view = new Int32Array(memory.buffer); |
| 161 | + const worker1 = new Worker(getWorkerPath('wait-notify-worker.js')); |
| 162 | + const worker2 = new Worker(getWorkerPath('wait-notify-worker.js')); |
| 163 | + |
| 164 | + view[0] = 0; // address 0 |
| 165 | + view[2] = 0; // ready index 1 |
| 166 | + view[3] = 0; // ready index 2 |
| 167 | + |
| 168 | + const msg = {module: module, memory: memory, timeout: -1n, expected: 0}; |
| 169 | + worker1.postMessage({...msg, address: 0, readyIndex: 2}); |
| 170 | + worker2.postMessage({...msg, address: 0, readyIndex: 3}); |
| 171 | + |
| 172 | + while (Atomics.load(view, 2) === 0); |
| 173 | + while (Atomics.load(view, 3) === 0); |
| 174 | + |
| 175 | + const instance = createInstance(module, memory); |
| 176 | + |
| 177 | + let notified = 0; |
| 178 | + const start = Date.now(); |
| 179 | + while ((notified += instance.exports.notify(0, 2 - notified)) < 2) { |
| 180 | + assert_within_timeout( |
| 181 | + start, 30, 'Both workers should wake up within 30 seconds'); |
| 182 | + } |
| 183 | + assert_equals(notified, 2, 'Notify 2 at once'); |
| 184 | + |
| 185 | + assert_equals(waitForWorker(worker1), 0); |
| 186 | + assert_equals(waitForWorker(worker2), 0); |
| 187 | + |
| 188 | + worker1.terminate(); |
| 189 | + worker2.terminate(); |
| 190 | + }, 'Two waiters on same address woken up at once'); |
| 191 | +} |
0 commit comments