The Atomics
object provides atomic operations as static methods. They are used with {{jsxref("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 {{jsxref("Math")}} object, for example).
Properties
Atomics.OK
- A value property whose value is 0.
Atomics.NOTEQUAL
- A value property whose value is -1.
Atomics.TIMEDOUT
- A value property whose value -2.
Methods
Atomic operations
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.
- {{jsxref("Atomics.add()")}}
- Adds a given value at a given position in the array. Returns the old value at that position.
- {{jsxref("Atomics.and()")}}
- Computes a bitwise AND at a given position in the array. Returns the old value at that position.
- {{jsxref("Atomics.compareExchange()")}}
- Stores a given value at a given position in the array, if it equals a given value. Returns the old value.
- {{jsxref("Atomics.exchange()")}}
- Stores a given value at a given position in the array. Returns the old value.
- {{jsxref("Atomics.load()")}}
- Returns the value at the given position in the array.
- {{jsxref("Atomics.or()")}}
- Computes a bitwise OR at a given position in the array. Returns the old value at that position.
- {{jsxref("Atomics.store()")}}
- Stores a given value at the given position in the array. Returns the value.
- {{jsxref("Atomics.sub()")}}
- Subtracts a given value at a given position in the array. Returns the old value at that position.
- {{jsxref("Atomics.xor()")}}
- Computes a bitwise XOR at a given position in the array. Returns the old value at that position.
Fast user-space mutex (Futex)
Futex methods (named from Linux, where it means "fast user-space mutex") provide ways for waiting until a certain condition becomes true and are typically used as blocking constructs.
- {{jsxref("Atomics.futexWait()")}}
-
Verifies that a given position in the array still contains a given value and sleeps awaiting or times out. Returns one of the Atomic properties
OK
,NOTEQUAL
, orTIMEDOUT
. If waiting is not allowed in the calling agent then it throws an Error exception. (Most browsers will not allow futexWait() on the browser's main thread.) - {{jsxref("Atomics.futexWake()")}}
- Wakes up some agents that are sleeping in the wait queue on the given array position. Returns the number of agents that were woken up.
- {{jsxref("Atomics.futexWakeOrRequeue()")}}
- An optimization primitive that wakes up some agents that are sleeping in the wait queue on the given position, and moves the rest of the waiting agents on that position to the back of the queue on a different position. Experts only.
- {{jsxref("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.
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('Shared Memory', '#AtomicsObject', 'Atomics')}} | {{Spec2('Shared Memory')}} | Initial definition. |
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | {{CompatNo}} [2] | {{CompatNo}} | {{CompatGeckoDesktop("46")}} [1] | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatNo}} | {{CompatGeckoMobile("46")}} [1] | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
[1] This feature is disabled by a preference setting. In about:config, set javascript.options.shared_memory
to true
.
[2] The implementation is under development and needs these runtime flags: --js-flags=--harmony-sharedarraybuffer --enable-blink-feature=SharedArrayBuffer
See also
- {{jsxref("ArrayBuffer")}}
- JavaScript typed arrays
- Web Workers
- parlib-simple – a simple library providing synchronization and work distribution abstractions.
- Shared Memory – a brief tutorial