Skip to content

Commit 4bd0bc3

Browse files
authored
Fix EXPORT_NAME + MODULARIZE_INSTANCE + pthreads (#10449)
In that case we have just the custom EXPORT_NAME in the global scope, and must connect to that properly in the worker.js file which only sees the global scope. Also we cannot use out()/err() for logging as those are internals of the emitted code, which in MODULARIZE* mode are not available to worker.js as well. fixes #9091
1 parent fcf0bf8 commit 4bd0bc3

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/worker.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ var threadInfoStruct = 0; // Info area for this thread in Emscripten HEAP (share
1212
var selfThreadId = 0; // The ID of this thread. 0 if not hosting a pthread.
1313
var parentThreadId = 0; // The ID of the parent pthread that launched this thread.
1414

15-
// Cannot use console.log or console.error in a web worker, since that would risk a browser deadlock! https://bugzilla.mozilla.org/show_bug.cgi?id=1049091
16-
// Therefore implement custom logging facility for threads running in a worker, which queue the messages to main thread to print.
1715
var Module = {};
1816

17+
// If we use a custom export name, refer to Module from it as well, so that
18+
// we connect properly to the modularized instance which is the only thing
19+
// in the global scope.
20+
#if MODULARIZE_INSTANCE && EXPORT_NAME != 'Module'
21+
var {{{ EXPORT_NAME }}} = Module;
22+
#endif
23+
1924
#if ASSERTIONS
2025
function assert(condition, text) {
2126
if (!condition) abort('Assertion failed: ' + text);
@@ -25,12 +30,19 @@ function assert(condition, text) {
2530
function threadPrintErr() {
2631
var text = Array.prototype.slice.call(arguments).join(' ');
2732
console.error(text);
28-
console.error(new Error().stack);
2933
}
3034
function threadAlert() {
3135
var text = Array.prototype.slice.call(arguments).join(' ');
3236
postMessage({cmd: 'alert', text: text, threadId: selfThreadId});
3337
}
38+
#if ASSERTIONS
39+
// We don't need out() for now, but may need to add it if we want to use it
40+
// here. Or, if this code all moves into the main JS, that problem will go
41+
// away. (For now, adding it here increases code size for no benefit.)
42+
var out = function() {
43+
throw 'out() is not defined in worker.js.';
44+
}
45+
#endif
3446
var err = threadPrintErr;
3547
this.alert = threadAlert;
3648

@@ -258,7 +270,7 @@ this.onmessage = function(e) {
258270
#if ASSERTIONS
259271
} else {
260272
// else e == 'unwind', and we should fall through here and keep the pthread alive for asynchronous events.
261-
out('Pthread 0x' + threadInfoStruct.toString(16) + ' completed its pthread main entry point with an unwind, keeping the pthread worker alive for asynchronous operation.');
273+
err('Pthread 0x' + threadInfoStruct.toString(16) + ' completed its pthread main entry point with an unwind, keeping the pthread worker alive for asynchronous operation.');
262274
#endif
263275
}
264276
}
@@ -274,11 +286,11 @@ this.onmessage = function(e) {
274286
}
275287
} else {
276288
err('worker.js received unknown command ' + e.data.cmd);
277-
console.error(e.data);
289+
err(e.data);
278290
}
279291
} catch(ex) {
280-
console.error('worker.js onmessage() captured an uncaught exception: ' + ex);
281-
if (ex.stack) console.error(ex.stack);
292+
err('worker.js onmessage() captured an uncaught exception: ' + ex);
293+
if (ex.stack) err(ex.stack);
282294
throw ex;
283295
}
284296
};

tests/test_browser.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4961,3 +4961,18 @@ def test_wasm2js_fallback(self):
49614961
open('test.html', 'w').write(html)
49624962
os.remove('test.wasm') # Also delete the Wasm file to test that it is not attempted to be loaded.
49634963
self.run_browser('test.html', 'hello!', '/report_result?0')
4964+
4965+
# Test that basic thread creation works in combination with MODULARIZE_INSTANCE=1 and EXPORT_NAME=MyModule
4966+
@no_fastcomp('more work would be needed for this to work in deprecated fastcomp')
4967+
@requires_threads
4968+
def test_pthread_modularize_export_name(self):
4969+
create_test_file('shell.html', '''
4970+
<body>
4971+
{{{ SCRIPT }}}
4972+
</body>
4973+
''')
4974+
self.btest(path_from_root('tests', 'pthread', 'test_pthread_create.cpp'),
4975+
expected='0',
4976+
args=['-s', 'TOTAL_MEMORY=64MB', '-s', 'USE_PTHREADS=1', '-s',
4977+
'PTHREAD_POOL_SIZE=8', '-s', 'MODULARIZE_INSTANCE=1',
4978+
'-s', 'EXPORT_NAME=MyModule', '--shell-file', 'shell.html'])

0 commit comments

Comments
 (0)