Skip to content

Commit 10e9935

Browse files
authored
Move code from worker.js in library_pthread.js invokeEntryPoint (#17087)
This is good for code size and means we can perhaps export fewer symbols to the worker in future.
1 parent 3b36c26 commit 10e9935

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

src/library_pthread.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ var LibraryPThread = {
3131
$PThread__deps: ['_emscripten_thread_init',
3232
'$killThread',
3333
'$cancelThread', '$cleanupThread', '$zeroMemory',
34-
'$ptrToString', '$spawnThread',
34+
'$spawnThread',
3535
'_emscripten_thread_free_data',
3636
'exit',
37+
#if PTHREADS_DEBUG || ASSERTIONS
38+
'$ptrToString',
39+
#endif
3740
#if !MINIMAL_RUNTIME
3841
'$handleException',
3942
#endif
@@ -979,6 +982,7 @@ var LibraryPThread = {
979982
#endif
980983
},
981984

985+
$invokeEntryPoint__deps: ['_emscripten_thread_exit'],
982986
$invokeEntryPoint: function(ptr, arg) {
983987
#if PTHREADS_DEBUG
984988
err('invokeEntryPoint: ' + ptrToString(ptr));
@@ -989,7 +993,31 @@ var LibraryPThread = {
989993
// in sync and might not contain the function pointer `ptr` at all.
990994
__emscripten_thread_sync_code();
991995
#endif
992-
return {{{ makeDynCall('ii', 'ptr') }}}(arg);
996+
// pthread entry points are always of signature 'void *ThreadMain(void *arg)'
997+
// Native codebases sometimes spawn threads with other thread entry point
998+
// signatures, such as void ThreadMain(void *arg), void *ThreadMain(), or
999+
// void ThreadMain(). That is not acceptable per C/C++ specification, but
1000+
// x86 compiler ABI extensions enable that to work. If you find the
1001+
// following line to crash, either change the signature to "proper" void
1002+
// *ThreadMain(void *arg) form, or try linking with the Emscripten linker
1003+
// flag -sEMULATE_FUNCTION_POINTER_CASTS to add in emulation for this x86
1004+
// ABI extension.
1005+
var result = {{{ makeDynCall('ii', 'ptr') }}}(arg);
1006+
#if STACK_OVERFLOW_CHECK
1007+
checkStackCookie();
1008+
#endif
1009+
#if MINIMAL_RUNTIME
1010+
// In MINIMAL_RUNTIME the noExitRuntime concept does not apply to
1011+
// pthreads. To exit a pthread with live runtime, use the function
1012+
// emscripten_unwind_to_js_event_loop() in the pthread body.
1013+
__emscripten_thread_exit(result);
1014+
#else
1015+
if (keepRuntimeAlive()) {
1016+
PThread.setExitStatus(result);
1017+
} else {
1018+
__emscripten_thread_exit(result);
1019+
}
1020+
#endif
9931021
},
9941022

9951023
$executeNotifiedProxyingQueue: function(queue) {

src/worker.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -232,33 +232,7 @@ self.onmessage = (e) => {
232232
}
233233

234234
try {
235-
// pthread entry points are always of signature 'void *ThreadMain(void *arg)'
236-
// Native codebases sometimes spawn threads with other thread entry point signatures,
237-
// such as void ThreadMain(void *arg), void *ThreadMain(), or void ThreadMain().
238-
// That is not acceptable per C/C++ specification, but x86 compiler ABI extensions
239-
// enable that to work. If you find the following line to crash, either change the signature
240-
// to "proper" void *ThreadMain(void *arg) form, or try linking with the Emscripten linker
241-
// flag -sEMULATE_FUNCTION_POINTER_CASTS to add in emulation for this x86 ABI extension.
242-
var result = Module['invokeEntryPoint'](e.data.start_routine, e.data.arg);
243-
244-
#if STACK_OVERFLOW_CHECK
245-
Module['checkStackCookie']();
246-
#endif
247-
#if MINIMAL_RUNTIME
248-
// In MINIMAL_RUNTIME the noExitRuntime concept does not apply to
249-
// pthreads. To exit a pthread with live runtime, use the function
250-
// emscripten_unwind_to_js_event_loop() in the pthread body.
251-
// The thread might have finished without calling pthread_exit(). If so,
252-
// then perform the exit operation ourselves.
253-
// (This is a no-op if explicit pthread_exit() had been called prior.)
254-
Module['__emscripten_thread_exit'](result);
255-
#else
256-
if (Module['keepRuntimeAlive']()) {
257-
Module['PThread'].setExitStatus(result);
258-
} else {
259-
Module['__emscripten_thread_exit'](result);
260-
}
261-
#endif
235+
Module['invokeEntryPoint'](e.data.start_routine, e.data.arg);
262236
} catch(ex) {
263237
if (ex != 'unwind') {
264238
// ExitStatus not present in MINIMAL_RUNTIME
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
16236
1+
16127

0 commit comments

Comments
 (0)