Debian Codegraph @

js/src/tests/test262/built-ins/Atomics/notify/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, 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 for (var i = 0; i < NUMAGENT; i++) {
27 $262.agent.start(`
28 $262.agent.receiveBroadcast(function(sab) {
29 const i32a = new Int32Array(sab);
30 Atomics.add(i32a, ${RUNNING}, 1);
31
32 $262.agent.report("A " + Atomics.wait(i32a, ${WAIT_INDEX}, 0));
33 $262.agent.leaving();
34 });
35 `);
36 }
37
38 $262.agent.start(`
39 $262.agent.receiveBroadcast(function(sab) {
40 const i32a = new Int32Array(sab);
41 Atomics.add(i32a, ${RUNNING}, 1);
42
43 // This will always time out.
44 $262.agent.report("B " + Atomics.wait(i32a, ${WAIT_FAKE}, 0, ${TIMEOUT}));
45
46 // If this value is not 1, then the agent timeout before the main agent
47 // called Atomics.notify.
48 const result = Atomics.load(i32a, ${NOTIFY_INDEX}) === 1
49 ? "timeout after Atomics.notify"
50 : "timeout before Atomics.notify";
51 $262.agent.report("W " + result);
52
53 $262.agent.leaving();
54 });
55 `);
56
57 const i32a = new Int32Array(
58 new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * BUFFER_SIZE)
59 );
60
61 $262.agent.safeBroadcast(i32a);
62
63 // Wait for agents to be running.
64 $262.agent.waitUntil(i32a, RUNNING, 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(i32a, WAIT_INDEX),
73 NUMAGENT,
74 'Atomics.notify(i32a, WAIT_INDEX) returns the value of `NUMAGENT`'
75 );
76
77 Atomics.store(i32a, NOTIFY_INDEX, 1);
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);