@@ -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 ) {
0 commit comments