[2] 元の[[アルゴリズム]]やその実行元とは別の[[アルゴリズム]]を同時に実行させることを[DFN[[RUBYB[並列で]@en[in parallel]]]]の実行といいます。

* 仕様書

[REFS[
- [1] '''[CITE@en-GB-x-hixie[HTML Standard]] ([TIME[2014-09-19 23:39:59 +09:00]] 版) <https://html.spec.whatwg.org/#in-parallel>'''
- [19] [CITE@en[DOM Standard]] ([TIME[2016-02-12 01:13:05 +09:00]] 版) <https://dom.spec.whatwg.org/#observing-event-listeners>
]REFS]

* 動作

[3] ある[[アルゴリズム]]が[[並列]]に実行するという時、その[[アルゴリズム]]の手順は[[イベントループ]]など他の処理と同時に動作することを表しています。 [SRC[>>1]]

[4] その実装方法は例えば[[プロセス]]を分離したり[[スレッド]]を使ったり、
[[時分割]]の[[協調マルチタスク]]としたりと色々と考えられますが、
仕様上は特に限定されていません [SRC[>>1]]。

* キュー

[30] [DFN[[RUBYB[並列キュー]@en[parallel queue]]]]は、
[[並列に]]実行される[[アルゴリズム]]同士の[[競合]]を避けるために用いられる[[キュー]]です
[SRC[>>1]]。

[32] [[共有ワーカー管理器]]で使われています。

;; [31] [[並列に]]実行されるもののすべてが[[並列キュー]]を用いるわけではありません。

[33] [[並列キュー]]は、
[[キュー]]である[DFN[[F[[RUBYB[[[アルゴリズムキュー]]]@en[algorithm queue]]]]]]を持ちます。
初期状態は[[空]]です。 [SRC[>>1]]

[36] [DFN[[RUBYB[新しい並列キューを開始]@en[start a new parallel queue]]]]するには、
次のようにします。 [SRC[>>1]]

[FIG(steps)[
= [37] [VAR[並列キュー]]を、新しい[[並列キュー]]に設定します。
= [43] [VAR[並列キュー]]を返します。
]FIG]

[38] そして[[並列に]]、次のようにします。 [SRC[>>1]]

[FIG(steps)[
= [39] 繰り返し、
== [40] [VAR[手順群]]を、[VAR[並列キュー]]の[F[アルゴリズムキュー]]から [[dequeue]]
した結果に設定します。
== [41] [VAR[手順群]]が存在する場合、
=== [42] [VAR[手順群]]を実行します。
]FIG]

;; [44] [[共有ワーカー管理器]]が呼び出します。

[34] [[並列キュー]][VAR[キュー]]に[DFN[[RUBYB[手順群をキューに追加]@en[enqueue steps]]]]するには、
[VAR[手順群]]について、次のようにします。 [SRC[>>1]]

[FIG(steps)[
= [35] [VAR[キュー]]の[F[アルゴリズムキュー]]について、[VAR[手順群]]を[[キューに追加]]します。
]FIG]

* 並列に動作するアルゴリズム

[5] [[Webブラウザー]]上で[[並列]]に動作する[[アルゴリズム]]には、次のものがあります。

[FIG(short list)[
- メインの[[イベントループ]]
- 各[[ワーカー]]の[[イベントループ]]
- 各[[並列キュー]]の処理
]FIG]

[6] [[イベントループ]]自体は[[並列]]に動作するとは明記されていませんが、
常時動作するとの要件を満たすためには相互に[[並列]]に動作させるしかありません。

[7] [[ワーカー]]の[[イベントループ]]は [[run a worker]] から呼び出されますが、
この[[アルゴリズム]]は新しい[[並列]]な実行環境を作成するものです。

[8] 次に示す[[アルゴリズム]]の一部または全部は仕様書上「[RUBYB[[[並列]]で]@en[in parallel]]」
と明記されており、それぞれ[[並列]]に実行されます。

[FIG(short list)[
- [[スピン]]
- [[セッション履歴イベントループ]]
- [[fetch]] に関わるいくつかのアルゴリズム
- [[ハイパーリンクのダウンロード]]
- [CODE(JS)@en[[[document.load]]]]
- [CODE(HTMLe)@en[[[canvas]]]] の [CODE(DOMm)@en[[[toBlob]]]]
- [[downloading hyperlinks]]
- [[application cache download process]]
- [CODE(DOMm)@en[[[setTimeout]]]]、[CODE(DOMm)@en[[[setInterval]]]]
- [CODE(JS)@en[[[new]] [[Worker]]]]
- [CODE(JS)@en[[[new]] [[SharedWorker]]]]
- [CODE(DOMi)@en[[[EventSource]]]] の [[reestablish the connection]]
- [CODE(HTMLa)@en[[[ping]]]]
- [[favicon]]
- [CODE(JS)@en[[[new]] [[EventSource]]]]
- [CODE(JS)@en[[[new]] [[WebSocket]]]]
- [CODE(JS)@en[[[new]] [[Notification]]]] [SRC[[[Notifications API]]]]
- [[cache failure steps]]
- [[kill a worker]]
- [[terminate a worker]]
- [CODE(HTML)@en[[[<input type=file>]]]] の [CODE(DOMm)@en[[[click]]]]
- [CODE(DOMm)@en[[[createImageBitmap]]]]
- [[update the image data]]
- [[navigate]]
- [[handle fetch]]
- [[サービスワーカー登録]]
- [[ジョブを走らせる]]
- [CODE[img.decode]]
- [CODE[HostImportModuleDynamically]]
- その他多数

[HISTORY[
- [CODE(DOMm)@en[requestAutocomplete]]
- [CODE(DOMm)@en[[[postMessage]]]]
- [[printing steps]]
]HISTORY]
]FIG]

;; [9] これらの一部は[[同期区間]]をも有しています。

;; [10] [[HTML Standard]] では「in the background」とも記述されていることがありますが、
いずれも [[in parallel]] と併用されています。

[13] 次に示す[[アルゴリズム]]の一部または全部は仕様書上
「[DFN[[RUBYB[非同期的に]@en[asynchronously]]]]」
実行すると規定されており、それぞれ[[並列]]に実行されます。

[FIG(middle list)[
- [[ファイルシステム]]からの読み込み [SRC[[[File API]]]]
- [CODE(DOMm)@en[[[getUserMedia]]]] [SRC[[[getUserMedia]]]]
- [CODE(DOMi)@en[[[FontFace]]]] 関係のいくつかの処理 [SRC[[[css-font-loading]]]]
- [[バイブレーションを実施]] [SRC[[[Vibration API]]]]
- [CODE(DOMm)@en[requestIdleCallback]]
- その他多数
]FIG]

;; [12] [[HTML Standard]] で「asynchronously」と記述されていた箇所は明確化のためすべて
[[in parallel]] に置き換えられました [SRC[>>11, >>16]]。 >>13 のアルゴリズムはそれ以前に
[[HTML Standard]] の作法を真似たものの、追随していないものです。

[18] [[受動的]]な[[イベントリスナー]]しかない場合に、
[[利用者エージェント]]は[[イベント]]を含む処理を[[並列に]]実行することで[[最適化]]できるかもしれません
[SRC[>>19]]。

;; [[受動的リスナー]]の項も参照。

[22] 入出力の[[装置][装置 (Web)]]の処理も、[[並列に]]行われます。

[EG[
[23] 例えば[[マウス操作]]を[[プラットフォーム]]から随時受け取って、
適当な[[イベントループ]]に[[タスクを追加]]する必要があります。
]EG]

[EG[
[24] 例えば[[再生]]中の[[動画]]を処理して、適宜 
([[イベントループ]]内の[[レンダリングの更新]]とは別に)
[[画面]]の[[描画]]に反映させる必要がある場合もあります。
]EG]

[EG[
[25] 例えば[[ストレージ][ストレージ (Web)]]への実際の書き込みを遅延させている場合、
[[タスク]]完了後、[[並列に]]、[[プラットフォーム]]に書き込み指示を引き渡す必要があります。
]EG]

[21] [[標準化]]の範囲外なので[[仕様書]]には明記されていませんが、
この他に、[[Webブラウザー]]の[[利用者インターフェイス]]に関わる処理も[[並列に]]実行する必要があります。

* 通信

[14] いくつかの[[アルゴリズム]]では[[同期区間]]という形で[[イベントループ]]との同期待ちが行われます。

;; [15] 実際には[[並列]]に実行される手続きの最後で[[マイクロタスク]]を追加するという形で実装されているかもしれません。

[105] [CODE(DOMi)@en[[[ApplicationCache]]]] の [CODE(DOMm)@en[[[abort]]]] [[メソッド]]は[RUBYB[裏で動いている]@en[in the background]]処理に対して[DFN[[RUBYB[信号を送信]@en[send a signal]]]]します。

* 停止

[72] ある[[アルゴリズム]]が同時に実行中の他の[[アルゴリズム]]を[RUBYB[停止]@en[abort]]させることがあります。 

[FIG(list)[
- [73] [[fetch]] はしばしば停止させられることがあります。
-- [74] [[update the image data]]
-- [78] [[[CODE(HTMLe)@en[track]]処理モデルの開始]]
-- [80] [CODE(DOMi)@en[[[EventSource]]]] の [CODE(DOMm)@en[[[close]]]]
-- [81] [CODE(DOMi)@en[[[EventSource]]]] の [[forcibly close]]
-- [82] [CODE(DOMi)@en[[[EventSource]]]] が[[ごみ収集]]された場合
- [75] [[媒体要素読込算法]]が[[資源選択算法]]を停止させることがあります。
- [76] [[seek]] が同時に実行中の [[seek]] を停止させることがあります。
- [79] [[ワーカーを殺す]][[算法]]、[[ワーカーを終端させる]][[算法]]は実行中の[[スクリプト]]を停止させます。
- [77] [[[CODE(HTMLe)@en[track]]処理モデルの開始]]は他の[[算法]]は停止させませんが、
その代わりに動作中の同じ[[算法]]に処理を委ねて自身が停止します。
- [[update the image data]]
- [[バイブレーションを実施]]
]FIG]

* 歴史

[REFS[
- [11] [CITE@en[Web Applications 1.0 r8799 Stop using the word 'asynchronously', and reduce usage of the word 'synchronous'.]] ([TIME[2014-09-20 08:19:00 +09:00]] 版) <https://html5.org/r/8799>
- [16] [CITE@en[Sync with https://html5.org/r/8799 (Stop using the word 'asynchronously'... · fcc802a · ResponsiveImagesCG/picture-element]] ([TIME[2014-09-23 06:25:10 +09:00]] 版) <https://github.com/ResponsiveImagesCG/picture-element/commit/fcc802a79442529b3b5fb9eed4b8429c0699682a>
- [106] [CITE@en[Asynchronously -> in parallel · b2279a9 · whatwg/notifications]]
( ([TIME[2014-10-07 08:36:52 +09:00]] 版))
<https://github.com/whatwg/notifications/commit/b2279a91cfb2dcb6002ac54b7c5e3fe4dfc5fd91>
- [107] [CITE@en[Use "in parallel" · 21c1135 · whatwg/fetch]]
( ([TIME[2014-10-07 08:39:21 +09:00]] 版))
<https://github.com/whatwg/fetch/commit/21c113517998443dbc361be36f96524f3ee3a586>
- [17] [CITE@en[Make update the image data run when the document becomes active. · ee68507 · ResponsiveImagesCG/picture-element]] ([TIME[2014-10-23 10:40:49 +09:00]] 版) <https://github.com/ResponsiveImagesCG/picture-element/commit/ee685075cd65f02022d74b7932073fda4102d15f>
]REFS]

[20] [CITE@en[Add EventListenerOptions and passive event listener feature · whatwg/dom@253a21b]]
([TIME[2016-02-15 00:07:42 +09:00]] 版)
<https://github.com/whatwg/dom/commit/253a21b8e78e37447c47983916a7cf39c4f6a3c5>

[26] [CITE@en[Make navigate use "in parallel" · whatwg/html@77b1e02]]
([TIME[2016-05-01 13:52:47 +09:00]] 版)
<https://github.com/whatwg/html/commit/77b1e021bcb9229c60fc90dc72bbb0e0d1cff7e4>

[27] [CITE@en[refactor: install steps (#485)]]
([[marcoscaceres]]著, [TIME[2016-08-09 17:04:34 +09:00]])
<https://github.com/w3c/manifest/commit/e0906abde1fa4286526ecf866ee59856e3914716>

[28] [CITE@en["fetch a single module script" needs to post a task back to the main thread · Issue #2202 · whatwg/html]]
([TIME[2017-01-05 20:11:10 +09:00]])
<https://github.com/whatwg/html/issues/2202>

[29] [CITE@en[Make shared worker creation deterministic]]
([[jakearchibald]]著, [TIME[2017-09-21 22:27:09 +09:00]])
<https://github.com/whatwg/html/commit/9fda90f44c7c9223de6e3755a043f67ea7c92bf5>

[45] [CITE@en[Introduce a concept named threads to better describe concurrency · Issue #1843 · whatwg/html]]
([TIME[2017-09-29 17:47:35 +09:00]])
<https://github.com/whatwg/html/issues/1843>

[46] [CITE@en[Adding a method to queue particular "in parallel" steps. by jakearchibald · Pull Request #3038 · whatwg/html]]
([TIME[2017-09-29 17:55:49 +09:00]])
<https://github.com/whatwg/html/pull/3038>

[47] [CITE@en[Adding a method to queue particular "in parallel" steps. by jakearchibald · Pull Request #3038 · whatwg/html]]
([TIME[2017-09-29 17:58:16 +09:00]])
<https://github.com/whatwg/html/pull/3038>

[48] [CITE@en[Parallel queues by jakearchibald · Pull Request #53 · WICG/background-fetch]]
([TIME[2017-09-29 17:58:38 +09:00]])
<https://github.com/WICG/background-fetch/pull/53>

[49] [CITE@en[Editorial: less continue after returning]]
([[annevk]]著, [TIME[2018-01-23 23:24:34 +09:00]])
<https://github.com/whatwg/html/commit/0d097af1acbeabe56a1b0d0c2e2f521eca4defa8>

[50] [CITE@en["Return the new EventSource object, but continue..." · Issue #692 · whatwg/html]]
([TIME[2018-01-25 13:01:55 +09:00]])
<https://github.com/whatwg/html/issues/692>

[51] [CITE@en[Editorial: less continue after returning by annevk · Pull Request #3385 · whatwg/html]]
([TIME[2018-01-25 13:02:38 +09:00]])
<https://github.com/whatwg/html/pull/3385>

[52] [CITE@en[Editorial: no more "return, but continue" for <input type=file>]]
([[annevk]]著, [TIME[2018-01-24 00:21:18 +09:00]])
<https://github.com/whatwg/html/commit/b2fc1c1d49ad5701b4bdcf80dcee115c3167005e>

[53] [CITE@en[Editorial: no more "return, but continue" for <input type=file> by annevk · Pull Request #3388 · whatwg/html]]
([TIME[2018-01-25 13:03:48 +09:00]])
<https://github.com/whatwg/html/pull/3388>

[54] [CITE@en[Editorial: rewrite createImageBitmap()]]
([[annevk]]著, [TIME[2018-01-24 17:54:28 +09:00]])
<https://github.com/whatwg/html/commit/5b40420b83dad83e5e566de9581dc51e4e4ee6cd>

[55] [CITE@en[Editorial: rewrite createImageBitmap() by annevk · Pull Request #3389 · whatwg/html]]
([TIME[2018-01-25 13:05:28 +09:00]])
<https://github.com/whatwg/html/pull/3389>

[56] [CITE@en[Editorial: "return, but continue" cleanup for downloads and Worker]]
([[annevk]]著, [TIME[2018-01-24 22:04:54 +09:00]])
<https://github.com/whatwg/html/commit/0217e1d4cb1ace836f6ef72a402b643aa879fbed>

[57] [CITE@en[Editorial: "return, but continue" cleanup for downloads and Worker by annevk · Pull Request #3397 · whatwg/html]]
([TIME[2018-01-27 16:50:06 +09:00]])
<https://github.com/whatwg/html/pull/3397>

[58] [CITE@en[Clarify that objects created during pipeTo() are not observable]]
([[domenic]]著, [TIME[2018-03-09 19:38:24 +09:00]])
<https://github.com/whatwg/streams/commit/7b8dffe8ec270bb918340e011349e2c1b5521a15>

[59] [CITE@en[Clarify realm and task-queuing situation in pipeTo() by domenic · Pull Request #902 · whatwg/streams]]
([TIME[2018-03-10 13:30:04 +09:00]])
<https://github.com/whatwg/streams/pull/902>

[60] [CITE@en[Editorial: link to guidance on "in parallel" near its definition]]
([[TimothyGu]]著, [TIME[2019-06-14 07:45:52 +09:00]])
<https://github.com/whatwg/html/commit/4f45a250261f763f523ed3c2130e9352cd8675de>

[61] [CITE@en[Editorial: add link to guidance on using "in parallel" near its definition by TimothyGu · Pull Request #4683 · whatwg/html]]
([TIME[2020-01-12 14:44:35 +09:00]])
<https://github.com/whatwg/html/pull/4683>

[62] [CITE@en[What goes into "in parallel" blocks vs. in a task? · Issue #4669 · whatwg/html]]
([TIME[2020-01-12 14:44:47 +09:00]])
<https://github.com/whatwg/html/issues/4669>