From ff27062decade57ce69d39e882ef0fb3ae2744c3 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 10 Oct 2024 22:13:55 -0700 Subject: [PATCH] Use emscripten_has_threading_support. NFC (#22709) This change also fixes a bug in `LIBRARY_DEBUG` where it wasn't handling single line arrow function correctly. --- src/jsifier.mjs | 22 +++++++++++-------- src/library_pthread.js | 7 ++++-- src/runtime_debug.js | 2 +- .../test_codesize_minimal_pthreads.gzsize | 2 +- .../test_codesize_minimal_pthreads.jssize | 2 +- test/test_browser.py | 9 ++++---- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/jsifier.mjs b/src/jsifier.mjs index eba2ba29b08b..09b41f08dff5 100644 --- a/src/jsifier.mjs +++ b/src/jsifier.mjs @@ -284,17 +284,21 @@ ${argConversions} // apply LIBRARY_DEBUG if relevant if (LIBRARY_DEBUG && !isJsOnlySymbol(symbol)) { - snippet = modifyJSFunction( - snippet, - (args, body, async) => `\ + snippet = modifyJSFunction(snippet, (args, body, async, oneliner) => { + var run_func; + if (oneliner) { + run_func = `var ret = ${body}`; + } else { + run_func = `var ret = (() => { ${body} })();`; + } + return `\ function(${args}) { - var ret = (() => { if (runtimeDebug) err("[library call:${mangled}: " + Array.prototype.slice.call(arguments).map(prettyPrint) + "]"); - ${body} - })(); - if (runtimeDebug && typeof ret != "undefined") err(" [ return:" + prettyPrint(ret)); + if (runtimeDebug) err("[library call:${mangled}: " + Array.prototype.slice.call(arguments).map(prettyPrint) + "]"); + ${run_func} + if (runtimeDebug) err(" [ return:" + prettyPrint(ret)); return ret; -}`, - ); +}`; + }); } const sig = LibraryManager.library[symbol + '__sig']; diff --git a/src/library_pthread.js b/src/library_pthread.js index f0b2ee0a0e77..397582e75b1b 100644 --- a/src/library_pthread.js +++ b/src/library_pthread.js @@ -690,13 +690,16 @@ var LibraryPThread = { __pthread_create_js__noleakcheck: true, #endif __pthread_create_js__deps: ['$spawnThread', 'pthread_self', '$pthreadCreateProxied', + 'emscripten_has_threading_support', #if OFFSCREENCANVAS_SUPPORT 'malloc', #endif ], __pthread_create_js: (pthread_ptr, attr, startRoutine, arg) => { - if (typeof SharedArrayBuffer == 'undefined') { - err('Current environment does not support SharedArrayBuffer, pthreads are not available!'); + if (!_emscripten_has_threading_support()) { +#if ASSERTIONS + dbg('pthread_create: environment does not support SharedArrayBuffer, pthreads are not available'); +#endif return {{{ cDefs.EAGAIN }}}; } #if PTHREADS_DEBUG diff --git a/src/runtime_debug.js b/src/runtime_debug.js index 7637adbb65e9..2cc1d464e8ea 100644 --- a/src/runtime_debug.js +++ b/src/runtime_debug.js @@ -171,7 +171,7 @@ var runtimeDebug = true; // Switch to false at runtime to disable logging at the var printObjectList = []; function prettyPrint(arg) { - if (typeof arg == 'undefined') return '!UNDEFINED!'; + if (typeof arg == 'undefined') return 'undefined'; if (typeof arg == 'boolean') arg = arg + 0; if (!arg) return arg; var index = printObjectList.indexOf(arg); diff --git a/test/other/codesize/test_codesize_minimal_pthreads.gzsize b/test/other/codesize/test_codesize_minimal_pthreads.gzsize index 3fc7764b32fa..f9d8cb95dd8c 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.gzsize +++ b/test/other/codesize/test_codesize_minimal_pthreads.gzsize @@ -1 +1 @@ -4951 +4911 diff --git a/test/other/codesize/test_codesize_minimal_pthreads.jssize b/test/other/codesize/test_codesize_minimal_pthreads.jssize index 6cc48c769c97..7cf4960815af 100644 --- a/test/other/codesize/test_codesize_minimal_pthreads.jssize +++ b/test/other/codesize/test_codesize_minimal_pthreads.jssize @@ -1 +1 @@ -10465 +10376 diff --git a/test/test_browser.py b/test/test_browser.py index 3cfd27957684..b89def707d33 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -3847,11 +3847,12 @@ def test_pthread_condition_variable(self): # Test that pthreads are able to do printf. @parameterized({ - '': (False,), - 'debug': (True,), + '': ([],), + 'O3': (['-O3'],), + 'debug': (['-sLIBRARY_DEBUG'],), }) - def test_pthread_printf(self, debug): - self.btest_exit('pthread/test_pthread_printf.c', args=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE', '-sLIBRARY_DEBUG=%d' % debug]) + def test_pthread_printf(self, args): + self.btest_exit('pthread/test_pthread_printf.c', args=['-pthread', '-sPTHREAD_POOL_SIZE'] + args) # Test that pthreads are able to do cout. Failed due to https://bugzilla.mozilla.org/show_bug.cgi?id=1154858. def test_pthread_iostream(self):