Önkénteseink még nem fordították le ezt a cikket magyar nyelvre. Csatlakozzon, és segítsen a munka befejezésében!
Elolvashatja a cikket English (US) nyelven is.

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 notify

The wait() and notify() 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.notify()
Notifies agents that are waiting on the specified index of the array. Returns the number of agents that were notified.
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 support

68

60 — 632

No3

574 5

55 — 57

46 — 556

No No10.1 —?
add

68

60 — 632

163

574 5

55 — 57

46 — 556

No No10.1 —?
and

68

60 — 632

163

574 5

55 — 57

46 — 556

No No10.1 —?
compareExchange

68

60 — 632

163

574 5

55 — 57

46 — 556

No No10.1 —?
exchange

68

60 — 632

163

574 5

55 — 57

46 — 556

No No10.1 —?
isLockFree

68

60 — 632

163

574 5

55 — 57

46 — 556

No No10.1 —?
load

68

60 — 632

163

574 5

55 — 57

46 — 556

No No10.1 —?
notify

68

60 — 632 8

163 8

634 9

574 5 8

55 — 57

48 — 5510

46 — 4811 12 13

No No10.1 —?8
or

68

60 — 632

163

574 5

55 — 57

46 — 556

No No10.1 —?
store

68

60 — 632

163

574 5

55 — 57

46 — 556

No No10.1 —?
sub

68

60 — 632

163

574 5

55 — 57

46 — 556

No No10.1 —?
wait

68

60 — 632

163

574 5

55 — 57

48 — 5510

46 — 4812 14 15

No No10.1 —?
xor

68

60 — 632

163

574 5

55 — 57

46 — 556

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

574 5

55 — 57

46 — 556

No No No
add60 — 63160 — 631 ?

574 5

55 — 57

46 — 556

No No No
and60 — 63160 — 631 ?

574 5

55 — 57

46 — 556

No No No
compareExchange60 — 63160 — 631 ?

574 5

55 — 57

46 — 556

No No No
exchange60 — 63160 — 631 ?

574 5

55 — 57

46 — 556

No No No
isLockFree60 — 63160 — 631 ?

574 5

55 — 57

46 — 556

No No No
load60 — 63160 — 637 ?

574 5

55 — 57

46 — 556

No No No
notify60 — 631 860 — 631 8 ?

634 9

574 5 8

55 — 57

48 — 5510

46 — 4811 12 13

No No No
or60 — 63160 — 631 ?

574 5

55 — 57

46 — 556

No No No
store60 — 63160 — 631 ?

574 5

55 — 57

46 — 556

No No No
sub60 — 63160 — 637 ?

574 5

55 — 57

46 — 556

No No No
wait60 — 63160 — 631 ?

574 5

55 — 57

48 — 5510

46 — 4812 14 15

No No No
xor60 — 63160 — 631 ?

574 5

55 — 57

46 — 556

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. Chrome disabled SharedArrayBuffer on January 5, 2018 to help reduce the efficacy of speculative side-channel attacks. This was a temporary removal while mitigations were put in place.

3. Support was removed to mitigate speculative execution side-channel attacks (Windows blog).

4. Support was disabled by default to mitigate speculative execution side-channel attacks (Mozilla Security Blog).

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

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

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

8. Supported as wake.

9. From version 63: this feature is behind the javascript.options.shared_memory preference (needs to be set to true). To change preferences in Firefox, visit about:config.

10. From version 48 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.

11. The count parameter defaults to 0 instead of the later-specified +Infinity.

12. From version 46 until version 48 (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.

13. Supported as futexWake.

14. The method returns values Atomics.OK, Atomics.TIMEDOUT, and Atomics.NOTEQUAL, instead of the later-specified strings.

15. Supported as futexWait.

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:

  • The methods Atomics.futexWakeOrRequeue() and Atomics.fence() are now removed entirely (bug 1259544 and bug 1225028).
  • The methods 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.wake() has then been renamed to Atomics.notify() in version 63.
  • The properties 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).

See also

Dokumentum címkék és a közreműködők

Utoljára frissítve: fscholz,