Skip to content

Commit ae2357d

Browse files
committed
simplified
1 parent c2edefd commit ae2357d

File tree

4 files changed

+42
-52
lines changed

4 files changed

+42
-52
lines changed

src/library_pthread.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ var LibraryPThread = {
893893
$proxyToMainThreadPtr: (...args) => BigInt(proxyToMainThread(...args)),
894894
#endif
895895
896-
$proxyToMainThread__deps: ['$stackSave', '$stackRestore', '$stackAlloc', '_emscripten_run_on_main_thread_js', ...i53ConversionDeps],
896+
$proxyToMainThread__deps: ['$stackSave', '$stackRestore', '$stackAlloc', '_emscripten_run_on_main_thread_js', '_emscripten_await_on_main_thread_js', ...i53ConversionDeps],
897897
$proxyToMainThread__docs: '/** @type{function(number, (number|boolean), ...number)} */',
898898
$proxyToMainThread: (funcIndex, emAsmAddr, sync, asyncAwait, ...callArgs) => {
899899
// EM_ASM proxying is done by passing a pointer to the address of the EM_ASM
@@ -933,7 +933,12 @@ var LibraryPThread = {
933933
HEAPF64[b + i] = arg;
934934
#endif
935935
}
936-
var rtn = __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync, asyncAwait);
936+
var rtn;
937+
if (asyncAwait) {
938+
rtn = __emscripten_await_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args);
939+
} else {
940+
rtn = __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync);
941+
}
937942
stackRestore(sp);
938943
return rtn;
939944
},

system/lib/pthread/proxying.c

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
429406
static 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-
458428
static 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,
635605
static 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-
651615
double _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+
699679
void _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;

tools/emscripten.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,7 @@ def create_pointer_conversion_wrappers(metadata):
10641064
'_wasmfs_read_file': 'pp',
10651065
'__dl_seterr': '_pp',
10661066
'_emscripten_run_on_main_thread_js': '__p_p_',
1067+
'_emscripten_await_on_main_thread_js': '__p_p_',
10671068
'_emscripten_proxy_promise_finish': 'pp',
10681069
'_emscripten_proxy_execute_task_queue': '_p',
10691070
'_emscripten_thread_exit': '_p',

tools/link.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,10 @@ def setup_pthreads():
499499
settings.REQUIRED_EXPORTS += [
500500
'_emscripten_thread_free_data',
501501
'_emscripten_thread_crashed',
502+
'emscripten_main_runtime_thread_id',
503+
'emscripten_main_thread_process_queued_calls',
504+
'_emscripten_run_on_main_thread_js',
505+
'_emscripten_await_on_main_thread_js',
502506
'_emscripten_proxy_promise_finish',
503507
]
504508

0 commit comments

Comments
 (0)