11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_cirequire('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst child_process = require('child_process');
51cb0ef41Sopenharmony_ciconst { Worker } = require('worker_threads');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
81cb0ef41Sopenharmony_ci  const i32arr = new Int32Array(new SharedArrayBuffer(8));
91cb0ef41Sopenharmony_ci  assert.strictEqual(Atomics.wait(i32arr, 0, 1), 'not-equal');
101cb0ef41Sopenharmony_ci  assert.strictEqual(Atomics.wait(i32arr, 0, 0, 10), 'timed-out');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci  new Worker(`
131cb0ef41Sopenharmony_ci  const i32arr = require('worker_threads').workerData;
141cb0ef41Sopenharmony_ci  Atomics.store(i32arr, 1, -1);
151cb0ef41Sopenharmony_ci  Atomics.notify(i32arr, 1);
161cb0ef41Sopenharmony_ci  Atomics.wait(i32arr, 1, -1);
171cb0ef41Sopenharmony_ci  `, { eval: true, workerData: i32arr });
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  Atomics.wait(i32arr, 1, 0);
201cb0ef41Sopenharmony_ci  assert.strictEqual(Atomics.load(i32arr, 1), -1);
211cb0ef41Sopenharmony_ci  Atomics.store(i32arr, 1, 0);
221cb0ef41Sopenharmony_ci  Atomics.notify(i32arr, 1);
231cb0ef41Sopenharmony_ci  return;
241cb0ef41Sopenharmony_ci}
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciconst proc = child_process.spawnSync(
271cb0ef41Sopenharmony_ci  process.execPath,
281cb0ef41Sopenharmony_ci  [ '--trace-atomics-wait', __filename, 'child' ],
291cb0ef41Sopenharmony_ci  { encoding: 'utf8', stdio: [ 'inherit', 'inherit', 'pipe' ] });
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciif (proc.status !== 0) console.log(proc);
321cb0ef41Sopenharmony_ciassert.strictEqual(proc.status, 0);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciconst SABAddress = proc.stderr.match(/Atomics\.wait\((?<SAB>.+) \+/).groups.SAB;
351cb0ef41Sopenharmony_ciconst actualTimeline = proc.stderr
361cb0ef41Sopenharmony_ci  .replace(new RegExp(SABAddress, 'g'), '<address>')
371cb0ef41Sopenharmony_ci  .replace(new RegExp(`\\(node:${proc.pid}\\) `, 'g'), '')
381cb0ef41Sopenharmony_ci  .replace(/\binf(inity)?\b/gi, 'inf')
391cb0ef41Sopenharmony_ci  .replace(/\r/g, '')
401cb0ef41Sopenharmony_ci  .trim();
411cb0ef41Sopenharmony_ciconsole.log('+++ normalized stdout +++');
421cb0ef41Sopenharmony_ciconsole.log(actualTimeline);
431cb0ef41Sopenharmony_ciconsole.log('--- normalized stdout ---');
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciconst begin =
461cb0ef41Sopenharmony_ci`[Thread 0] Atomics.wait(<address> + 0, 1, inf) started
471cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 0, 1, inf) did not wait because the \
481cb0ef41Sopenharmony_civalues mismatched
491cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 0, 0, 10) started
501cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 0, 0, 10) timed out`;
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciconst expectedTimelines = [
531cb0ef41Sopenharmony_ci  `${begin}
541cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) started
551cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) started
561cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread
571cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) was woken up by another thread`,
581cb0ef41Sopenharmony_ci  `${begin}
591cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, 0, inf) started
601cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, -1, inf) started
611cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread
621cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) was woken up by another thread`,
631cb0ef41Sopenharmony_ci  `${begin}
641cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) started
651cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread
661cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) started
671cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) was woken up by another thread`,
681cb0ef41Sopenharmony_ci  `${begin}
691cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) started
701cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread
711cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) started
721cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) did not wait because the \
731cb0ef41Sopenharmony_civalues mismatched`,
741cb0ef41Sopenharmony_ci  `${begin}
751cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) started
761cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) started
771cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread
781cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) did not wait because the \
791cb0ef41Sopenharmony_civalues mismatched`,
801cb0ef41Sopenharmony_ci  `${begin}
811cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, 0, inf) started
821cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, -1, inf) started
831cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) was woken up by another thread
841cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) did not wait because the \
851cb0ef41Sopenharmony_civalues mismatched`,
861cb0ef41Sopenharmony_ci  `${begin}
871cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) started
881cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) did not wait because the \
891cb0ef41Sopenharmony_civalues mismatched
901cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) started
911cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) did not wait because the \
921cb0ef41Sopenharmony_civalues mismatched`,
931cb0ef41Sopenharmony_ci  `${begin}
941cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) started
951cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) started
961cb0ef41Sopenharmony_ci[Thread 0] Atomics.wait(<address> + 4, 0, inf) did not wait because the \
971cb0ef41Sopenharmony_civalues mismatched
981cb0ef41Sopenharmony_ci[Thread 1] Atomics.wait(<address> + 4, -1, inf) was woken up by another thread`,
991cb0ef41Sopenharmony_ci];
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ciassert(expectedTimelines.includes(actualTimeline));
102