Atomics.hx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package js.lib;
  2. private typedef E<A, B> = haxe.extern.EitherType<A, B>;
  3. private typedef IntTypedArray = E<Int8Array, E<Uint8Array, E<Int16Array, E<Uint16Array, E<Int32Array, Uint32Array>>>>>;
  4. /**
  5. The Atomics object provides atomic operations as static methods. They are used with SharedArrayBuffer and ArrayBuffer objects.
  6. Documentation [Atomics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics) by [Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics/contributors.txt), licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
  7. **/
  8. @:native("Atomics")
  9. extern class Atomics {
  10. /**
  11. Adds the provided value to the existing value at the specified index of the array.
  12. Returns the old value at that index.
  13. This atomic operation guarantees that no other write happens until the modified value is written back.
  14. **/
  15. static function add(typedArray:IntTypedArray, index:Int, value:Int):Int;
  16. /**
  17. Computes a bitwise AND on the value at the specified index of the array with the provided value.
  18. Returns the old value at that index.
  19. This atomic operation guarantees that no other write happens until the modified value is written back.
  20. **/
  21. static function and(typedArray:IntTypedArray, index:Int, value:Int):Int;
  22. /**
  23. Stores a value at the specified index of the array, if it equals a value.
  24. Returns the old value.
  25. This atomic operation guarantees that no other write happens until the modified value is written back.
  26. **/
  27. static function compareExchange(typedArray:IntTypedArray, index:Int, expectedValue:Int, replacementValue:Int):Int;
  28. /**
  29. Stores a value at the specified index of the array.
  30. Returns the old value.
  31. This atomic operation guarantees that no other write happens until the modified value is written back.
  32. **/
  33. static function exchange(typedArray:IntTypedArray, index:Int, value:Int):Int;
  34. /**
  35. An optimization primitive that can be used to determine whether to use locks or atomic operations.
  36. Returns `true` if an atomic operation on arrays of the given element size will be implemented using a hardware atomic operation (as opposed to a lock). Experts only.
  37. **/
  38. static function isLockFree(size:Int):Bool;
  39. /**
  40. Returns the value at the specified index of the array.
  41. This atomic operation guarantees that no other write happens until the modified value is written back.
  42. **/
  43. static function load(typedArray:IntTypedArray, index:Int):Int;
  44. /**
  45. Notifies agents that are waiting on the specified index of the array.
  46. Returns the number of agents that were notified.
  47. **/
  48. static function notify(typedArray:IntTypedArray, index:Int, ?count:Int):Int;
  49. /**
  50. Computes a bitwise OR on the value at the specified index of the array with the provided value.
  51. Returns the old value at that index.
  52. This atomic operation guarantees that no other write happens until the modified value is written back.
  53. **/
  54. static function or(typedArray:IntTypedArray, index:Int, value:Int):Int;
  55. /**
  56. Stores a value at the specified index of the array.
  57. Returns the value.
  58. This atomic operation guarantees that no other write happens until the modified value is written back.
  59. **/
  60. static function store(typedArray:IntTypedArray, index:Int, value:Int):Int;
  61. /**
  62. Subtracts a value at the specified index of the array.
  63. Returns the old value at that index.
  64. This atomic operation guarantees that no other write happens until the modified value is written back.
  65. **/
  66. static function sub(typedArray:IntTypedArray, index:Int, value:Int):Int;
  67. /**
  68. Verifies that the specified index of the array still contains a value and sleeps awaiting or times out.
  69. Returns either "ok", "not-equal", or "timed-out". If waiting is not allowed in the calling agent then it throws an Error exception.
  70. Most browsers will not allow wait() on the browser's main thread.)
  71. **/
  72. static function wait(typedArray:Int32Array, index:Int, value:Int, ?timeout:Int):WaitValue;
  73. /**
  74. Computes a bitwise XOR on the value at the specified index of the array with the provided value.
  75. Returns the old value at that index.
  76. This atomic operation guarantees that no other write happens until the modified value is written back.
  77. **/
  78. static function xor(typedArray:IntTypedArray, index:Int, value:Int):Int;
  79. }
  80. enum abstract WaitValue(String) {
  81. var OK = "ok";
  82. var NotEqual = "not-equal";
  83. var TimedOut = "timed-out";
  84. }