atomics-waitasync-1thread-timeouts-and-no-timeouts.js 1.84 KB
Newer Older
1 2 3 4
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --harmony-sharedarraybuffer --harmony-atomics-waitasync
6

7 8
const N = 10;

9
function workerCode(N) {
10 11
  const sab = new SharedArrayBuffer(16);
  const i32a = new Int32Array(sab);
12 13
  const location = 0;
  const expected_value = 0;
14

15 16 17 18 19 20 21 22 23 24 25 26 27
  function start() {
    // Create N async waiters; the even ones without timeout and the odd ones
    // with timeout.
    for (let i = 0; i < N; ++i) {
      let result;
      if (i % 2 == 0) {
        result = Atomics.waitAsync(i32a, location, expected_value);
      } else {
        result = Atomics.waitAsync(i32a, location, expected_value, i);
      }
      result.value.then(
        (value) => { postMessage(value + " " + i); },
        () => { postMessage("unexpected"); });
28 29 30
    }
  }

31
  function wakeUpRemainingWaiters() {
32
    // Wake up all waiters
33 34
    let notify_return_value = Atomics.notify(i32a, location);
    postMessage("notify return value " + notify_return_value);
35 36
  }

37
  onmessage = function(param) {
38 39 40 41
    if (param == "start") {
      start();
    } else if (param == "wakeUpRemainingWaiters") {
      wakeUpRemainingWaiters();
42
    }
43 44
  };
}
45

46
const w = new Worker(workerCode, {type: 'function', arguments: [N]});
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
w.postMessage("start");

// Verify that all timed out waiters timed out in timeout order.
let waiter_no = 1;
for (let i = 0; i < N / 2; ++i) {
  const m = w.getMessage();
  assertEquals("timed-out " + waiter_no, m);
  waiter_no += 2;
}
w.postMessage("wakeUpRemainingWaiters");
const m = w.getMessage();
assertEquals("notify return value " + N / 2, m);

// Verify that the waiters woke up in FIFO order.
waiter_no = 0;
for (let i = 0; i < N / 2; ++i) {
  const m = w.getMessage();
  assertEquals("ok " + waiter_no, m);
  waiter_no += 2;
}
w.terminate();