@@ -402,38 +402,17 @@ int emscripten_proxy_sync_with_ctx(em_proxying_queue* q,
402
402
return ret ;
403
403
}
404
404
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
-
428
405
// Helper for signaling the end of the task after the user function returns.
429
406
static void call_then_finish_task (em_proxying_ctx * ctx , void * arg ) {
430
407
task * t = arg ;
431
408
t -> func (t -> arg );
432
409
emscripten_proxy_finish (ctx );
433
410
}
434
411
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 ) {
436
413
task * t = arg ;
414
+ proxied_js_func_t * p = t -> arg ;
415
+ p -> ctx = ctx ;
437
416
t -> func (t -> arg );
438
417
}
439
418
@@ -446,15 +425,6 @@ int emscripten_proxy_sync(em_proxying_queue* q,
446
425
q , target_thread , call_then_finish_task , & t );
447
426
}
448
427
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
-
458
428
static int do_proxy_callback (em_proxying_queue * q ,
459
429
pthread_t target_thread ,
460
430
void (* func )(em_proxying_ctx * ctx , void * ),
@@ -635,25 +605,18 @@ em_promise_t emscripten_proxy_promise(em_proxying_queue* q,
635
605
static void run_js_func (void * arg ) {
636
606
proxied_js_func_t * f = (proxied_js_func_t * )arg ;
637
607
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 );
639
609
if (f -> owned ) {
640
610
free (f -> argBuffer );
641
611
free (f );
642
612
}
643
613
}
644
614
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
-
651
615
double _emscripten_run_on_main_thread_js (int func_index ,
652
616
void * em_asm_addr ,
653
617
int num_args ,
654
618
double * buffer ,
655
- int sync ,
656
- int asyncAwait ) {
619
+ int sync ) {
657
620
proxied_js_func_t f = {
658
621
.funcIndex = func_index ,
659
622
.emAsmAddr = em_asm_addr ,
@@ -666,15 +629,8 @@ double _emscripten_run_on_main_thread_js(int func_index,
666
629
em_proxying_queue * q = emscripten_proxy_get_system_queue ();
667
630
pthread_t target = emscripten_main_runtime_thread_id ();
668
631
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" );
671
632
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 )) {
678
634
assert (false && "emscripten_proxy_sync failed" );
679
635
return 0 ;
680
636
}
@@ -696,6 +652,30 @@ double _emscripten_run_on_main_thread_js(int func_index,
696
652
return 0 ;
697
653
}
698
654
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
+
699
679
void _emscripten_proxy_promise_finish (em_proxying_ctx * ctx , void * res ) {
700
680
task * t = (task * )ctx -> arg ;
701
681
proxied_js_func_t * func = (proxied_js_func_t * )t -> arg ;
0 commit comments