Debian Codegraph @

tests/auto/qml/ecmascripttests/test262/test/built-ins/Atomics/wake/wake-in-order.js

raw
1 // Copyright (C) 2017 Mozilla Corporation. All rights reserved.
2 // This code is governed by the BSD license found in the LICENSE file.
3
4 /*---
5 esid: sec-atomics.wake
6 description: >
7 Test that Atomics.wake wakes agents in the order they are waiting.
8 ---*/
9
10 var NUMAGENT = 3;
11
12 var WAKEUP = 0; // Waiters on this will be woken
13 var SPIN = 1; // Worker i (zero-based) spins on location SPIN+i
14 var RUNNING = SPIN + NUMAGENT; // Accounting of live agents
15 var NUMELEM = RUNNING + 1;
16
17 // Create workers and start them all spinning. We set atomic slots to make
18 // them go into a wait, thus controlling the waiting order. Then we wake them
19 // one by one and observe the wakeup order.
20
21 for ( var i=0 ; i < NUMAGENT ; i++ ) {
22 $262.agent.start(
23 `
24 $262.agent.receiveBroadcast(function (sab) {
25 var ia = new Int32Array(sab);
26 Atomics.add(ia, ${RUNNING}, 1);
27 while (Atomics.load(ia, ${SPIN + i}) === 0)
28 /* nothing */ ;
29 $262.agent.report(${i} + Atomics.wait(ia, ${WAKEUP}, 0));
30 $262.agent.leaving();
31 })
32 `);
33 }
34
35 var ia = new Int32Array(new SharedArrayBuffer(NUMELEM * Int32Array.BYTES_PER_ELEMENT));
36 $262.agent.broadcast(ia.buffer);
37
38 // Wait for agents to be running.
39 waitUntil(ia, RUNNING, NUMAGENT);
40
41 // Then wait some more to give the agents a fair chance to wait. If we don't,
42 // we risk sending the wakeup before agents are sleeping, and we hang.
43 $262.agent.sleep(500);
44
45 // Make them sleep in order 0 1 2 on ia[0]
46 for (var i = 0; i < NUMAGENT; i++) {
47 Atomics.store(ia, SPIN + i, 1);
48 $262.agent.sleep(500);
49 }
50
51 // Wake them up one at a time and check the order is 0 1 2
52 for (var i = 0; i < NUMAGENT; i++) {
53 assert.sameValue(Atomics.wake(ia, WAKEUP, 1), 1);
54 assert.sameValue(getReport(), i + "ok");
55 }
56
57 function getReport() {
58 var r;
59 while ((r = $262.agent.getReport()) == null)
60 $262.agent.sleep(100);
61 return r;
62 }
63
64 function waitUntil(ia, k, value) {
65 var i = 0;
66 while (Atomics.load(ia, k) !== value && i < 15) {
67 $262.agent.sleep(100);
68 i++;
69 }
70 assert.sameValue(Atomics.load(ia, k), value, "All agents are running");
71 }