Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions system/lib/libc/musl/src/thread/__timedwait.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,14 @@ int __timedwait_cp(volatile int *addr, int val,

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

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

if (self->canceldisable != PTHREAD_CANCEL_DISABLE) {
double max_ms_slice_to_sleep = 100;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
double max_ms_slice_to_sleep = 100;
const double max_ms_slice_to_sleep = 100;

double sleepUntilTime = emscripten_get_now() + msecsToSleep;
do {
if (self->cancel) {
Expand All @@ -79,7 +75,7 @@ int __timedwait_cp(volatile int *addr, int val,
// __pthread_testcancel(), which won't return at all.
__pthread_testcancel();
// If __pthread_testcancel does return here it means that canceldisable
// must be set to PTHREAD_CANCEL_MASKED. This appear to mean "return
// must be set to PTHREAD_CANCEL_MASKED. This appears to mean "return
// ECANCELLED to the caller". See pthread_cond_timedwait.c for the only
// use of this that I could find.
return ECANCELED;
Expand Down
3 changes: 2 additions & 1 deletion system/lib/pthread/emscripten_futex_wait.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static int futex_wait_main_browser_thread(volatile void* addr,
while (1) {
#ifdef __EMSCRIPTEN_PTHREADS__
if (cancelable && pthread_self()->cancel) {
__pthread_testcancel();
return -ETIMEDOUT;
}
#endif
Expand Down Expand Up @@ -213,7 +214,7 @@ int emscripten_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms)
#endif
#ifdef __EMSCRIPTEN_PTHREADS__
if (cancelable && ret == ATOMICS_WAIT_TIMED_OUT && self->cancel) {
// Break out of the loop early if we were cancelled
__pthread_testcancel();
break;
}
// If remainder_ns is negative it means we want wait forever, and we don't
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_minimal_pthreads.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 7363,
"a.out.js.gz": 3604,
"a.out.nodebug.wasm": 19256,
"a.out.nodebug.wasm.gz": 8935,
"total": 26619,
"total_gz": 12539,
"a.out.nodebug.wasm": 19239,
"a.out.nodebug.wasm.gz": 8916,
"total": 26602,
"total_gz": 12520,
"sent": [
"a (memory)",
"b (emscripten_get_now)",
Expand Down
8 changes: 4 additions & 4 deletions test/codesize/test_codesize_minimal_pthreads_memgrowth.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 7765,
"a.out.js.gz": 3810,
"a.out.nodebug.wasm": 19257,
"a.out.nodebug.wasm.gz": 8936,
"total": 27022,
"total_gz": 12746,
"a.out.nodebug.wasm": 19240,
"a.out.nodebug.wasm.gz": 8917,
"total": 27005,
"total_gz": 12727,
"sent": [
"a (memory)",
"b (emscripten_get_now)",
Expand Down
Loading