Working multithreading requires support in the browsers, in Emscripten, and in Qt.
Thread support in browsers is starting to come back after SPECTRE, and can often be enabled in the browser settings for development purposes.
Emscripten has a “USE_PTHREADS=1” build mode which enables threads. This mode somewhat less stable than the default mode, with some limitations. See https://kripken.github.io/emscripten-site/docs/porting/pthreads.html
Qt already supports threads, so what we need to do here is set the required Emscripten options and make sure we use the Emscripten API correctly.
Patch for enabling USE_PTHREADS for Qt: WebAssembly: implement thread support
Specific Emscripten bugs and limitations hit by Qt include:
- Qt Quick apps miscompiles: https://github.com/WebAssembly/binaryen/issues/1718
- Can’t use the MODULARIZE feature: https://github.com/kripken/emscripten/issues/7448
- The ALLOW_MEMORY_GROWTH flag is not supported: Qt must hardcode how much memory the app should use, and the amount is then allocated on startup. Browsers limit this startup allocation to 1 GB. https://github.com/kripken/emscripten/issues/7110
Learnings relevant to the Qt port:
- Enable by setting USE_PTHREADS = 1 or USE_PTHREADS = 2
- There's a ripple effect other options not supported, or new options needed (WASM_MEM_MAX)
- In particular MODULARIZE=1 does not work. We can work around this in qtloader.js.
We already have a (-no)-thread feature, and need to implement "threads enabled" mode.