Skip to content
Snippets Groups Projects
Commit 73be2127 authored by Rick Waldron's avatar Rick Waldron
Browse files

Atomics: wake/wake-in-order.js, capture waiterlist order for wake comparison

parent fc4a6f12
No related branches found
No related tags found
No related merge requests found
...@@ -36,13 +36,11 @@ function getReport() { ...@@ -36,13 +36,11 @@ function getReport() {
* @param {Number} expected The number of agents that are expected to report as active. * @param {Number} expected The number of agents that are expected to report as active.
*/ */
function waitUntil(i32a, index, expected) { function waitUntil(i32a, index, expected) {
var i = 0; while (Atomics.load(i32a, index) !== expected) {
while (Atomics.load(i32a, index) !== expected && i < 15) { /* nothing */
$262.agent.sleep(10);
i++;
} }
const agents = Atomics.load(i32a, index); const agents = Atomics.load(i32a, index);
assert.sameValue(agents, expected , `'agents' equals the value of expected (${expected })`); assert.sameValue(agents, expected, `'agents' equals the value of 'expected' (${expected})`);
} }
......
...@@ -19,40 +19,59 @@ const NUMELEM = RUNNING + 1; ...@@ -19,40 +19,59 @@ const NUMELEM = RUNNING + 1;
// them go into a wait, thus controlling the waiting order. Then we wake them // them go into a wait, thus controlling the waiting order. Then we wake them
// one by one and observe the wakeup order. // one by one and observe the wakeup order.
for (var i = 0; i < NUMAGENT; i++) { for (var attempt = 0; attempt < 10; attempt++) {
$262.agent.start(` for (var i = 0; i < NUMAGENT; i++) {
$262.agent.receiveBroadcast(function(sab) { $262.agent.start(`
const i32a = new Int32Array(sab); $262.agent.receiveBroadcast(function(sab) {
Atomics.add(i32a, ${RUNNING}, 1); const i32a = new Int32Array(sab);
while (Atomics.load(i32a, ${SPIN + i}) === 0) Atomics.add(i32a, ${RUNNING}, 1);
/* nothing */ ; while (Atomics.load(i32a, ${SPIN + i}) === 0) {
$262.agent.report(${i} + Atomics.wait(i32a, ${WAKEUP}, 0)); /* nothing */
$262.agent.leaving(); }
}); $262.agent.report(${i});
`); Atomics.wait(i32a, ${WAKEUP}, 0);
} $262.agent.report(${i});
$262.agent.leaving();
const i32a = new Int32Array( });
new SharedArrayBuffer(NUMELEM * Int32Array.BYTES_PER_ELEMENT) `);
); }
$262.agent.broadcast(i32a.buffer); const i32a = new Int32Array(
new SharedArrayBuffer(NUMELEM * Int32Array.BYTES_PER_ELEMENT)
);
// Wait for agents to be running. $262.agent.broadcast(i32a.buffer);
waitUntil(i32a, RUNNING, NUMAGENT);
// Sleep to allow the agents a fair chance to wait. If we don't, // Wait for agents to be running.
// we risk sending the wakeup before agents are sleeping, and we hang. waitUntil(i32a, RUNNING, NUMAGENT);
$262.agent.sleep(50);
// Make them sleep in order 0 1 2 on i32a[0] // Sleep to allow the agents a fair chance to wait. If we don't,
for (var i = 0; i < NUMAGENT; i++) { // we risk sending the wakeup before agents are sleeping, and we hang.
Atomics.store(i32a, SPIN + i, 1);
$262.agent.sleep(50); $262.agent.sleep(50);
}
// Wake them up one at a time and check the order is 0 1 2 var waiterlist = [];
for (var i = 0; i < NUMAGENT; i++) { assert.sameValue(Atomics.store(i32a, SPIN + 0, 1), 1);
assert.sameValue(Atomics.wake(i32a, WAKEUP, 1), 1, 'Atomics.wake(i32a, WAKEUP, 1) returns 1'); waiterlist.push(getReport());
assert.sameValue(getReport(), i + 'ok', 'getReport() returns i + "ok"');
assert.sameValue(Atomics.store(i32a, SPIN + 1, 1), 1);
waiterlist.push(getReport());
assert.sameValue(Atomics.store(i32a, SPIN + 2, 1), 1);
waiterlist.push(getReport());
var notified = [];
assert.sameValue(Atomics.wake(i32a, WAKEUP, 1), 1);
notified.push(getReport());
assert.sameValue(Atomics.wake(i32a, WAKEUP, 1), 1);
notified.push(getReport());
assert.sameValue(Atomics.wake(i32a, WAKEUP, 1), 1);
notified.push(getReport());
assert.sameValue(
notified.join(''),
waiterlist.join(''),
`Attempt #${attempt}: notified and waiterlist order do not match.`
);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment