atomicsHelper.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright (C) 2017 Mozilla Corporation. All rights reserved.
  2. // This code is governed by the BSD license found in the LICENSE file.
  3. /*---
  4. description: >
  5. Collection of functions used to interact with Atomics.* operations across agent boundaries.
  6. ---*/
  7. /**
  8. * The amount of slack allowed for testing time-related Atomics methods (i.e. wait and notify).
  9. * The absolute value of the difference of the observed time and the expected time must
  10. * be epsilon-close.
  11. */
  12. $262.agent.MAX_TIME_EPSILON = 100;
  13. /**
  14. * @return {String} A report sent from an agent.
  15. */
  16. {
  17. // This is only necessary because the original
  18. // $262.agent.getReport API was insufficient.
  19. //
  20. // All runtimes currently have their own
  21. // $262.agent.getReport which is wrong, so we
  22. // will pave over it with a corrected version.
  23. //
  24. // Binding $262.agent is necessary to prevent
  25. // breaking SpiderMonkey's $262.agent.getReport
  26. let getReport = $262.agent.getReport.bind($262.agent);
  27. $262.agent.getReport = function() {
  28. var r;
  29. while ((r = getReport()) == null) {
  30. $262.agent.sleep(1);
  31. }
  32. return r;
  33. };
  34. }
  35. /**
  36. * With a given Int32Array or BigInt64Array, wait until the expected number of agents have
  37. * reported themselves by calling:
  38. *
  39. * Atomics.add(typedArray, index, 1);
  40. *
  41. * @param {(Int32Array|BigInt64Array)} typedArray An Int32Array or BigInt64Array with a SharedArrayBuffer
  42. * @param {number} index The index of which all agents will report.
  43. * @param {number} expected The number of agents that are expected to report as active.
  44. */
  45. $262.agent.waitUntil = function(typedArray, index, expected) {
  46. var agents = 0;
  47. while ((agents = Atomics.load(typedArray, index)) !== expected) {
  48. /* nothing */
  49. }
  50. assert.sameValue(agents, expected, "Reporting number of 'agents' equals the value of 'expected'");
  51. };
  52. /**
  53. * Timeout values used throughout the Atomics tests. All timeouts are specified in milliseconds.
  54. *
  55. * @property {number} yield Used for `$262.agent.tryYield`. Must not be used in other functions.
  56. * @property {number} small Used when agents will always timeout and `Atomics.wake` is not part
  57. * of the test semantics. Must be larger than `$262.agent.timeouts.yield`.
  58. * @property {number} long Used when some agents may timeout and `Atomics.wake` is called on some
  59. * agents. The agents are required to wait and this needs to be observable
  60. * by the main thread.
  61. * @property {number} huge Used when `Atomics.wake` is called on all waiting agents. The waiting
  62. * must not timeout. The agents are required to wait and this needs to be
  63. * observable by the main thread. All waiting agents must be woken by the
  64. * main thread.
  65. *
  66. * Usage for `$262.agent.timeouts.small`:
  67. * const WAIT_INDEX = 0;
  68. * const RUNNING = 1;
  69. * const TIMEOUT = $262.agent.timeouts.small;
  70. * const i32a = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 2));
  71. *
  72. * $262.agent.start(`
  73. * $262.agent.receiveBroadcast(function(sab) {
  74. * const i32a = new Int32Array(sab);
  75. * Atomics.add(i32a, ${RUNNING}, 1);
  76. *
  77. * $262.agent.report(Atomics.wait(i32a, ${WAIT_INDEX}, 0, ${TIMEOUT}));
  78. *
  79. * $262.agent.leaving();
  80. * });
  81. * `);
  82. * $262.agent.broadcast(i32a.buffer);
  83. *
  84. * // Wait until the agent was started and then try to yield control to increase
  85. * // the likelihood the agent has called `Atomics.wait` and is now waiting.
  86. * $262.agent.waitUntil(i32a, RUNNING, 1);
  87. * $262.agent.tryYield();
  88. *
  89. * // The agent is expected to time out.
  90. * assert.sameValue($262.agent.getReport(), "timed-out");
  91. *
  92. *
  93. * Usage for `$262.agent.timeouts.long`:
  94. * const WAIT_INDEX = 0;
  95. * const RUNNING = 1;
  96. * const NUMAGENT = 2;
  97. * const TIMEOUT = $262.agent.timeouts.long;
  98. * const i32a = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 2));
  99. *
  100. * for (let i = 0; i < NUMAGENT; i++) {
  101. * $262.agent.start(`
  102. * $262.agent.receiveBroadcast(function(sab) {
  103. * const i32a = new Int32Array(sab);
  104. * Atomics.add(i32a, ${RUNNING}, 1);
  105. *
  106. * $262.agent.report(Atomics.wait(i32a, ${WAIT_INDEX}, 0, ${TIMEOUT}));
  107. *
  108. * $262.agent.leaving();
  109. * });
  110. * `);
  111. * }
  112. * $262.agent.broadcast(i32a.buffer);
  113. *
  114. * // Wait until the agents were started and then try to yield control to increase
  115. * // the likelihood the agents have called `Atomics.wait` and are now waiting.
  116. * $262.agent.waitUntil(i32a, RUNNING, NUMAGENT);
  117. * $262.agent.tryYield();
  118. *
  119. * // Wake exactly one agent.
  120. * assert.sameValue(Atomics.wake(i32a, WAIT_INDEX, 1), 1);
  121. *
  122. * // When it doesn't matter how many agents were woken at once, a while loop
  123. * // can be used to make the test more resilient against intermittent failures
  124. * // in case even though `tryYield` was called, the agents haven't started to
  125. * // wait.
  126. * //
  127. * // // Repeat until exactly one agent was woken.
  128. * // var woken = 0;
  129. * // while ((woken = Atomics.wake(i32a, WAIT_INDEX, 1)) !== 0) ;
  130. * // assert.sameValue(woken, 1);
  131. *
  132. * // One agent was woken and the other one timed out.
  133. * const reports = [$262.agent.getReport(), $262.agent.getReport()];
  134. * assert(reports.includes("ok"));
  135. * assert(reports.includes("timed-out"));
  136. *
  137. *
  138. * Usage for `$262.agent.timeouts.huge`:
  139. * const WAIT_INDEX = 0;
  140. * const RUNNING = 1;
  141. * const NUMAGENT = 2;
  142. * const TIMEOUT = $262.agent.timeouts.huge;
  143. * const i32a = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 2));
  144. *
  145. * for (let i = 0; i < NUMAGENT; i++) {
  146. * $262.agent.start(`
  147. * $262.agent.receiveBroadcast(function(sab) {
  148. * const i32a = new Int32Array(sab);
  149. * Atomics.add(i32a, ${RUNNING}, 1);
  150. *
  151. * $262.agent.report(Atomics.wait(i32a, ${WAIT_INDEX}, 0, ${TIMEOUT}));
  152. *
  153. * $262.agent.leaving();
  154. * });
  155. * `);
  156. * }
  157. * $262.agent.broadcast(i32a.buffer);
  158. *
  159. * // Wait until the agents were started and then try to yield control to increase
  160. * // the likelihood the agents have called `Atomics.wait` and are now waiting.
  161. * $262.agent.waitUntil(i32a, RUNNING, NUMAGENT);
  162. * $262.agent.tryYield();
  163. *
  164. * // Wake all agents.
  165. * assert.sameValue(Atomics.wake(i32a, WAIT_INDEX), NUMAGENT);
  166. *
  167. * // When it doesn't matter how many agents were woken at once, a while loop
  168. * // can be used to make the test more resilient against intermittent failures
  169. * // in case even though `tryYield` was called, the agents haven't started to
  170. * // wait.
  171. * //
  172. * // // Repeat until all agents were woken.
  173. * // for (var wokenCount = 0; wokenCount < NUMAGENT; ) {
  174. * // var woken = 0;
  175. * // while ((woken = Atomics.wake(i32a, WAIT_INDEX)) !== 0) ;
  176. * // // Maybe perform an action on the woken agents here.
  177. * // wokenCount += woken;
  178. * // }
  179. *
  180. * // All agents were woken and none timeout.
  181. * for (var i = 0; i < NUMAGENT; i++) {
  182. * assert($262.agent.getReport(), "ok");
  183. * }
  184. */
  185. $262.agent.timeouts = {
  186. yield: 100,
  187. small: 200,
  188. long: 1000,
  189. huge: 10000,
  190. };
  191. /**
  192. * Try to yield control to the agent threads.
  193. *
  194. * Usage:
  195. * const VALUE = 0;
  196. * const RUNNING = 1;
  197. * const i32a = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 2));
  198. *
  199. * $262.agent.start(`
  200. * $262.agent.receiveBroadcast(function(sab) {
  201. * const i32a = new Int32Array(sab);
  202. * Atomics.add(i32a, ${RUNNING}, 1);
  203. *
  204. * Atomics.store(i32a, ${VALUE}, 1);
  205. *
  206. * $262.agent.leaving();
  207. * });
  208. * `);
  209. * $262.agent.broadcast(i32a.buffer);
  210. *
  211. * // Wait until agent was started and then try to yield control.
  212. * $262.agent.waitUntil(i32a, RUNNING, 1);
  213. * $262.agent.tryYield();
  214. *
  215. * // Note: This result is not guaranteed, but should hold in practice most of the time.
  216. * assert.sameValue(Atomics.load(i32a, VALUE), 1);
  217. *
  218. * The default implementation simply waits for `$262.agent.timeouts.yield` milliseconds.
  219. */
  220. $262.agent.tryYield = function() {
  221. $262.agent.sleep($262.agent.timeouts.yield);
  222. };
  223. /**
  224. * Try to sleep the current agent for the given amount of milliseconds. It is acceptable,
  225. * but not encouraged, to ignore this sleep request and directly continue execution.
  226. *
  227. * The default implementation calls `$262.agent.sleep(ms)`.
  228. *
  229. * @param {number} ms Time to sleep in milliseconds.
  230. */
  231. $262.agent.trySleep = function(ms) {
  232. $262.agent.sleep(ms);
  233. };