From de4f256d48145778ed56389f5e883c5994b34dd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Tue, 20 Mar 2018 13:30:53 +0100 Subject: Wasm: enable thread support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit configure.json: Make the “thread” feature be allowed for wasm but disabled by default. Change qmake.conf and wasm.prf to enable Emscripten pthreads mode: - Add USE_PTHREADS=1 linker flag - Add PTHREAD_POOL_SIZE linker flag with a default pool size (4). - Add TOTAL_MEMORY linker flag to set available memory (1GB) It is possible to override options such as PTHREAD_POOL_SIZE from the application .pro file using QMAKE_WASM_PTHREAD_POOL_SIZE To change TOTAL_MEMORY, use QMAKE_WASM_TOTAL_MEMORY Make qtloader.js work in pthreads mode: - The Module.instantiateWasm callback must provide the module in addition to the instance to Emscripten. - Set Module.mainScriptUrlOrBlob so that the pthreads web workers can access the main script Task-number: QTBUG-64625 Change-Id: I1ab5a559ec97c27c5fc24500ba5f863bcd275141 Reviewed-by: Morten Johan Sørvig Reviewed-by: Lorn Potter --- mkspecs/features/wasm/wasm.prf | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'mkspecs/features/wasm') diff --git a/mkspecs/features/wasm/wasm.prf b/mkspecs/features/wasm/wasm.prf index c885b166ce..de726c674c 100644 --- a/mkspecs/features/wasm/wasm.prf +++ b/mkspecs/features/wasm/wasm.prf @@ -2,6 +2,39 @@ # DESTDIR will be empty if not set in the app .pro file; make sure it has a value isEmpty(DESTDIR): DESTDIR = $$OUT_PWD +exists($$QMAKE_QT_CONFIG) { + qtConfig(thread) { + + EMCC_THREAD_LFLAGS += -s USE_PTHREADS=1 + # Hardcode wasm memory size. Emscripten does not currently support memory growth + # (ALLOW_MEMORY_GROWTH) in pthreads mode, and requires specifying the memory size + # at build time. Further, browsers limit the maximum initial memory size to 1GB. + TOTAL_MEMORY = 1GB + !isEmpty(QMAKE_WASM_TOTAL_MEMORY) { + TOTAL_MEMORY = $$QMAKE_WASM_TOTAL_MEMORY + } + + message("Setting TOTAL_MEMORY to" $$TOTAL_MEMORY) + EMCC_THREAD_LFLAGS += -s TOTAL_MEMORY=$$TOTAL_MEMORY + + # Create worker threads at startup. This is supposed to be an optimization, + # however exceeding the pool size has been obesverved to hang the application. + POOL_SIZE = 4 + !isEmpty(QMAKE_WASM_PTHREAD_POOL_SIZE) { + POOL_SIZE = $$QMAKE_WASM_PTHREAD_POOL_SIZE + } + + message("Setting PTHREAD_POOL_SIZE to" $$POOL_SIZE) + EMCC_THREAD_LFLAGS += -s PTHREAD_POOL_SIZE=$$POOL_SIZE + } else { + EMCC_THREAD_LFLAGS += -s ALLOW_MEMORY_GROWTH=1 + } + QMAKE_LFLAGS += $$EMCC_THREAD_LFLAGS + QMAKE_LFLAGS_DEBUG += $$EMCC_THREAD_LFLAGS + QMAKE_CFLAGS += $$EMCC_THREAD_LFLAGS + QMAKE_CXXFLAGS += $$EMCC_THREAD_LFLAGS +} + # Create js and wasm files for applications contains(TEMPLATE, .*app) { TARGET_BASE = $${TARGET} -- cgit v1.2.3