tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

notify-one.js (2351B)


      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 one waiter if that's what the count is.
      9 includes: [atomicsHelper.js]
     10 features: [Atomics, SharedArrayBuffer, TypedArray]
     11 ---*/
     12 
     13 const WAIT_INDEX = 0;             // Agents wait here
     14 const RUNNING = 1;                // Accounting of live agents here
     15 const NOTIFYCOUNT = 1;
     16 const NUMAGENT = 3;
     17 const BUFFER_SIZE = 4;
     18 
     19 const TIMEOUT = $262.agent.timeouts.long;
     20 
     21 for (var i = 0; i < NUMAGENT; i++ ) {
     22  $262.agent.start(`
     23    $262.agent.receiveBroadcast(function(sab) {
     24      const i32a = new Int32Array(sab);
     25      Atomics.add(i32a, ${RUNNING}, 1);
     26 
     27      // Waiters that are not woken will time out eventually.
     28      $262.agent.report(Atomics.wait(i32a, ${WAIT_INDEX}, 0, ${TIMEOUT}));
     29      $262.agent.leaving();
     30    });
     31  `);
     32 }
     33 
     34 const i32a = new Int32Array(
     35  new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * BUFFER_SIZE)
     36 );
     37 
     38 $262.agent.safeBroadcast(i32a);
     39 
     40 // Wait for agents to be running.
     41 $262.agent.waitUntil(i32a, RUNNING, NUMAGENT);
     42 
     43 // Try to yield control to ensure the agent actually started to wait.
     44 $262.agent.tryYield();
     45 
     46 // There's a slight risk we'll fail to notify the desired count, if the preceding
     47 // tryYield() took much longer than anticipated and workers have started timing
     48 // out.
     49 assert.sameValue(
     50  Atomics.notify(i32a, 0, NOTIFYCOUNT),
     51  NOTIFYCOUNT,
     52  'Atomics.notify(i32a, 0, NOTIFYCOUNT) returns the value of `NOTIFYCOUNT`'
     53 );
     54 
     55 // Try to sleep past the timeout.
     56 $262.agent.trySleep(TIMEOUT);
     57 
     58 // Collect and check results
     59 const reports = [];
     60 for (var i = 0; i < NUMAGENT; i++) {
     61  reports.push($262.agent.getReport());
     62 }
     63 reports.sort();
     64 
     65 for (var i = 0; i < NOTIFYCOUNT; i++) {
     66  assert.sameValue(reports[i], 'ok', 'The value of reports[i] is "ok"');
     67 }
     68 for (var i = NOTIFYCOUNT; i < NUMAGENT; i++) {
     69  assert.sameValue(reports[i], 'timed-out', 'The value of reports[i] is "timed-out"');
     70 }
     71 
     72 reportCompare(0, 0);