Skip to content

Commit daf801a

Browse files
committed
wip
1 parent b7910c5 commit daf801a

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

accepted/2023/wasm-browser-threads.md

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Move all managed user code out of UI/DOM thread, so that it becomes consistent w
6161
- they need to be disposed on correct thread. GC is running on random thread
6262

6363
**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.
6565
- Because they could only be created asynchronously, but `pthread_create` is synchronous call
6666
- Because they are slow to start
6767
- 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
196196
- this will create double proxy for `Task`, `JSObject`, `Func<>` etc
197197
- difficult to GC, difficult to debug
198198
- double marshaling of parameters
199-
- Avoids **1,2** for JS callback
200199
- Solves **1,2** for managed code.
200+
- Avoids **1,2** for JS callback
201201
- emscripten main loop stays responsive
202202
- Solves **3,4,5**
203203

@@ -210,40 +210,51 @@ Move all managed user code out of UI/DOM thread, so that it becomes consistent w
210210
- this will create double proxy for `Task`, `JSObject`, `Func<>` etc
211211
- difficult to GC, difficult to debug
212212
- double marshaling of parameters
213-
- Ignores **1,2** for JS callback
214213
- Solves **1,2** for managed code
215-
- emscripten main loop stays responsive
216214
- unless there is sync `JSImport`->`JSExport` call
215+
- Ignores **1,2** for JS callback
216+
- emscripten main loop stays responsive
217217
- Solves **3,4,5**
218218

219-
## (12) Deputy + managed dispatch to UI + JSWebWorker
219+
## (12) Deputy + managed dispatch to UI + JSWebWorker + with sync JSExport
220220
- **B,F,K,N,Q,S/T,U,W,a/b/c,d+f,h,l,n,s/z,v**
221221
- this uses `JSSynchronizationContext` to dispatch calls to UI thread
222222
- this is "dirty" as compared to sidecar because some managed code is actually running on UI thread
223223
- it needs to also use `SynchronizationContext` for `JSExport` and callbacks, to dispatch to deputy.
224224
- blazor render could be both legacy render or Blazor server style
225225
- because we have both memory and mono on the UI thread
226-
- Ignores **1,2** for JS callback
227226
- Solves **1,2** for managed code
228-
- emscripten main loop stays responsive
229227
- unless there is sync `JSImport`->`JSExport` call
228+
- Ignores **1,2** for JS callback
229+
- emscripten main loop could deadlock on sync JSExport
230230
- Solves **3,4,5**
231231

232-
## (13) Deputy + emscripten dispatch to UI + JSWebWorker
232+
## (13) Deputy + emscripten dispatch to UI + JSWebWorker + with sync JSExport
233233
- **B,F,J,N,R,T,U,W,a/b/c,d+f,i,l,n,s,v**
234234
- is variation of **(12)**
235235
- with emscripten dispatch and marshaling in UI thread
236236
- 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.
237237
- it uses other `cwraps` locally on UI thread, like `mono_wasm_new_root`, `stringToMonoStringRoot`, `malloc`, `free`, `create_task_callback_method`
238238
- it means that interop related managed runtime code is running on the UI thread, but not the user code.
239239
- 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
241241
- blazor render could be both legacy render or Blazor server style
242242
- 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
243245
- 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
244253
- Solves **1,2** for managed code
245254
- emscripten main loop stays responsive
246255
- unless there is sync `JSImport`->`JSExport` call
256+
- Avoids **2** for JS callback
257+
- by throwing PNSE
247258
- Solves **3,4,5**
248259

249260
# Details
@@ -257,8 +268,9 @@ Move all managed user code out of UI/DOM thread, so that it becomes consistent w
257268
- async could be called on all `JSWebWorker` threads
258269
- sync could be called on `JSWebWorker`
259270
- 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
262274

263275
## Proxies - thread affinity
264276
- all of them have thread affinity

0 commit comments

Comments
 (0)