Skip to content

Commit 0fbb41a

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 d1f7fa6 commit 0fbb41a

File tree

4 files changed

+13
-17
lines changed

4 files changed

+13
-17
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,13 @@ 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();
63-
64-
// Main runtime thread may need to run proxied calls, so sleep in very small slices to be responsive.
65-
double max_ms_slice_to_sleep = is_runtime_thread ? 1 : 100;
6662

6763
// cp suffix in the function name means "cancellation point", so this wait can be cancelled
6864
// by the users unless current threads cancelability is set to PTHREAD_CANCEL_DISABLE
6965
// which may be either done by the user of __timedwait() function.
7066
pthread_t self = pthread_self();
71-
if (is_runtime_thread ||
72-
self->canceldisable != PTHREAD_CANCEL_DISABLE ||
73-
self->cancelasync) {
67+
if (self->canceldisable != PTHREAD_CANCEL_DISABLE) {
68+
double max_ms_slice_to_sleep = 100;
7469
double sleepUntilTime = emscripten_get_now() + msecsToSleep;
7570
do {
7671
if (self->cancel) {
@@ -79,7 +74,7 @@ int __timedwait_cp(volatile int *addr, int val,
7974
// __pthread_testcancel(), which won't return at all.
8075
__pthread_testcancel();
8176
// If __pthread_testcancel does return here it means that canceldisable
82-
// must be set to PTHREAD_CANCEL_MASKED. This appear to mean "return
77+
// must be set to PTHREAD_CANCEL_MASKED. This appears to mean "return
8378
// ECANCELLED to the caller". See pthread_cond_timedwait.c for the only
8479
// use of this that I could find.
8580
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": 19259,
5-
"a.out.nodebug.wasm.gz": 8931,
6-
"total": 26622,
7-
"total_gz": 12535,
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": 19260,
5-
"a.out.nodebug.wasm.gz": 8933,
6-
"total": 27025,
7-
"total_gz": 12743,
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)