no-spurious-wakeup-no-operation.js (2686B)
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 /*--- 6 esid: sec-atomics.waitasync 7 description: > 8 Test that Atomics.waitAsync returns the right result when it timed out and that 9 the time to time out is reasonable. 10 info: | 11 AddWaiter ( WL, waiterRecord ) 12 13 5. Append waiterRecord as the last element of WL.[[Waiters]] 14 6. If waiterRecord.[[Timeout]] is finite, then in parallel, 15 a. Wait waiterRecord.[[Timeout]] milliseconds. 16 b. Perform TriggerTimeout(WL, waiterRecord). 17 18 TriggerTimeout( WL, waiterRecord ) 19 20 3. If waiterRecord is in WL.[[Waiters]], then 21 a. Set waiterRecord.[[Result]] to "timed-out". 22 b. Perform RemoveWaiter(WL, waiterRecord). 23 c. Perform NotifyWaiter(WL, waiterRecord). 24 4. Perform LeaveCriticalSection(WL). 25 26 flags: [async] 27 includes: [atomicsHelper.js] 28 features: [Atomics.waitAsync, SharedArrayBuffer, TypedArray, Atomics, arrow-function, async-functions] 29 ---*/ 30 assert.sameValue(typeof Atomics.waitAsync, 'function', 'The value of `typeof Atomics.waitAsync` is "function"'); 31 32 const RUNNING = 1; 33 const TIMEOUT = $262.agent.timeouts.small; 34 35 const i32a = new Int32Array( 36 new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 4) 37 ); 38 39 $262.agent.start(` 40 $262.agent.receiveBroadcast(async (sab) => { 41 const i32a = new Int32Array(sab); 42 Atomics.add(i32a, ${RUNNING}, 1); 43 44 const before = $262.agent.monotonicNow(); 45 const unpark = await Atomics.waitAsync(i32a, 0, 0, ${TIMEOUT}).value; 46 const duration = $262.agent.monotonicNow() - before; 47 48 $262.agent.report(duration); 49 $262.agent.report(unpark); 50 $262.agent.leaving(); 51 }); 52 `); 53 54 $262.agent.safeBroadcastAsync(i32a, RUNNING, 1).then(async (agentCount) => { 55 56 assert.sameValue(agentCount, 1, 'The value of `agentCount` is 1'); 57 58 const lapse = await $262.agent.getReportAsync(); 59 60 assert( 61 lapse >= TIMEOUT, 62 'The result of evaluating `(lapse >= TIMEOUT)` is true' 63 ); 64 65 const result = await $262.agent.getReportAsync(); 66 67 assert.sameValue( 68 result, 69 'timed-out', 70 'The value of `result` is "timed-out"' 71 ); 72 assert.sameValue(Atomics.notify(i32a, 0), 0, 'Atomics.notify(new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 4)), 0) must return 0'); 73 }).then($DONE, $DONE);