waiterlist-order-of-operations-is-fifo.js (2957B)
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, SharedArrayBuffer, TypedArray] 18 ---*/ 19 20 var NUMAGENT = 3; 21 22 var WAIT_INDEX = 0; 23 var RUNNING = 1; 24 var LOCK_INDEX = 2; 25 26 for (var i = 0; i < NUMAGENT; i++) { 27 var agentNum = i; 28 29 $262.agent.start(` 30 $262.agent.receiveBroadcast(function(sab) { 31 const i32a = new Int32Array(sab); 32 Atomics.add(i32a, ${RUNNING}, 1); 33 34 // Synchronize workers before reporting the initial report. 35 while (Atomics.compareExchange(i32a, ${LOCK_INDEX}, 0, 1) !== 0) ; 36 37 // Report the agent number before waiting. 38 $262.agent.report(${agentNum}); 39 40 // Wait until restarted by main thread. 41 var status = Atomics.wait(i32a, ${WAIT_INDEX}, 0); 42 43 // Report wait status. 44 $262.agent.report(status); 45 46 // Report the agent number after waiting. 47 $262.agent.report(${agentNum}); 48 49 $262.agent.leaving(); 50 }); 51 `); 52 } 53 54 const i32a = new Int32Array( 55 new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 4) 56 ); 57 58 $262.agent.safeBroadcast(i32a); 59 60 // Wait until all agents started. 61 $262.agent.waitUntil(i32a, RUNNING, NUMAGENT); 62 63 // Agents may be started in any order. 64 const started = []; 65 for (var i = 0; i < NUMAGENT; i++) { 66 // Wait until an agent entered its critical section. 67 $262.agent.waitUntil(i32a, LOCK_INDEX, 1); 68 69 // Record the agent number. 70 started.push($262.agent.getReport()); 71 72 // The agent may have been interrupted between reporting its initial report 73 // and the `Atomics.wait` call. Try to yield control to ensure the agent 74 // actually started to wait. 75 $262.agent.tryYield(); 76 77 // Now continue with the next agent. 78 Atomics.store(i32a, LOCK_INDEX, 0); 79 } 80 81 // Agents must notify in the order they waited. 82 for (var i = 0; i < NUMAGENT; i++) { 83 var woken = 0; 84 while ((woken = Atomics.notify(i32a, WAIT_INDEX, 1)) === 0) ; 85 86 assert.sameValue(woken, 1, 87 'Atomics.notify(i32a, WAIT_INDEX, 1) returns 1, at index = ' + i); 88 89 assert.sameValue($262.agent.getReport(), 'ok', 90 '$262.agent.getReport() returns "ok", at index = ' + i); 91 92 assert.sameValue($262.agent.getReport(), started[i], 93 '$262.agent.getReport() returns the value of `started[' + i + ']`'); 94 } 95 96 reportCompare(0, 0);