Os nossos voluntários ainda não traduziram este artigo para Português (Europeu). Junte-se a nós e ajude-nos a fazer o trabalho!
Pode também ler o artigo em English (US).

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).

Properties

Atomics[Symbol.toStringTag]
The value of this property is "Atomics".

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.

Atomics.add()
Adds the provided value to the existing value at the specified index of the array. Returns the old value at that index.
Atomics.and()
Computes a bitwise AND on the value at the specified index of the array with the provided value. Returns the old value at that index.
Atomics.compareExchange()
Stores a value at the specified index of the array, if it equals a value. Returns the old value.
Atomics.exchange()
Stores a value at the specified index of the array. Returns the old value.
Atomics.load()
Returns the value at the specified index of the array.
Atomics.or()
Computes a bitwise OR on the value at the specified index of the array with the provided value. Returns the old value at that index.
Atomics.store()
Stores a value at the specified index of the array. Returns the value.
Atomics.sub()
Subtracts a value at the specified index of the array. Returns the old value at that index.
Atomics.xor()
Computes a bitwise XOR on the value at the specified index of the array with the provided value. Returns the old value at that index.

Wait and wake

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 the specified index of the array still contains a 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()
Wakes up agents that are waiting on the specified index of the array. Returns the number of agents that were woken up.
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
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  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support601 No2

573 4

55 — 57

46 — 555

No No10.1 —?
add601162

573 4

55 — 57

46 — 555

No No10.1 —?
and601162

573 4

55 — 57

46 — 555

No No10.1 —?
compareExchange601162

573 4

55 — 57

46 — 555

No No10.1 —?
exchange601162

573 4

55 — 57

46 — 555

No No10.1 —?
isLockFree601162

573 4

55 — 57

46 — 555

No No10.1 —?
load601162

573 4

55 — 57

46 — 555

No No10.1 —?
or601162

573 4

55 — 57

46 — 555

No No10.1 —?
store601162

573 4

55 — 57

46 — 555

No No10.1 —?
sub601162

573 4

55 — 57

46 — 555

No No10.1 —?
wait601162

573 4

55 — 57

46 — 555

No No10.1 —?
wake601162

573 4

55 — 57

46 — 555

No No10.1 —?
xor601162

573 4

55 — 57

46 — 555

No No10.1 —?
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support601601 ?

573 4

55 — 57

46 — 555

No No No
add601601 ?

573 4

55 — 57

46 — 555

No No No
and601601 ?

573 4

55 — 57

46 — 555

No No No
compareExchange601601 ?

573 4

55 — 57

46 — 555

No No No
exchange601601 ?

573 4

55 — 57

46 — 555

No No No
isLockFree601601 ?

573 4

55 — 57

46 — 555

No No No
load601601 ?

573 4

55 — 57

46 — 555

No No No
or601601 ?

573 4

55 — 57

46 — 555

No No No
store601601 ?

573 4

55 — 57

46 — 555

No No No
sub601601 ?

573 4

55 — 57

46 — 555

No No No
wait601601 ?

573 4

55 — 57

46 — 555

No No No
wake601601 ?

573 4

55 — 57

46 — 555

No No No
xor601601 ?

573 4

55 — 57

46 — 555

No 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.

Compatibility notes

Prior to Firefox 48, the latest API names and semantics weren't implemented yet. The changes between Firefox version 46 and version 48 are:

See also

Etiquetas do documento e contribuidores

Última atualização por: rwaldron,