Skip to content

Commit 8ef03ec

Browse files
committed
Remove some of the complexity from __timedwait.c. NFC
This should have been part of #26471. The breaking up of the wait time for 2 of the 3 cases that are handled here is now handled one layer own in emscripten_futex_wait: 1. Breaking up the wait because we are the main runtime thread. 2. Breaking up the wait because we are async cancelable. The third cases here (breaking up the wait because we are cancelable in the non-async sense) still needs to be handled here at the higher level.
1 parent fc50a96 commit 8ef03ec

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

system/lib/libc/musl/src/thread/__timedwait.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,14 @@ int __timedwait_cp(volatile int *addr, int val,
5959

6060
#ifdef __EMSCRIPTEN__
6161
double msecsToSleep = top ? (top->tv_sec * 1000 + top->tv_nsec / 1000000.0) : INFINITY;
62-
int is_runtime_thread = emscripten_is_main_runtime_thread();
6362

6463
// cp suffix in the function name means "cancellation point", so this wait can be cancelled
6564
// by the users unless current threads cancelability is set to PTHREAD_CANCEL_DISABLE
6665
// which may be either done by the user of __timedwait() function.
6766
pthread_t self = pthread_self();
68-
if (is_runtime_thread ||
69-
self->canceldisable != PTHREAD_CANCEL_DISABLE ||
70-
self->cancelasync) {
71-
// Main runtime thread may need to run proxied calls, so sleep in very small slices to be responsive.
72-
double max_ms_slice_to_sleep = is_runtime_thread ? 1 : 100;
7367

68+
if (self->canceldisable != PTHREAD_CANCEL_DISABLE) {
69+
double max_ms_slice_to_sleep = 100;
7470
double sleepUntilTime = emscripten_get_now() + msecsToSleep;
7571
do {
7672
if (self->cancel) {
@@ -79,7 +75,7 @@ int __timedwait_cp(volatile int *addr, int val,
7975
// __pthread_testcancel(), which won't return at all.
8076
__pthread_testcancel();
8177
// If __pthread_testcancel does return here it means that canceldisable
82-
// must be set to PTHREAD_CANCEL_MASKED. This appear to mean "return
78+
// must be set to PTHREAD_CANCEL_MASKED. This appears to mean "return
8379
// ECANCELLED to the caller". See pthread_cond_timedwait.c for the only
8480
// use of this that I could find.
8581
return ECANCELED;

system/lib/pthread/emscripten_futex_wait.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static int futex_wait_main_browser_thread(volatile void* addr,
4949
while (1) {
5050
#ifdef __EMSCRIPTEN_PTHREADS__
5151
if (cancelable && pthread_self()->cancel) {
52+
__pthread_testcancel();
5253
return -ETIMEDOUT;
5354
}
5455
#endif
@@ -213,7 +214,7 @@ int emscripten_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms)
213214
#endif
214215
#ifdef __EMSCRIPTEN_PTHREADS__
215216
if (cancelable && ret == ATOMICS_WAIT_TIMED_OUT && self->cancel) {
216-
// Break out of the loop early if we were cancelled
217+
__pthread_testcancel();
217218
break;
218219
}
219220
// If remainder_ns is negative it means we want wait forever, and we don't

test/codesize/test_codesize_minimal_pthreads.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 7363,
33
"a.out.js.gz": 3604,
4-
"a.out.nodebug.wasm": 19256,
5-
"a.out.nodebug.wasm.gz": 8935,
6-
"total": 26619,
7-
"total_gz": 12539,
4+
"a.out.nodebug.wasm": 19239,
5+
"a.out.nodebug.wasm.gz": 8916,
6+
"total": 26602,
7+
"total_gz": 12520,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",

test/codesize/test_codesize_minimal_pthreads_memgrowth.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 7765,
33
"a.out.js.gz": 3810,
4-
"a.out.nodebug.wasm": 19257,
5-
"a.out.nodebug.wasm.gz": 8936,
6-
"total": 27022,
7-
"total_gz": 12746,
4+
"a.out.nodebug.wasm": 19240,
5+
"a.out.nodebug.wasm.gz": 8917,
6+
"total": 27005,
7+
"total_gz": 12727,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",

0 commit comments

Comments
 (0)