The Atomics
object provides atomic operations as static methods. They are used with SharedArrayBuffer
objects.
The Atomic operations are installed on an Atomics
module. Unlike the other global objects, Atomics
is not a constructor. You cannot use it with a new
operator or invoke the Atomics
object as a function. All properties and methods of Atomics
are static (as is the case with the Math
object, for example).
Atomics[Symbol.toStringTag]
When memory is shared, multiple threads can read and write the same data in memory. Atomic operations make sure that predictable values are written and read, that operations are finished before the next operation starts and that operations are not interrupted.
Atomics.add()
Atomics.and()
Atomics.compareExchange()
Atomics.exchange()
Atomics.load()
Atomics.or()
Atomics.store()
Atomics.sub()
Atomics.xor()
The wait()
and wake()
methods are modeled on Linux futexes ("fast user-space mutex") and provide ways for waiting until a certain condition becomes true and are typically used as blocking constructs.
Atomics.wait()
Verifies that a given position in the array still contains a given value and sleeps awaiting or times out. Returns either "ok"
, "not-equal"
, or "timed-out"
. If waiting is not allowed in the calling agent then it throws an Error exception (most browsers will not allow wait()
on the browser's main thread).
Atomics.wake()
Atomics.isLockFree(size)
An optimization primitive that can be used to determine whether to use locks or atomic operations. 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.
Specification | Status | Comment |
---|---|---|
ECMAScript Latest Draft (ECMA-262) The definition of 'Atomics' in that specification. | Draft | Initial definition in ES2017. |
ECMAScript 2017 (ECMA-262) The definition of 'Atomics' in that specification. | Standard |
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
add |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
and |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
compareExchange |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
exchange |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
isLockFree |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
load |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
or |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
store |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
sub |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
wait |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
wake |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
xor |
601 | No2 |
55 — 57 46 — 555 |
No | No | 10.1 —? |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | 601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
add |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
and |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
compareExchange |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
exchange |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
isLockFree |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
load |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
or |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
store |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
sub |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
wait |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
wake |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
xor |
601 | 601 | ? |
55 — 57 46 — 555 |
No | No | ? |
1. Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This is intended as a temporary measure until other mitigations are in place.
2. Support was removed to mitigate speculative execution side-channel attacks (Windows blog).
3. Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog).
4. From version 57: this feature is behind the javascript.options.shared_memory
preference (needs to be set to true
). To change preferences in Firefox, visit about:config.
5. From version 46 until version 55 (exclusive): this feature is behind the javascript.options.shared_memory
preference (needs to be set to true
). To change preferences in Firefox, visit about:config.
Prior to Firefox 48, the latest API names and semantics weren't implemented yet. The changes between Firefox version 46 and version 48 are:
Atomics.futexWakeOrRequeue()
and Atomics.fence()
are now removed entirely (bug 1259544 and bug 1225028).Atomics.wait()
and Atomics.wake()
were named Atomics.futexWait()
and Atomics.futexWake()
(bug 1260910). Note: The old names have been removed in version 49 and later (bug 1262062).Atomics.OK
, Atomics.TIMEDOUT
, Atomics.NOTEQUAL
have been removed. The Atomics.wait()
method now returns the strings "ok", "timed-out" and "not-equal" (bug 1260835).The count
parameter of the Atomics.wake()
method has been changed: it now defaults to +Infinity
, not 0
(bug 1253350).
ArrayBuffer
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Atomics