@@ -982,8 +982,9 @@ var LibraryPThread = {
982982
983983 _emscripten_receive_on_main_thread_js__deps: [
984984 '$proxyToMainThread',
985+ '_emscripten_run_js_on_main_thread_done',
985986 '$proxiedJSCallArgs'],
986- _emscripten_receive_on_main_thread_js: (funcIndex, emAsmAddr, callingThread, bufSize, args) => {
987+ _emscripten_receive_on_main_thread_js: (funcIndex, emAsmAddr, callingThread, bufSize, args, ctx, ctxArgs ) => {
987988 // Sometimes we need to backproxy events to the calling thread (e.g.
988989 // HTML5 DOM events handlers such as
989990 // emscripten_set_mousemove_callback()), so keep track in a globally
@@ -1022,6 +1023,11 @@ var LibraryPThread = {
10221023 PThread.currentProxiedOperationCallerThread = callingThread;
10231024 var rtn = func(...proxiedJSCallArgs);
10241025 PThread.currentProxiedOperationCallerThread = 0;
1026+ if (ctx) {
1027+ rtn.then((rtn) => __emscripten_run_js_on_main_thread_done(ctx, ctxArgs, rtn));
1028+ return;
1029+ }
1030+
10251031#if MEMORY64
10261032 // In memory64 mode some proxied functions return bigint/pointer but
10271033 // our return type is i53/double.
@@ -1160,14 +1166,11 @@ var LibraryPThread = {
11601166 }
11611167 },
11621168
1163- // Asynchronous version dlsync_threads. Always run on the main thread.
1164- // This work happens asynchronously. The `callback` is called once this work
1165- // is completed, passing the ctx.
1166- // TODO(sbc): Should we make a new form of __proxy attribute for JS library
1167- // function that run asynchronously like but blocks the caller until they are
1168- // done. Perhaps "sync_with_ctx"?
1169- _emscripten_dlsync_threads_async__deps : [ '_emscripten_proxy_dlsync_async' , '$makePromise' ] ,
1170- _emscripten_dlsync_threads_async : ( caller , callback , ctx ) => {
1169+ // Asynchronous version dlsync. Always run on the main thread.
1170+ // This work happens asynchronously.
1171+ $dlsyncAsync__deps: ['_emscripten_proxy_dlsync_async', '$makePromise'],
1172+ $dlsyncAsync: async () => {
1173+ const caller = PThread.currentProxiedOperationCallerThread;
11711174#if PTHREADS_DEBUG
11721175 dbg("_emscripten_dlsync_threads_async caller=" + ptrToString(caller));
11731176#endif
@@ -1202,27 +1205,27 @@ var LibraryPThread = {
12021205#if PTHREADS_DEBUG
12031206 dbg(` _emscripten_dlsync_threads_async : waiting on ${promises . length } promises `);
12041207#endif
1205- // Once all promises are resolved then we know all threads are in sync and
1206- // we can call the callback.
1207- Promise . all ( promises ) . then ( ( ) => {
1208- PThread . outstandingPromises = { } ;
1208+ await Promise.all(promises);
1209+
1210+ PThread.outstandingPromises = {};
12091211#if PTHREADS_DEBUG
1210- dbg ( '_emscripten_dlsync_threads_async done: calling callback ' ) ;
1212+ dbg('_emscripten_dlsync_threads_async done');
12111213#endif
1212- { { { makeDynCall ( 'vp' , 'callback' ) } } } ( ctx ) ;
1213- } ) ;
12141214 },
12151215
1216- // Synchronous version dlsync_threads. This is only needed for the case then
1216+ // Synchronous version of dlsync_threads. This is only needed for the case then
12171217 // the main thread call dlopen and in that case we have not choice but to
12181218 // synchronously block the main thread until all other threads are in sync.
12191219 // When ` dlopen ` is called from a worker, the worker itself is blocked but
12201220 // the operation its waiting on (on the main thread) can be async.
1221- _emscripten_dlsync_threads__deps : [ '_emscripten_proxy_dlsync' ] ,
1222- _emscripten_dlsync_threads : ( ) => {
1223- #if ASSERTIONS
1224- assert ( ! ENVIRONMENT_IS_PTHREAD , 'Internal Error! _emscripten_dlsync_threads() can only ever be called from main thread' ) ;
1225- #endif
1221+ _dlsync__deps: ['_emscripten_proxy_dlsync', '$dlsyncAsync'],
1222+ _dlsync__async: true,
1223+ _dlsync__proxy: 'sync',
1224+ _dlsync: () => {
1225+ const callingThread = PThread.currentProxiedOperationCallerThread;
1226+ if (callingThread) {
1227+ return dlsyncAsync()
1228+ }
12261229 for (const ptr of Object.keys(PThread.pthreads)) {
12271230 const pthread_ptr = Number(ptr);
12281231 if (!PThread.finishedThreads.has(pthread_ptr)) {
0 commit comments