From 73be21272daf163f8c6a5895acb44da79105ee24 Mon Sep 17 00:00:00 2001
From: Rick Waldron <waldron.rick@gmail.com>
Date: Mon, 25 Jun 2018 17:02:40 -0400
Subject: [PATCH] Atomics: wake/wake-in-order.js, capture waiterlist order for
 wake comparison

---
 harness/atomicsHelper.js                     |  8 +-
 test/built-ins/Atomics/wake/wake-in-order.js | 79 ++++++++++++--------
 2 files changed, 52 insertions(+), 35 deletions(-)

diff --git a/harness/atomicsHelper.js b/harness/atomicsHelper.js
index e14e5df976..e26fab043e 100644
--- a/harness/atomicsHelper.js
+++ b/harness/atomicsHelper.js
@@ -36,13 +36,11 @@ function getReport() {
  * @param {Number} expected The number of agents that are expected to report as active.
  */
 function waitUntil(i32a, index, expected) {
-  var i = 0;
-  while (Atomics.load(i32a, index) !== expected  && i < 15) {
-    $262.agent.sleep(10);
-    i++;
+  while (Atomics.load(i32a, index) !== expected) {
+    /* nothing */
   }
   const agents = Atomics.load(i32a, index);
-  assert.sameValue(agents, expected , `'agents' equals the value of expected  (${expected })`);
+  assert.sameValue(agents, expected, `'agents' equals the value of 'expected' (${expected})`);
 }
 
 
diff --git a/test/built-ins/Atomics/wake/wake-in-order.js b/test/built-ins/Atomics/wake/wake-in-order.js
index 7a5faedc6d..0b81dda43f 100644
--- a/test/built-ins/Atomics/wake/wake-in-order.js
+++ b/test/built-ins/Atomics/wake/wake-in-order.js
@@ -19,40 +19,59 @@ const NUMELEM = RUNNING + 1;
 // them go into a wait, thus controlling the waiting order.  Then we wake them
 // one by one and observe the wakeup order.
 
-for (var i = 0; i < NUMAGENT; i++) {
-  $262.agent.start(`
-    $262.agent.receiveBroadcast(function(sab) {
-      const i32a = new Int32Array(sab);
-      Atomics.add(i32a, ${RUNNING}, 1);
-      while (Atomics.load(i32a, ${SPIN + i}) === 0)
-          /* nothing */ ;
-      $262.agent.report(${i} + Atomics.wait(i32a, ${WAKEUP}, 0));
-      $262.agent.leaving();
-    });
-  `);
-}
-
-const i32a = new Int32Array(
-  new SharedArrayBuffer(NUMELEM * Int32Array.BYTES_PER_ELEMENT)
-);
+for (var attempt = 0; attempt < 10; attempt++) {
+  for (var i = 0; i < NUMAGENT; i++) {
+    $262.agent.start(`
+      $262.agent.receiveBroadcast(function(sab) {
+        const i32a = new Int32Array(sab);
+        Atomics.add(i32a, ${RUNNING}, 1);
+        while (Atomics.load(i32a, ${SPIN + i}) === 0) {
+          /* nothing */
+        }
+        $262.agent.report(${i});
+        Atomics.wait(i32a, ${WAKEUP}, 0);
+        $262.agent.report(${i});
+        $262.agent.leaving();
+      });
+    `);
+  }
 
-$262.agent.broadcast(i32a.buffer);
+  const i32a = new Int32Array(
+    new SharedArrayBuffer(NUMELEM * Int32Array.BYTES_PER_ELEMENT)
+  );
 
-// Wait for agents to be running.
-waitUntil(i32a, RUNNING, NUMAGENT);
+  $262.agent.broadcast(i32a.buffer);
 
-// Sleep to allow the agents a fair chance to wait. If we don't,
-// we risk sending the wakeup before agents are sleeping, and we hang.
-$262.agent.sleep(50);
+  // Wait for agents to be running.
+  waitUntil(i32a, RUNNING, NUMAGENT);
 
-// Make them sleep in order 0 1 2 on i32a[0]
-for (var i = 0; i < NUMAGENT; i++) {
-  Atomics.store(i32a, SPIN + i, 1);
+  // Sleep to allow the agents a fair chance to wait. If we don't,
+  // we risk sending the wakeup before agents are sleeping, and we hang.
   $262.agent.sleep(50);
-}
 
-// Wake them up one at a time and check the order is 0 1 2
-for (var i = 0; i < NUMAGENT; i++) {
-  assert.sameValue(Atomics.wake(i32a, WAKEUP, 1), 1, 'Atomics.wake(i32a, WAKEUP, 1) returns 1');
-  assert.sameValue(getReport(), i + 'ok', 'getReport() returns i + "ok"');
+  var waiterlist = [];
+  assert.sameValue(Atomics.store(i32a, SPIN + 0, 1), 1);
+  waiterlist.push(getReport());
+
+  assert.sameValue(Atomics.store(i32a, SPIN + 1, 1), 1);
+  waiterlist.push(getReport());
+
+  assert.sameValue(Atomics.store(i32a, SPIN + 2, 1), 1);
+  waiterlist.push(getReport());
+
+  var notified = [];
+  assert.sameValue(Atomics.wake(i32a, WAKEUP, 1), 1);
+  notified.push(getReport());
+
+  assert.sameValue(Atomics.wake(i32a, WAKEUP, 1), 1);
+  notified.push(getReport());
+
+  assert.sameValue(Atomics.wake(i32a, WAKEUP, 1), 1);
+  notified.push(getReport());
+
+  assert.sameValue(
+    notified.join(''),
+    waiterlist.join(''),
+    `Attempt #${attempt}: notified and waiterlist order do not match.`
+  );
 }
-- 
GitLab