Skip to content

Commit aaf0481

Browse files
committed
[JSPI] Fix PROXY_TO_PTHREAD when used with JSPI.
Use a WebAssembly.Promising wrapper on the function that calls main to allow JSPI to be used from the main (worker) thread. The code that invokes the wrapper also adds async/await to handle this async call to main. Fixes #22354
1 parent 44c7540 commit aaf0481

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

src/library_pthread.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ var LibraryPThread = {
10431043
'$runtimeKeepaliveCounter',
10441044
#endif
10451045
],
1046-
$invokeEntryPoint: (ptr, arg) => {
1046+
$invokeEntryPoint: {{{ asyncIf(ASYNCIFY == 2) }}} (ptr, arg) => {
10471047
#if PTHREADS_DEBUG
10481048
dbg(`invokeEntryPoint: ${ptrToString(ptr)}`);
10491049
#endif
@@ -1079,7 +1079,11 @@ var LibraryPThread = {
10791079
// *ThreadMain(void *arg) form, or try linking with the Emscripten linker
10801080
// flag -sEMULATE_FUNCTION_POINTER_CASTS to add in emulation for this x86
10811081
// ABI extension.
1082+
#if ASYNCIFY == 2
1083+
var result = WebAssembly.promising({{{ makeDynCall('pp', 'ptr') }}})(arg);
1084+
#else
10821085
var result = {{{ makeDynCall('pp', 'ptr') }}}(arg);
1086+
#endif
10831087
#if STACK_OVERFLOW_CHECK
10841088
checkStackCookie();
10851089
#endif
@@ -1098,10 +1102,9 @@ var LibraryPThread = {
10981102
#endif
10991103
}
11001104
#if ASYNCIFY == 2
1101-
Promise.resolve(result).then(finish);
1102-
#else
1103-
finish(result);
1105+
result = await result;
11041106
#endif
1107+
finish(result);
11051108
},
11061109
11071110
#if MAIN_MODULE

src/runtime_pthread.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ if (ENVIRONMENT_IS_PTHREAD) {
9090
// notified about them.
9191
self.onunhandledrejection = (e) => { throw e.reason || e; };
9292

93-
function handleMessage(e) {
93+
{{{ asyncIf(ASYNCIFY == 2) }}} function handleMessage(e) {
9494
try {
9595
var msgData = e['data'];
9696
//dbg('msgData: ' + Object.keys(msgData));
@@ -201,7 +201,7 @@ if (ENVIRONMENT_IS_PTHREAD) {
201201
}
202202

203203
try {
204-
invokeEntryPoint(msgData.start_routine, msgData.arg);
204+
{{{ awaitIf(ASYNCIFY == 2) }}} invokeEntryPoint(msgData.start_routine, msgData.arg);
205205
} catch(ex) {
206206
if (ex != 'unwind') {
207207
// The pthread "crashed". Do not call `_emscripten_thread_exit` (which

test/test_other.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3316,6 +3316,20 @@ def test_jspi_add_function(self):
33163316
'-Wno-experimental']
33173317
self.do_runf('other/test_jspi_add_function.c', 'done')
33183318

3319+
@requires_node_canary
3320+
@requires_jspi
3321+
def test_jspi_proxy_to_pthread(self):
3322+
create_file('main.c', r'''
3323+
#include <stdio.h>
3324+
#include <emscripten/emscripten.h>
3325+
int main() {
3326+
emscripten_sleep(1);
3327+
printf("ok\n");
3328+
}
3329+
''')
3330+
self.do_runf('main.c', 'ok',
3331+
emcc_args=['-sJSPI', '-pthread', '-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'])
3332+
33193333
@parameterized({
33203334
'': [[]],
33213335
'with_jsgen': [['-sEMBIND_AOT']]

0 commit comments

Comments
 (0)