You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: accepted/2023/wasm-browser-threads.md
+23-11Lines changed: 23 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -61,7 +61,7 @@ Move all managed user code out of UI/DOM thread, so that it becomes consistent w
61
61
- they need to be disposed on correct thread. GC is running on random thread
62
62
63
63
**4)** State management of JS context `self` of the worker.
64
-
- emscripten pre-allocates poll of web worker to be used as pthreads.
64
+
- emscripten pre-allocates pool of web worker to be used as pthreads.
65
65
- Because they could only be created asynchronously, but `pthread_create` is synchronous call
66
66
- Because they are slow to start
67
67
- those pthreads have stateful JS context `self`, which is re-used when mapped to C# thread pool
@@ -196,8 +196,8 @@ Move all managed user code out of UI/DOM thread, so that it becomes consistent w
196
196
- this will create double proxy for `Task`, `JSObject`, `Func<>` etc
197
197
- difficult to GC, difficult to debug
198
198
- double marshaling of parameters
199
-
- Avoids **1,2** for JS callback
200
199
- Solves **1,2** for managed code.
200
+
- Avoids **1,2** for JS callback
201
201
- emscripten main loop stays responsive
202
202
- Solves **3,4,5**
203
203
@@ -210,40 +210,51 @@ Move all managed user code out of UI/DOM thread, so that it becomes consistent w
210
210
- this will create double proxy for `Task`, `JSObject`, `Func<>` etc
211
211
- difficult to GC, difficult to debug
212
212
- double marshaling of parameters
213
-
- Ignores **1,2** for JS callback
214
213
- Solves **1,2** for managed code
215
-
- emscripten main loop stays responsive
216
214
- unless there is sync `JSImport`->`JSExport` call
215
+
- Ignores **1,2** for JS callback
216
+
- emscripten main loop stays responsive
217
217
- Solves **3,4,5**
218
218
219
-
## (12) Deputy + managed dispatch to UI + JSWebWorker
219
+
## (12) Deputy + managed dispatch to UI + JSWebWorker + with sync JSExport
220
220
-**B,F,K,N,Q,S/T,U,W,a/b/c,d+f,h,l,n,s/z,v**
221
221
- this uses `JSSynchronizationContext` to dispatch calls to UI thread
222
222
- this is "dirty" as compared to sidecar because some managed code is actually running on UI thread
223
223
- it needs to also use `SynchronizationContext` for `JSExport` and callbacks, to dispatch to deputy.
224
224
- blazor render could be both legacy render or Blazor server style
225
225
- because we have both memory and mono on the UI thread
226
-
- Ignores **1,2** for JS callback
227
226
- Solves **1,2** for managed code
228
-
- emscripten main loop stays responsive
229
227
- unless there is sync `JSImport`->`JSExport` call
228
+
- Ignores **1,2** for JS callback
229
+
- emscripten main loop could deadlock on sync JSExport
230
230
- Solves **3,4,5**
231
231
232
-
## (13) Deputy + emscripten dispatch to UI + JSWebWorker
232
+
## (13) Deputy + emscripten dispatch to UI + JSWebWorker + with sync JSExport
233
233
-**B,F,J,N,R,T,U,W,a/b/c,d+f,i,l,n,s,v**
234
234
- is variation of **(12)**
235
235
- with emscripten dispatch and marshaling in UI thread
236
236
- this uses `emscripten_dispatch_to_thread_async` for `call_entry_point`, `complete_task`, `cwraps.mono_wasm_invoke_method_bound`, `mono_wasm_invoke_bound_function`, `mono_wasm_invoke_import`, `call_delegate_method` to get to the UI thread.
237
237
- it uses other `cwraps` locally on UI thread, like `mono_wasm_new_root`, `stringToMonoStringRoot`, `malloc`, `free`, `create_task_callback_method`
238
238
- it means that interop related managed runtime code is running on the UI thread, but not the user code.
239
239
- it means that parameter marshalling is fast (compared to sidecar)
240
-
- it still needs to enter GC barrier and so it could block UI for GC run
240
+
- it still needs to enter GC barrier and so it could block UI for GC run shortly
241
241
- blazor render could be both legacy render or Blazor server style
242
242
- because we have both memory and mono on the UI thread
243
+
- Solves **1,2** for managed code
244
+
- unless there is sync `JSImport`->`JSExport` call
243
245
- Ignores **1,2** for JS callback
246
+
- emscripten main loop could deadlock on sync JSExport
247
+
- Solves **3,4,5**
248
+
249
+
## (14) Deputy + emscripten dispatch to UI + JSWebWorker + no sync JSExport
250
+
-**B,F,J,N,R,T,U,W,a/b/c,d+f,i,l,n,s,v**
251
+
- is variation of **(13)**
252
+
- without support for synchronous JSExport
244
253
- Solves **1,2** for managed code
245
254
- emscripten main loop stays responsive
246
255
- unless there is sync `JSImport`->`JSExport` call
256
+
- Avoids **2** for JS callback
257
+
- by throwing PNSE
247
258
- Solves **3,4,5**
248
259
249
260
# Details
@@ -257,8 +268,9 @@ Move all managed user code out of UI/DOM thread, so that it becomes consistent w
257
268
- async could be called on all `JSWebWorker` threads
258
269
- sync could be called on `JSWebWorker`
259
270
- sync could be called on from UI thread is problematic
260
-
- with spin-wait in UI in JS it would at least block the UI rendering
261
-
- with spin-wait in emscripten, it could deadlock the rest of the app
271
+
- with spin-wait in UI in JS it has **2)** problems
272
+
- with spin-wait in UI when emscripten is there could also deadlock the rest of the app
273
+
- this means that combination of sync JSExport and deputy design is dangerous
0 commit comments