Debian Codegraph @

js/src/tests/test262/built-ins/Atomics/waitAsync/bigint/no-spurious-wakeup-on-compareExchange.js

raw
1 // |reftest| shell-option(--setpref=atomics_wait_async) skip-if(!this.hasOwnProperty('SharedArrayBuffer')||!this.hasOwnProperty('Atomics')||(this.hasOwnProperty('getBuildConfiguration')&&getBuildConfiguration('arm64-simulator'))||!xulRuntime.shell) async -- SharedArrayBuffer,Atomics is not enabled unconditionally, ARM64 Simulator cannot emulate atomics, requires shell-options
2 // Copyright (C) 2020 Rick Waldron. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4 /*---
5 esid: sec-atomics.waitasync
6 description: >
7 Waiter does not spuriously notify on index which is subject to compareExchange operation
8 info: |
9 AddWaiter ( WL, waiterRecord )
10
11 5. Append waiterRecord as the last element of WL.[[Waiters]]
12 6. If waiterRecord.[[Timeout]] is finite, then in parallel,
13 a. Wait waiterRecord.[[Timeout]] milliseconds.
14 b. Perform TriggerTimeout(WL, waiterRecord).
15
16 TriggerTimeout( WL, waiterRecord )
17
18 3. If waiterRecord is in WL.[[Waiters]], then
19 a. Set waiterRecord.[[Result]] to "timed-out".
20 b. Perform RemoveWaiter(WL, waiterRecord).
21 c. Perform NotifyWaiter(WL, waiterRecord).
22 4. Perform LeaveCriticalSection(WL).
23
24 flags: [async]
25 includes: [atomicsHelper.js]
26 features: [Atomics.waitAsync, SharedArrayBuffer, TypedArray, Atomics, BigInt, arrow-function, async-functions]
27 ---*/
28 assert.sameValue(typeof Atomics.waitAsync, 'function', 'The value of `typeof Atomics.waitAsync` is "function"');
29 const RUNNING = 1;
30 const TIMEOUT = $262.agent.timeouts.small;
31 const i64a = new BigInt64Array(new SharedArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT * 4));
32
33 $262.agent.start(`
34 $262.agent.receiveBroadcast(async (sab) => {
35 const i64a = new BigInt64Array(sab);
36 Atomics.add(i64a, ${RUNNING}, 1n);
37
38 const before = $262.agent.monotonicNow();
39 const unpark = await Atomics.waitAsync(i64a, 0, 0n, ${TIMEOUT}).value;
40 const duration = $262.agent.monotonicNow() - before;
41
42 $262.agent.report(duration);
43 $262.agent.report(unpark);
44 $262.agent.leaving();
45 });
46 `);
47
48 $262.agent.safeBroadcastAsync(i64a, RUNNING, 1n).then(async agentCount => {
49 assert.sameValue(agentCount, 1n, 'The value of `agentCount` is 1n');
50 Atomics.compareExchange(i64a, 0, 0n, 1n);
51 const lapse = await $262.agent.getReportAsync();
52 assert(lapse >= TIMEOUT, 'The result of evaluating `(lapse >= TIMEOUT)` is true');
53 const result = await $262.agent.getReportAsync();
54 assert.sameValue(result, 'timed-out', 'The value of `result` is "timed-out"');
55
56 assert.sameValue(
57 Atomics.notify(i64a, 0),
58 0,
59 'Atomics.notify(new BigInt64Array(new SharedArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT * 4)), 0) must return 0'
60 );
61 }).then($DONE, $DONE);