@@ -402,38 +402,17 @@ int emscripten_proxy_sync_with_ctx(em_proxying_queue* q,
402402 return ret ;
403403}
404404
405- int emscripten_proxy_async_await_with_ctx (em_proxying_queue * q ,
406- pthread_t target_thread ,
407- void (* func )(em_proxying_ctx * , void * ),
408- void * arg , proxied_js_func_t * f ) {
409- assert (!pthread_equal (target_thread , pthread_self ()) &&
410- "Cannot synchronously wait for work proxied to the current thread" );
411- em_proxying_ctx ctx ;
412- em_proxying_ctx_init_sync (& ctx , func , arg );
413- f -> ctx = & ctx ;
414- if (!do_proxy (q , target_thread , (task ){call_with_ctx , cancel_ctx , & ctx })) {
415- em_proxying_ctx_deinit (& ctx );
416- return 0 ;
417- }
418- pthread_mutex_lock (& ctx .sync .mutex );
419- while (ctx .sync .state == PENDING ) {
420- pthread_cond_wait (& ctx .sync .cond , & ctx .sync .mutex );
421- }
422- pthread_mutex_unlock (& ctx .sync .mutex );
423- int ret = ctx .sync .state == DONE ;
424- em_proxying_ctx_deinit (& ctx );
425- return ret ;
426- }
427-
428405// Helper for signaling the end of the task after the user function returns.
429406static void call_then_finish_task (em_proxying_ctx * ctx , void * arg ) {
430407 task * t = arg ;
431408 t -> func (t -> arg );
432409 emscripten_proxy_finish (ctx );
433410}
434411
435- static void call_task (em_proxying_ctx * ctx , void * arg ) {
412+ static void call_proxied_js_task_with_ctx (em_proxying_ctx * ctx , void * arg ) {
436413 task * t = arg ;
414+ proxied_js_func_t * p = t -> arg ;
415+ p -> ctx = ctx ;
437416 t -> func (t -> arg );
438417}
439418
@@ -446,15 +425,6 @@ int emscripten_proxy_sync(em_proxying_queue* q,
446425 q , target_thread , call_then_finish_task , & t );
447426}
448427
449- int emscripten_proxy_async_await (em_proxying_queue * q ,
450- pthread_t target_thread ,
451- void (* func )(void * ),
452- proxied_js_func_t * f ) {
453- task t = {.func = func , .arg = (void * )f };
454- return emscripten_proxy_async_await_with_ctx (
455- q , target_thread , call_task , & t , f );
456- }
457-
458428static int do_proxy_callback (em_proxying_queue * q ,
459429 pthread_t target_thread ,
460430 void (* func )(em_proxying_ctx * ctx , void * ),
@@ -635,25 +605,18 @@ em_promise_t emscripten_proxy_promise(em_proxying_queue* q,
635605static void run_js_func (void * arg ) {
636606 proxied_js_func_t * f = (proxied_js_func_t * )arg ;
637607 f -> result = _emscripten_receive_on_main_thread_js (
638- f -> funcIndex , f -> emAsmAddr , f -> callingThread , f -> numArgs , f -> argBuffer , NULL );
608+ f -> funcIndex , f -> emAsmAddr , f -> callingThread , f -> numArgs , f -> argBuffer , f -> ctx );
639609 if (f -> owned ) {
640610 free (f -> argBuffer );
641611 free (f );
642612 }
643613}
644614
645- static void run_js_async_await (void * arg ) {
646- proxied_js_func_t * f = (proxied_js_func_t * )arg ;
647- f -> result = _emscripten_receive_on_main_thread_js (
648- f -> funcIndex , f -> emAsmAddr , f -> callingThread , f -> numArgs , f -> argBuffer , (void * )f -> ctx );
649- }
650-
651615double _emscripten_run_on_main_thread_js (int func_index ,
652616 void * em_asm_addr ,
653617 int num_args ,
654618 double * buffer ,
655- int sync ,
656- int asyncAwait ) {
619+ int sync ) {
657620 proxied_js_func_t f = {
658621 .funcIndex = func_index ,
659622 .emAsmAddr = em_asm_addr ,
@@ -666,15 +629,8 @@ double _emscripten_run_on_main_thread_js(int func_index,
666629 em_proxying_queue * q = emscripten_proxy_get_system_queue ();
667630 pthread_t target = emscripten_main_runtime_thread_id ();
668631
669- // make sure it is not the case that sync==false and asyncAwait==true
670- assert (!(sync == 0 && asyncAwait == 1 ) && "asyncAwait cannot be true if sync is false" );
671632 if (sync ) {
672- if (asyncAwait ) {
673- if (!emscripten_proxy_async_await (q , target , run_js_async_await , & f )) {
674- assert (false && "emscripten_proxy_promise failed" );
675- return 0 ;
676- }
677- } else if (!emscripten_proxy_sync (q , target , run_js_func , & f )) {
633+ if (!emscripten_proxy_sync (q , target , run_js_func , & f )) {
678634 assert (false && "emscripten_proxy_sync failed" );
679635 return 0 ;
680636 }
@@ -696,6 +652,30 @@ double _emscripten_run_on_main_thread_js(int func_index,
696652 return 0 ;
697653}
698654
655+ double _emscripten_await_on_main_thread_js (int func_index ,
656+ void * em_asm_addr ,
657+ int num_args ,
658+ double * buffer ) {
659+ em_proxying_queue * q = emscripten_proxy_get_system_queue ();
660+ pthread_t target = emscripten_main_runtime_thread_id ();
661+
662+ proxied_js_func_t f = {
663+ .funcIndex = func_index ,
664+ .emAsmAddr = em_asm_addr ,
665+ .callingThread = pthread_self (),
666+ .numArgs = num_args ,
667+ .argBuffer = buffer ,
668+ .owned = false,
669+ };
670+ task t = {.func = run_js_func , .arg = & f };
671+
672+ if (!emscripten_proxy_sync_with_ctx (q , target , call_proxied_js_task_with_ctx , & t )) {
673+ assert (false && "emscripten_proxy_sync_with_ctx failed" );
674+ return 0 ;
675+ }
676+ return f .result ;
677+ }
678+
699679void _emscripten_proxy_promise_finish (em_proxying_ctx * ctx , void * res ) {
700680 task * t = (task * )ctx -> arg ;
701681 proxied_js_func_t * func = (proxied_js_func_t * )t -> arg ;
0 commit comments