| 1 |
// |reftest| skip-if(!this.hasOwnProperty('Atomics')||!this.hasOwnProperty('SharedArrayBuffer')||(this.hasOwnProperty('getBuildConfiguration')&&getBuildConfiguration('arm64-simulator'))) -- Atomics,SharedArrayBuffer is not enabled unconditionally, ARM64 Simulator cannot emulate atomics |
| 2 |
// Copyright (C) 2017 Mozilla Corporation. All rights reserved. |
| 3 |
// This code is governed by the BSD license found in the LICENSE file. |
| 4 |
|
| 5 |
/*--- |
| 6 |
esid: sec-atomics.notify |
| 7 |
description: > |
| 8 |
Test that Atomics.notify notifies agents in the order they are waiting. |
| 9 |
includes: [atomicsHelper.js] |
| 10 |
features: [Atomics, SharedArrayBuffer, TypedArray] |
| 11 |
---*/ |
| 12 |
|
| 13 |
const NUMAGENT = 3; |
| 14 |
const WAIT_INDEX = 0; // Waiters on this will be woken |
| 15 |
const SPIN = 1; // Worker i (zero-based) spins on location SPIN+i |
| 16 |
const RUNNING = SPIN + NUMAGENT; // Accounting of live agents |
| 17 |
const BUFFER_SIZE = RUNNING + 1; |
| 18 |
|
| 19 |
// Create workers and start them all spinning. We set atomic slots to make |
| 20 |
// them go into a wait, thus controlling the waiting order. Then we notify them |
| 21 |
// one by one and observe the notification order. |
| 22 |
|
| 23 |
for (var i = 0; i < NUMAGENT; i++) { |
| 24 |
$262.agent.start(` |
| 25 |
$262.agent.receiveBroadcast(function(sab) { |
| 26 |
const i32a = new Int32Array(sab); |
| 27 |
Atomics.add(i32a, ${RUNNING}, 1); |
| 28 |
|
| 29 |
while (Atomics.load(i32a, ${SPIN + i}) === 0) { |
| 30 |
/* nothing */ |
| 31 |
} |
| 32 |
|
| 33 |
$262.agent.report(${i}); |
| 34 |
Atomics.wait(i32a, ${WAIT_INDEX}, 0); |
| 35 |
$262.agent.report(${i}); |
| 36 |
|
| 37 |
$262.agent.leaving(); |
| 38 |
}); |
| 39 |
`); |
| 40 |
} |
| 41 |
|
| 42 |
const i32a = new Int32Array( |
| 43 |
new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * BUFFER_SIZE) |
| 44 |
); |
| 45 |
|
| 46 |
$262.agent.safeBroadcast(i32a); |
| 47 |
|
| 48 |
// Wait for agents to be running. |
| 49 |
$262.agent.waitUntil(i32a, RUNNING, NUMAGENT); |
| 50 |
|
| 51 |
var waiterlist = []; |
| 52 |
for (var i = 0; i < NUMAGENT; i++) { |
| 53 |
assert.sameValue( |
| 54 |
Atomics.store(i32a, SPIN + i, 1), |
| 55 |
1, |
| 56 |
`Atomics.store(i32a, SPIN + ${i}, 1) returns 1` |
| 57 |
); |
| 58 |
|
| 59 |
waiterlist.push($262.agent.getReport()); |
| 60 |
|
| 61 |
// Try to yield control to ensure the agent actually started to wait. |
| 62 |
$262.agent.tryYield(); |
| 63 |
} |
| 64 |
|
| 65 |
var notified = []; |
| 66 |
for (var i = 0; i < NUMAGENT; i++) { |
| 67 |
assert.sameValue( |
| 68 |
Atomics.notify(i32a, WAIT_INDEX, 1), |
| 69 |
1, |
| 70 |
`Atomics.notify(i32a, WAIT_INDEX, 1) returns 1 (${i})` |
| 71 |
); |
| 72 |
|
| 73 |
notified.push($262.agent.getReport()); |
| 74 |
} |
| 75 |
|
| 76 |
assert.sameValue( |
| 77 |
notified.join(''), |
| 78 |
waiterlist.join(''), |
| 79 |
'notified.join(\'\') returns waiterlist.join(\'\')' |
| 80 |
); |
| 81 |
|
| 82 |
reportCompare(0, 0); |