From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/atomics/add/index.html | 84 ++++++++++++ .../global_objects/atomics/and/index.html | 130 ++++++++++++++++++ .../atomics/compareexchange/index.html | 86 ++++++++++++ .../global_objects/atomics/exchange/index.html | 85 ++++++++++++ .../reference/global_objects/atomics/index.html | 151 +++++++++++++++++++++ .../global_objects/atomics/islockfree/index.html | 69 ++++++++++ .../global_objects/atomics/load/index.html | 76 +++++++++++ .../global_objects/atomics/notify/index.html | 93 +++++++++++++ .../reference/global_objects/atomics/or/index.html | 129 ++++++++++++++++++ .../global_objects/atomics/store/index.html | 77 +++++++++++ .../global_objects/atomics/sub/index.html | 82 +++++++++++ .../global_objects/atomics/wait/index.html | 95 +++++++++++++ .../global_objects/atomics/xor/index.html | 126 +++++++++++++++++ 13 files changed, 1283 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/add/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/and/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/compareexchange/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/exchange/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/islockfree/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/load/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/notify/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/or/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/store/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/sub/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/wait/index.html create mode 100644 files/zh-cn/web/javascript/reference/global_objects/atomics/xor/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/atomics') diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/add/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/add/index.html new file mode 100644 index 0000000000..256ddc4019 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/add/index.html @@ -0,0 +1,84 @@ +--- +title: Atomics.add() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/add +tags: + - Atomics + - JavaScript + - Shared Memory + - 共享内存 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/add +--- +
{{JSRef}}
+ +

Atomics.add() 静态方法会将给定的值加到数组里的某个特定位置上,并返回该位置的旧值。此原子操作保证在写上修改的值之前不会发生其他写操作。

+ +
{{EmbedInteractiveExample("pages/js/atomics-add.html")}}
+ + + +

语法

+ +
Atomics.add(typedArray, index, value)
+
+ +

参数

+ +
+
typedArray
+
一个共享的整型 typed array。例如 {{jsxref("Int8Array")}},{{jsxref("Uint8Array")}},{{jsxref("Int16Array")}},{{jsxref("Uint16Array")}},{{jsxref("Int32Array")}},或者 {{jsxref("Uint32Array")}}。
+
index
+
typedArray 中的位置,该位置数值会被加总并更新。
+
value
+
增加的数字。
+
+ +

返回值

+ +

给定位置的旧值(typedArray[index])

+ +

错误

+ + + +

示例

+ +
var sab = new SharedArrayBuffer(1024);
+var ta = new Uint8Array(sab);
+
+Atomics.add(ta, 0, 12); // returns 0, the old value
+Atomics.load(ta, 0); // 12
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-atomics.add', 'Atomics.add')}}{{Spec2('ESDraft')}}Initial definition in ES2017.
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.Atomics.add")}}

+ +

相关

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/and/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/and/index.html new file mode 100644 index 0000000000..87754093bc --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/and/index.html @@ -0,0 +1,130 @@ +--- +title: Atomics.and() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/and +tags: + - Atomics + - JavaScript + - 共享内存 + - 实验性 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/and +--- +
{{JSRef}} {{SeeCompatTable}}
+ +

Atomics.and() 静态方法会将给定的值与数组上的值进行按位与操作,并将结果赋值给数组,然后返回数组该位置上的旧值。此原子操作保证在写上修改的值之前不会发生其他写操作。

+ +
{{EmbedInteractiveExample("pages/js/atomics-and.html")}}
+ + + +

语法

+ +
Atomics.and(typedArray, index, value)
+
+ +

参数

+ +
+
typedArray
+
一个共享的整型 typed array。例如 {{jsxref("Int8Array")}},{{jsxref("Uint8Array")}},{{jsxref("Int16Array")}},{{jsxref("Uint16Array")}},{{jsxref("Int32Array")}},或 {{jsxref("Uint32Array")}}。
+
index
+
按位与操作的 typedArray 的值在数组上的索引。
+
value
+
给定的按位与操作的值。
+
+ +

返回值

+ +

给定位置的旧值(typedArray[index])。

+ +

错误

+ + + +

描述

+ +

假如 a 和 b 都是 1,那么按位与运算( a & b)仅产生1。与操作的真值表为:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
aba & b
000
010
100
111
+ +

比如,一个按位与如 5 & 1 的结果是 0001,其十进制就是1

+ +
5  0101
+1  0001
+   ----
+1  0001
+ +

示例

+ +
var sab = new SharedArrayBuffer(1024);
+var ta = new Uint8Array(sab);
+ta[0] = 5;
+
+Atomics.and(ta, 0, 1); // returns 0, the old value
+Atomics.load(ta, 0);  // 1
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Shared Memory', '#Atomics.and', 'Atomics.and')}}{{Spec2('Shared Memory')}}Initial definition.
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.Atomics.and")}}

+ +

相关

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/compareexchange/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/compareexchange/index.html new file mode 100644 index 0000000000..1883180411 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/compareexchange/index.html @@ -0,0 +1,86 @@ +--- +title: Atomics.compareExchange() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/compareExchange +tags: + - Atomics + - JavaScript + - 共享内存 + - 方法 +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/compareExchange +--- +
{{JSRef}} {{SeeCompatTable}}
+ +

Atomics.compareExchange() 静态方法会在数组的值与期望值相等的时候,将给定的替换值替换掉数组上的值,然后返回旧值。此原子操作保证在写上修改的值之前不会发生其他写操作。

+ +
{{EmbedInteractiveExample("pages/js/atomics-compareexchange.html")}}
+ + + +

语法

+ +
Atomics.compareExchange(typedArray, index, expectedValue, replacementValue)
+
+ +

参数

+ +
+
typedArray
+
一个共享的整型 typed array。例如 {{jsxref("Int8Array")}},{{jsxref("Uint8Array")}},{{jsxref("Int16Array")}},{{jsxref("Uint16Array")}},{{jsxref("Int32Array")}},或 {{jsxref("Uint32Array")}}。
+
index
+
typedArray 的索引。
+
expectedValue
+
用于比较的值。
+
replacementValue
+
将要替换上的值。
+
+ +

返回值

+ +

给定位置的旧值(typedArray[index]).

+ +

错误

+ + + +

示例

+ +
var sab = new SharedArrayBuffer(1024);
+var ta = new Uint8Array(sab);
+ta[0] = 7;
+
+Atomics.compareExchange(ta, 0, 7, 12); // returns 7, the old value
+Atomics.load(ta, 0); // 12
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Shared Memory', '#Atomics.compareExchange', 'Atomics.compareExchange')}}{{Spec2('Shared Memory')}}Initial definition.
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.Atomics.compareExchange")}}

+ +

相关

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/exchange/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/exchange/index.html new file mode 100644 index 0000000000..4502bf8a52 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/exchange/index.html @@ -0,0 +1,85 @@ +--- +title: Atomics.exchange() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/exchange +tags: + - Atmoics + - Experimental + - JavaScript + - Method + - 共享内存 + - 实验性 +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/exchange +--- +
{{JSRef}} {{SeeCompatTable}}
+ +

Atomics.exchange() 静态方法会用给定的值替换掉数组上的值,然后返回数组的旧值。此原子操作保证在写上修改的值之前不会发生其他写操作。

+ +
{{EmbedInteractiveExample("pages/js/atomics-exchange.html")}}
+ + + +

语法

+ +
Atomics.exchange(typedArray, index, value)
+
+ +

参数

+ +
+
typedArray
+
一个共享的整型 typed array。例如 {{jsxref("Int8Array")}},{{jsxref("Uint8Array")}},{{jsxref("Int16Array")}},{{jsxref("Uint16Array")}},{{jsxref("Int32Array")}},或 {{jsxref("Uint32Array")}}。
+
index
+
被替换的 typedArray 值的索引。
+
value
+
去替换的值。
+
+ +

返回值

+ +

给定位置的旧值(typedArray[index]).

+ +

错误

+ + + +

示例

+ +
var sab = new SharedArrayBuffer(1024);
+var ta = new Uint8Array(sab);
+
+Atomics.exchange(ta, 0, 12); // returns 0, the old value
+Atomics.load(ta, 0); // 12
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Shared Memory', '#Atomics.exchange', 'Atomics.exchange')}}{{Spec2('Shared Memory')}}Initial definition.
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.Atomics.exchange")}}

+ +

相关

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/index.html new file mode 100644 index 0000000000..4c80b3f868 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/index.html @@ -0,0 +1,151 @@ +--- +title: Atomics +slug: Web/JavaScript/Reference/Global_Objects/Atomics +tags: + - JavaScript +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics +--- +

{{JSRef}}

+ +

Atomics 对象提供了一组静态方法对 {{jsxref("SharedArrayBuffer")}} 和  {{jsxref("ArrayBuffer")}} 对象进行原子操作。

+ +

描述

+ +

这些原子操作属于 Atomics 模块。与一般的全局对象不同,Atomics 不是构造函数,因此不能使用 new 操作符调用,也不能将其当作函数直接调用。Atomics 的所有属性和方法都是静态的(与 {{jsxref("Math")}}  对象一样)。

+ +

原子操作

+ +

多个共享内存的线程能够同时读写同一位置上的数据。原子操作会确保正在读或写的数据的值是符合预期的,即下一个原子操作一定会在上一个原子操作结束后才会开始,其操作过程不会中断。

+ +

等待和通知

+ +

wait() 和 notify() 方法采用的是 Linux 上的 futexes 模型(“快速用户空间互斥量”),可以让进程一直等待直到某个特定的条件为真,主要用于实现阻塞。

+ +

方法

+ +
+
 
+
{{jsxref("Atomics.add()")}}
+
将指定位置上的数组元素与给定的值相加,并返回相加前该元素的值。
+
{{jsxref("Atomics.and()")}}
+
将指定位置上的数组元素与给定的值相与,并返回与操作前该元素的值。
+
{{jsxref("Atomics.compareExchange()")}}
+
如果数组中指定的元素与给定的值相等,则将其更新为新的值,并返回该元素原先的值。
+
{{jsxref("Atomics.exchange()")}}
+
将数组中指定的元素更新为给定的值,并返回该元素更新前的值。
+
{{jsxref("Atomics.isLockFree()", "Atomics.isLockFree(size)")}}
+
可以用来检测当前系统是否支持硬件级的原子操作。对于指定大小的数组,如果当前系统支持硬件级的原子操作,则返回 true;否则就意味着对于该数组,Atomics 对象中的各原子操作都只能用锁来实现。此函数面向的是技术专家。
+
{{jsxref("Atomics.load()")}}
+
返回数组中指定元素的值。
+
{{jsxref("Atomics.notify()")}}
+
唤醒等待队列中正在数组指定位置的元素上等待的线程。返回值为成功唤醒的线程数量。
+
{{jsxref("Atomics.or()")}}
+
将指定位置上的数组元素与给定的值相或,并返回或操作前该元素的值。
+
{{jsxref("Atomics.store()")}}
+
将数组中指定的元素设置为给定的值,并返回该值
+
{{jsxref("Atomics.sub()")}}
+
将指定位置上的数组元素与给定的值相减,并返回相减前该元素的值。
+
{{jsxref("Atomics.wait()")}}
+
检测数组中某个指定位置上的值是否仍然是给定值,是则保持挂起直到被唤醒或超时。返回值为 "ok"、"not-equal" 或 "time-out"。调用时,如果当前线程不允许阻塞,则会抛出异常(大多数浏览器都不允许在主线程中调用 wait())。
+
{{jsxref("Atomics.xor()")}}
+
将指定位置上的数组元素与给定的值相异或,并返回异或操作前该元素的值。
+
+ +

示例

+ +

使用 Atomics

+ +
const sab = new SharedArrayBuffer(1024);
+const ta = new Uint8Array(sab);
+
+ta[0];
+// 0
+
+ta[0] = 5;
+// 5
+
+Atomics.add(ta, 0, 12);
+// 5
+Atomics.load(ta, 0);
+// 17 ✅
+// 12 ❌
+
+Atomics.and(ta, 0, 1);
+// 17
+Atomics.load(ta, 0);
+// 1
+
+Atomics.compareExchange(ta, 0, 5, 12);
+Atomics.load(ta, 0); // 12
+
+Atomics.exchange(ta, 0, 12);
+Atomics.load(ta, 0); // 12
+
+Atomics.isLockFree(1); // true
+Atomics.isLockFree(2); // true
+Atomics.isLockFree(3); // false
+Atomics.isLockFree(4); // true
+
+Atomics.or(ta, 0, 1);
+Atomics.load(ta, 0);  // 5
+
+Atomics.store(ta, 0, 12); // 12
+
+Atomics.sub(ta, 0, 2);
+Atomics.load(ta, 0); // 3
+
+Atomics.xor(ta, 0, 1);
+Atomics.load(ta, 0); // 4
+
+ +

Waiting 和 notifiying

+ +

给定一个共享的 Int32Array

+ +
const sab = new SharedArrayBuffer(1024);
+const int32 = new Int32Array(sab);
+
+ +

读取线程正在休眠并位置0上等待。只要该位置应为0,它就不会继续。但是,一旦写入线程存储了新值,写入线程将通知它并返回新值(123)。

+ +
Atomics.wait(int32, 0, 0);
+console.log(int32[0]); // 123
+
+ +

写入线程存储一个新值并再写入完成时通知等待线程:

+ +
console.log(int32[0]); // 0;
+Atomics.store(int32, 0, 123);
+Atomics.notify(int32, 0, 1);
+ +

规范

+ + + + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-atomics-object', 'Atomics')}}
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Atomics")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/islockfree/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/islockfree/index.html new file mode 100644 index 0000000000..be8b32b587 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/islockfree/index.html @@ -0,0 +1,69 @@ +--- +title: Atomics.isLockFree() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/isLockFree +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/isLockFree +--- +
{{JSRef}} {{SeeCompatTable}}
+ +

静态方法 Atomics.isLockFree() 用于校验是否能够使用原子操作的TypedArray的标准字节长度之一. 若该字节长度为可处理的TypedArray标准字节长度之一则返回  true.  TypedArray的标准字节长度参见 BYTES_PER_ELEMENT

+ +
{{EmbedInteractiveExample("pages/js/atomics-islockfree.html")}}
+ + + +

语法

+ +
Atomics.isLockFree(size)
+
+ +

参数

+ +
+
size
+
整形.字节长度,通常为TypedArray.BYTES_PER_ELEMENT
+
+ +

返回值

+ +

 {{jsxref("Boolean")}} 是否为能够使用原子操作的TypedArray的标准字节长度之一.

+ +

示例

+ +
Atomics.isLockFree(1); // true
+Atomics.isLockFree(2); // true
+Atomics.isLockFree(3); // false
+Atomics.isLockFree(4); // true
+Atomics.isLockFree(5); // false
+Atomics.isLockFree(6); // false
+Atomics.isLockFree(7); // false
+Atomics.isLockFree(8); // false
+Atomics.isLockFree(Float64Array.BYTES_PER_ELEMENT); // false,Atomics方法无法处理Float64Array
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Shared Memory', '#Atomics.isLockFree', 'Atomics.isLockFree')}}{{Spec2('Shared Memory')}}Initial definition.
+ +

Browser compatibility

+ + + +

{{Compat("javascript.builtins.Atomics.isLockFree")}}

+ +

See also

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/load/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/load/index.html new file mode 100644 index 0000000000..2129a3a4ab --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/load/index.html @@ -0,0 +1,76 @@ +--- +title: Atomics.load() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/load +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/load +--- +
{{JSRef}}
+ +

静态方法 Atomics.load() 返回一个数组当中给定位置的值。

+ +
{{EmbedInteractiveExample("pages/js/atomics-load.html")}}
+ + + +

语法

+ +
Atomics.load(typedArray, index)
+
+ +

参数

+ +
+
typedArray
+
一个共享的整型数组。可以是 {{jsxref("Int8Array")}},{{jsxref("Uint8Array")}},{{jsxref("Int16Array")}},{{jsxref("Uint16Array")}},{{jsxref("Int32Array")}} 或 {{jsxref("Uint32Array")}}.
+
index
+
在 typedArray 中需要加载的位置
+
+ +

返回值

+ +

给定位置的值 (typedArray[index])。

+ +

异常

+ + + +

示例

+ +
var sab = new SharedArrayBuffer(1024);
+var ta = new Uint8Array(sab);
+
+Atomics.add(ta, 0, 12);
+Atomics.load(ta, 0); // 12
+ +

规范

+ + + + + + + + + + + + + + +
规范状态论述
{{SpecName('ESDraft', '#sec-atomics.load', 'Atomics.load')}}{{Spec2('ESDraft')}}Initial definition in ES2017.
+ +

浏览器兼容

+ + + +

{{Compat("javascript.builtins.Atomics.load")}}

+ +

参阅

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/notify/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/notify/index.html new file mode 100644 index 0000000000..bf2399192c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/notify/index.html @@ -0,0 +1,93 @@ +--- +title: Atomics.notify() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/notify +tags: + - Atomics + - JavaScript + - Method + - Shared Memory +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/notify +--- +
{{JSRef}}
+ +

静态方法 Atomics.notify() 提醒一些在等待队列中休眠的代理。

+ +
+

注意:本操作仅在共享的 {{jsxref("Int32Array")}} 下可用。

+
+ +

语法

+ +
Atomics.notify(typedArray, index, count)
+
+ +

参数

+ +
+
typedArray
+
一个共享的 {{jsxref("Int32Array")}}。
+
index
+
typedArray 中要唤醒的目标索引。
+
count
+
要通知的正在休眠的代理的数量。默认是 {{jsxref("Infinity", "+Infinity")}}。
+
+ +

返回值

+ +

被唤醒的代理的数量。

+ +

异常

+ + + +

示例

+ +

分配一个共享的 Int32Array

+ +
var sab = new SharedArrayBuffer(1024);
+var int32 = new Int32Array(sab);
+
+ +

一个读线程会进入休眠并监视索引0处的值(默认为0)。只要索引0处的值不为0,读进程就会唤醒。但是,一旦写进程存储了一个新的值,写进程就会产生一个提醒并返回写入后的新值(123)。(这里示例有问题或者说对初学者不友好,如果直接在浏览器控制台运行下面代码会报错,因为我们不能尝试睡眠主线程,可以见 重学js —— 结构化数据之Atomics对象,同时我在 codepen 写了一个示例:Atomics.wait使用示例

+ +
Atomics.wait(int32, 0, 0);
+console.log(int32[0]); // 123
+ +

写进程写入一个新值并告知等待进程已经写入成功了:

+ +
console.log(int32[0]); // 0;
+Atomics.store(int32, 0, 123);
+Atomics.notify(int32, 0, 1);
+ +

规范

+ + + + + + + + + + + + + + +
规范状态注释
{{SpecName('ESDraft', '#sec-atomics.notify', 'Atomics.notify')}}{{Spec2('ESDraft')}}Initial definition in ES2017.
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Atomics.notify")}}

+ +

相关文档

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/or/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/or/index.html new file mode 100644 index 0000000000..286e89cc0a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/or/index.html @@ -0,0 +1,129 @@ +--- +title: Atomics.or() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/or +tags: + - Atomics + - JavaScript + - Method + - Shared Memory +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/or +--- +
{{JSRef}}
+ +

静态方法 Atomics.or() 用数组中指定位置的值进行一次按位或运算,并返回未计算时数组中指定位置处的值。这个atomic操作保证了在修改后的值被写回之前没有其它的写入操作发生。

+ +
{{EmbedInteractiveExample("pages/js/atomics-or.html")}}
+ + + +

Syntax

+ +
Atomics.or(typedArray, index, value)
+
+ +

参数

+ +
+
typedArray
+
一个共享的int数组,类型为 {{jsxref("Int8Array")}}、{{jsxref("Uint8Array")}}、{{jsxref("Int16Array")}}、{{jsxref("Uint16Array")}}、{{jsxref("Int32Array")}} 或 {{jsxref("Uint32Array")}}。
+
index
+
typedArray 中要进行按位或运算的索引。
+
value
+
要进行按位或运算的数。
+
+ +

返回值

+ +

typedArray[index] 处运算前的值。

+ +

异常

+ + + +

详情

+ +

当 a 或者 b 为1时,按位或运算结果为1。或运算真值表如下:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
aba | b
000
011
101
111
+ +

例如,让 5 & 1 进行按位或运算的结果是 0101 ,也就是十进制的5:

+ +
5  0101
+1  0001
+   ----
+5  0101
+
+ +

示例

+ +
var sab = new SharedArrayBuffer(1024);
+var ta = new Uint8Array(sab);
+ta[0] = 2;
+
+Atomics.or(ta, 0, 1); // returns 2, the old value
+Atomics.load(ta, 0);  // 3
+ +

规范

+ + + + + + + + + + + + + + +
规范状态注释
{{SpecName('ESDraft', '#sec-atomics.or', 'Atomics.or')}}{{Spec2('ESDraft')}}Initial definition in ES2017.
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Atomics.or")}}

+ +

相关文档

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/store/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/store/index.html new file mode 100644 index 0000000000..bfc5a03982 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/store/index.html @@ -0,0 +1,77 @@ +--- +title: Atomics.store() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/store +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/store +--- +
{{JSRef}}
+ +

静态的Atomics.store()方法将给定的值存储在数组中的指定位置,并返回该值。.

+ +
{{EmbedInteractiveExample("pages/js/atomics-store.html")}}
+ + + +

语法

+ +
Atomics.store(typedArray, index, value)
+
+ +

参数

+ +
+
typedArray
+
一个指定类型的shared数组. 类型为 {{jsxref("Int8Array")}}, {{jsxref("Uint8Array")}}, {{jsxref("Int16Array")}}, {{jsxref("Uint16Array")}}, {{jsxref("Int32Array")}}, 或者 {{jsxref("Uint32Array")}}其中一个.
+
index
+
typedArray中用来存储value的位置.
+
value
+
要存储的数字.
+
+ +

返回值

+ +

被存储的值.

+ +

异常

+ + + +

示例

+ +
var sab = new SharedArrayBuffer(1024);
+var ta = new Uint8Array(sab);
+
+Atomics.store(ta, 0, 12); // 12
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-atomics.store', 'Atomics.store')}}{{Spec2('ESDraft')}}Initial definition in ES2017.
+ +

浏览器兼容性

+ + + +

{{Compat("javascript.builtins.Atomics.store")}}

+ +

相关链接

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/sub/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/sub/index.html new file mode 100644 index 0000000000..c5a43d3e35 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/sub/index.html @@ -0,0 +1,82 @@ +--- +title: Atomics.sub() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/sub +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/sub +--- +
{{JSRef}}
+ +
Atomics.sub() 静态方法在数组中的给定位置减去给定值,并返回该位置的旧值。这个原子操作保证在修改后的值被写回之前不会发生其他写操作。
+ +
+ +
{{EmbedInteractiveExample("pages/js/atomics-sub.html")}}
+ + + +

语法

+ +
Atomics.sub(typedArray, index, value)
+
+ +

参数

+ +
+
typedArray
+
一个共享的整型 typed array。 例如 {{jsxref("Int8Array")}}, {{jsxref("Uint8Array")}}, {{jsxref("Int16Array")}}, {{jsxref("Uint16Array")}}, {{jsxref("Int32Array")}}, 或者 {{jsxref("Uint32Array")}}.
+
index
+
要被 value 值减去的 typedArray 索引位置。
+
value
+
要减去的数字。
+
+ +

返回值

+ +

给定位置的旧值 (typedArray[index])。

+ +

异常

+ + + +

例子

+ +
const sab = new SharedArrayBuffer(1024);
+const ta = new Uint8Array(sab);
+ta[0] = 48;
+
+Atomics.sub(ta, 0, 12); // returns 48, the old value
+Atomics.load(ta, 0); // 36
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-atomics.sub', 'Atomics.sub')}}{{Spec2('ESDraft')}}Initial definition in ES2017.
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.Atomics.sub")}}

+ +

相关

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/wait/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/wait/index.html new file mode 100644 index 0000000000..6a14b9260a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/wait/index.html @@ -0,0 +1,95 @@ +--- +title: Atomics.wait() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/wait +tags: + - Atomics + - JavaScript + - Method + - Shared Memory +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/wait +--- +
{{JSRef}}
+ +

静态方法 Atomics.wait() 确保了一个在 {{jsxref("Int32Array")}} 数组中给定位置的值没有发生变化、仍然是给定的值时进程将会睡眠,直到被唤醒或超时。该方法返回一个字符串,值为"ok", "not-equal", 或 "timed-out" 之一。

+ +
+

注意: 这项操作仅允许同一个共享内存的 {{jsxref("Int32Array")}} 配合使用并且无法运行在主线程中。

+
+ +

语法

+ +
Atomics.wait(typedArray, index, value[, timeout])
+
+ +

参数

+ +
+
typedArray
+
一个共享内存的  {{jsxref("Int32Array")}} 数组。
+
index
+
给定需要检测的 typedArray 数组的位置索引。
+
value
+
给定需要检测的位置索引的预期值。
+
timeout {{optional_inline}}
+
超时前等待的毫秒数。 {{jsxref("Infinity")}}, 如未提供该参数,将为无穷大。
+
+ +

返回值

+ +

一个 {{jsxref("String")}} 字符串,值为 "ok", "not-equal", 或 "timed-out" 三种之一。

+ +

异常

+ + + +

示例

+ +

创建一个共享内存的 Int32Array :

+ +
var sab = new SharedArrayBuffer(1024);
+var int32 = new Int32Array(sab);
+
+ +

检测给定的数组索引0的值,如果它如预期一般的等于我们给定的值0,则这个读取线程将会睡眠等待。一旦当有一个写入线程在这个位置存储了一个新值,它将会收到写入线程的通知并且返回新值 (123) :

+ +
Atomics.wait(int32, 0, 0);
+console.log(int32[0]); // 123
+ +

一旦某个写入线程存储了一个新值到int32 的索引0位置,则通知给该等待线程:

+ +
console.log(int32[0]); // 0;
+Atomics.store(int32, 0, 123);
+Atomics.notify(int32, 0, 1);
+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName('ESDraft', '#sec-atomics.wait', 'Atomics.wait')}}{{Spec2('ESDraft')}}Initial definition in ES2017.
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.Atomics.wait")}}

+ +

相关参阅

+ + diff --git a/files/zh-cn/web/javascript/reference/global_objects/atomics/xor/index.html b/files/zh-cn/web/javascript/reference/global_objects/atomics/xor/index.html new file mode 100644 index 0000000000..83b0bdfb46 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/atomics/xor/index.html @@ -0,0 +1,126 @@ +--- +title: Atomics.xor() +slug: Web/JavaScript/Reference/Global_Objects/Atomics/xor +translation_of: Web/JavaScript/Reference/Global_Objects/Atomics/xor +--- +
{{JSRef}}
+ +
Atomics.xor() 静态方法会在数组中给定位置进行一次按位异或操作,并返回该位置的旧值。这个原子操作保证在修改后的值被写回之前不会发生其他写操作。
+ +
+ +
{{EmbedInteractiveExample("pages/js/atomics-xor.html")}}
+ + + +

语法

+ +
Atomics.xor(typedArray, index, value)
+
+ +

参数

+ +
+
typedArray
+
一个共享的整型 typed array。例如 {{jsxref("Int8Array")}}, {{jsxref("Uint8Array")}}, {{jsxref("Int16Array")}}, {{jsxref("Uint16Array")}}, {{jsxref("Int32Array")}}, 或者 {{jsxref("Uint32Array")}}.
+
index
+
typedArray 中需要进行按位异或的索引位置。
+
value
+
要进行按位异或的数字。
+
+ +

返回值

+ +

给定位置的旧值 (typedArray[index])。

+ +

异常

+ + + +

描述

+ +

如果a和b不同,则按位异或操作产生1。异或操作的真值表如下:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
aba ^ b
000
011
101
110
+ +

例如,按位异或 5 & 1 将返回 0100,而 0100 是十进制为 4 。

+ +
5  0101
+1  0001
+   ----
+4  0100
+
+ +

例子

+ +
const sab = new SharedArrayBuffer(1024);
+const ta = new Uint8Array(sab);
+ta[0] = 5;
+
+Atomics.xor(ta, 0, 1); // returns 5, the old value
+Atomics.load(ta, 0);  // 4
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ESDraft', '#sec-atomics.xor', 'Atomics.xor')}}{{Spec2('ESDraft')}}Initial definition in ES2017.
+ +

浏览器支持

+ + + +

{{Compat("javascript.builtins.Atomics.xor")}}

+ +

相关

+ + -- cgit v1.2.3-54-g00ecf