Debian Codegraph @

js/src/tests/test262/built-ins/Atomics/notify/bigint/notify-all-on-loc.js

raw
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 all waiters on a location, but does not
9 notify waiters on other locations.
10 includes: [atomicsHelper.js]
11 features: [Atomics, BigInt, SharedArrayBuffer, TypedArray]
12 ---*/
13
14 const WAIT_INDEX = 0; // Waiters on this will be woken
15 const WAIT_FAKE = 1; // Waiters on this will not be woken
16 const RUNNING = 2; // Accounting of live agents
17 const NOTIFY_INDEX = 3; // Accounting for too early timeouts
18 const NUMAGENT = 3;
19 const TIMEOUT_AGENT_MESSAGES = 2; // Number of messages for the timeout agent
20 const BUFFER_SIZE = 4;
21
22 // Long timeout to ensure the agent doesn't timeout before the main agent calls
23 // `Atomics.notify`.
24 const TIMEOUT = $262.agent.timeouts.long;
25
26 const i64a = new BigInt64Array(
27 new SharedArrayBuffer(BigInt64Array.BYTES_PER_ELEMENT * BUFFER_SIZE)
28 );
29
30 for (var i = 0; i < NUMAGENT; i++) {
31 $262.agent.start(`
32 $262.agent.receiveBroadcast(function(sab) {
33 const i64a = new BigInt64Array(sab);
34 Atomics.add(i64a, ${RUNNING}, 1n);
35
36 $262.agent.report("A " + Atomics.wait(i64a, ${WAIT_INDEX}, 0n));
37 $262.agent.leaving();
38 });
39 `);
40 }
41
42 $262.agent.start(`
43 $262.agent.receiveBroadcast(function(sab) {
44 const i64a = new BigInt64Array(sab);
45 Atomics.add(i64a, ${RUNNING}, 1n);
46
47 // This will always time out.
48 $262.agent.report("B " + Atomics.wait(i64a, ${WAIT_FAKE}, 0n, ${TIMEOUT}));
49
50 // If this value is not 1n, then the agent timeout before the main agent
51 // called Atomics.notify.
52 const result = Atomics.load(i64a, ${NOTIFY_INDEX}) === 1n
53 ? "timeout after Atomics.notify"
54 : "timeout before Atomics.notify";
55 $262.agent.report("W " + result);
56
57 $262.agent.leaving();
58 });
59 `);
60
61 $262.agent.safeBroadcast(i64a);
62
63 // Wait for agents to be running.
64 $262.agent.waitUntil(i64a, RUNNING, BigInt(NUMAGENT + 1));
65
66 // Try to yield control to ensure the agent actually started to wait. If we
67 // don't, we risk sending the notification before agents are sleeping, and we hang.
68 $262.agent.tryYield();
69
70 // Notify all waiting on WAIT_INDEX, should be 3 always, they won't time out.
71 assert.sameValue(
72 Atomics.notify(i64a, WAIT_INDEX),
73 NUMAGENT,
74 'Atomics.notify(i64a, WAIT_INDEX) returns the value of `NUMAGENT`'
75 );
76
77 Atomics.store(i64a, NOTIFY_INDEX, 1n);
78
79 const reports = [];
80 for (var i = 0; i < NUMAGENT + TIMEOUT_AGENT_MESSAGES; i++) {
81 reports.push($262.agent.getReport());
82 }
83 reports.sort();
84
85 for (var i = 0; i < NUMAGENT; i++) {
86 assert.sameValue(reports[i], 'A ok', 'The value of reports[i] is "A ok"');
87 }
88 assert.sameValue(reports[NUMAGENT], 'B timed-out', 'The value of reports[NUMAGENT] is "B timed-out"');
89 assert.sameValue(reports[NUMAGENT + 1], "W timeout after Atomics.notify",
90 'The value of reports[NUMAGENT + 1] is "W timeout after Atomics.notify"');
91
92 reportCompare(0, 0);