| 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) 2018 Amal Hussein. All rights reserved. |
| 3 |
// This code is governed by the BSD license found in the LICENSE file. |
| 4 |
|
| 5 |
/*--- |
| 6 |
esid: sec-atomics.wait |
| 7 |
description: > |
| 8 |
New waiters should be applied to the end of the list and woken by order they entered the list (FIFO) |
| 9 |
info: | |
| 10 |
Atomics.wait( typedArray, index, value, timeout ) |
| 11 |
|
| 12 |
16.Perform AddWaiter(WL, W). |
| 13 |
... |
| 14 |
3.Add W to the end of the list of waiters in WL. |
| 15 |
|
| 16 |
includes: [atomicsHelper.js] |
| 17 |
features: [Atomics, BigInt, SharedArrayBuffer, TypedArray] |
| 18 |
---*/ |
| 19 |
|
| 20 |
var WAIT_INDEX = 0; |
| 21 |
var RUNNING = 1; |
| 22 |
var LOCK_INDEX = 2; |
| 23 |
var NUMAGENT = 3; |
| 24 |
|
| 25 |
const i64a = new BigInt64Array( |
| 26 |
new SharedArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT * 4) |
| 27 |
); |
| 28 |
|
| 29 |
for (var i = 0; i < NUMAGENT; i++) { |
| 30 |
var agentNum = i; |
| 31 |
|
| 32 |
$262.agent.start(` |
| 33 |
$262.agent.receiveBroadcast(function(sab) { |
| 34 |
const i64a = new BigInt64Array(sab); |
| 35 |
Atomics.add(i64a, ${RUNNING}, 1n); |
| 36 |
|
| 37 |
// Synchronize workers before reporting the initial report. |
| 38 |
while (Atomics.compareExchange(i64a, ${LOCK_INDEX}, 0n, 1n) !== 0n) ; |
| 39 |
|
| 40 |
// Report the agent number before waiting. |
| 41 |
$262.agent.report(${agentNum}); |
| 42 |
|
| 43 |
// Wait until restarted by main thread. |
| 44 |
var status = Atomics.wait(i64a, ${WAIT_INDEX}, 0n); |
| 45 |
|
| 46 |
// Report wait status. |
| 47 |
$262.agent.report(status); |
| 48 |
|
| 49 |
// Report the agent number after waiting. |
| 50 |
$262.agent.report(${agentNum}); |
| 51 |
|
| 52 |
$262.agent.leaving(); |
| 53 |
}); |
| 54 |
`); |
| 55 |
} |
| 56 |
|
| 57 |
$262.agent.safeBroadcast(i64a); |
| 58 |
|
| 59 |
// Wait until all agents started. |
| 60 |
$262.agent.waitUntil(i64a, RUNNING, BigInt(NUMAGENT)); |
| 61 |
|
| 62 |
// Agents may be started in any order. |
| 63 |
const started = []; |
| 64 |
for (var i = 0; i < NUMAGENT; i++) { |
| 65 |
// Wait until an agent entered its critical section. |
| 66 |
$262.agent.waitUntil(i64a, LOCK_INDEX, 1n); |
| 67 |
|
| 68 |
// Record the agent number. |
| 69 |
started.push($262.agent.getReport()); |
| 70 |
|
| 71 |
// The agent may have been interrupted between reporting its initial report |
| 72 |
// and the `Atomics.wait` call. Try to yield control to ensure the agent |
| 73 |
// actually started to wait. |
| 74 |
$262.agent.tryYield(); |
| 75 |
|
| 76 |
// Now continue with the next agent. |
| 77 |
Atomics.store(i64a, LOCK_INDEX, 0n); |
| 78 |
} |
| 79 |
|
| 80 |
// Agents must notify in the order they waited. |
| 81 |
for (var i = 0; i < NUMAGENT; i++) { |
| 82 |
var woken = 0; |
| 83 |
while ((woken = Atomics.notify(i64a, WAIT_INDEX, 1)) === 0) ; |
| 84 |
|
| 85 |
assert.sameValue(woken, 1, |
| 86 |
'Atomics.notify(i64a, WAIT_INDEX, 1) returns 1, at index = ' + i); |
| 87 |
|
| 88 |
assert.sameValue($262.agent.getReport(), 'ok', |
| 89 |
'$262.agent.getReport() returns "ok", at index = ' + i); |
| 90 |
|
| 91 |
assert.sameValue($262.agent.getReport(), started[i], |
| 92 |
'$262.agent.getReport() returns the value of `started[' + i + ']`'); |
| 93 |
} |
| 94 |
|
| 95 |
reportCompare(0, 0); |