diff --git a/system/lib/libc/musl/src/thread/__timedwait.c b/system/lib/libc/musl/src/thread/__timedwait.c index eb1e498b879ca..4dd08f8c58c59 100644 --- a/system/lib/libc/musl/src/thread/__timedwait.c +++ b/system/lib/libc/musl/src/thread/__timedwait.c @@ -59,20 +59,16 @@ 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(); - - // 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; // 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. - if (is_runtime_thread || - pthread_self()->canceldisable != PTHREAD_CANCEL_DISABLE || - pthread_self()->cancelasync) { + pthread_t self = pthread_self(); + if (!self->canceldisable) { + double max_ms_slice_to_sleep = 100; double sleepUntilTime = emscripten_get_now() + msecsToSleep; do { - if (pthread_self()->cancel) { + if (self->cancel) { // The thread was canceled by pthread_cancel(). // In the case of cancelasync or PTHREAD_CANCEL_ENABLE we can just call // __pthread_testcancel(), which won't return at all. diff --git a/system/lib/pthread/emscripten_futex_wait.c b/system/lib/pthread/emscripten_futex_wait.c index c7265abe84c73..88e6533a5ff5f 100644 --- a/system/lib/pthread/emscripten_futex_wait.c +++ b/system/lib/pthread/emscripten_futex_wait.c @@ -48,7 +48,8 @@ static int futex_wait_main_browser_thread(volatile void* addr, while (1) { #ifdef __EMSCRIPTEN_PTHREADS__ - if (cancelable && pthread_self()->cancel) { + if (cancelable) { + __pthread_testcancel(); return -ETIMEDOUT; } #endif @@ -130,7 +131,8 @@ int emscripten_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms) emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS_RUNNING, EM_THREAD_STATUS_WAITFUTEX); #ifdef __EMSCRIPTEN_PTHREADS__ - bool cancelable = pthread_self()->cancelasync == PTHREAD_CANCEL_ASYNCHRONOUS; + pthread_t self = pthread_self(); + bool cancelable = self->cancelasync; #else bool cancelable = false; #endif @@ -211,8 +213,8 @@ 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 && pthread_self()->cancel) { - // Break out of the loop early if we were cancelled + if (cancelable && ret == ATOMICS_WAIT_TIMED_OUT) { + __pthread_testcancel(); break; } // If remainder_ns is negative it means we want wait forever, and we don't diff --git a/test/codesize/test_codesize_minimal_pthreads.json b/test/codesize/test_codesize_minimal_pthreads.json index da85c8790e0ef..e57a28af67193 100644 --- a/test/codesize/test_codesize_minimal_pthreads.json +++ b/test/codesize/test_codesize_minimal_pthreads.json @@ -1,10 +1,10 @@ { "a.out.js": 7364, "a.out.js.gz": 3607, - "a.out.nodebug.wasm": 19265, - "a.out.nodebug.wasm.gz": 8934, - "total": 26629, - "total_gz": 12541, + "a.out.nodebug.wasm": 19231, + "a.out.nodebug.wasm.gz": 8905, + "total": 26595, + "total_gz": 12512, "sent": [ "a (memory)", "b (emscripten_get_now)", diff --git a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json index a96e1eea00ba9..6a535bf1fe27d 100644 --- a/test/codesize/test_codesize_minimal_pthreads_memgrowth.json +++ b/test/codesize/test_codesize_minimal_pthreads_memgrowth.json @@ -1,10 +1,10 @@ { "a.out.js": 7766, "a.out.js.gz": 3812, - "a.out.nodebug.wasm": 19266, - "a.out.nodebug.wasm.gz": 8935, - "total": 27032, - "total_gz": 12747, + "a.out.nodebug.wasm": 19234, + "a.out.nodebug.wasm.gz": 8919, + "total": 27000, + "total_gz": 12731, "sent": [ "a (memory)", "b (emscripten_get_now)", diff --git a/test/pthread/test_pthread_cancel_async.c b/test/pthread/test_pthread_cancel_async.c index 8b0a43416cd83..f9739e3a48cda 100644 --- a/test/pthread/test_pthread_cancel_async.c +++ b/test/pthread/test_pthread_cancel_async.c @@ -11,14 +11,24 @@ #include #include #include + +#ifdef __EMSCRIPTEN__ #include +#else +void emscripten_out(const char* msg) { + printf("%s\n", msg); +} + +void emscripten_outf(const char* msg, ...) { + printf("%s\n", msg); +} +#endif pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; _Atomic long res = 43; _Atomic int started = false; -static void cleanup_handler(void *arg) -{ +static void cleanup_handler(void *arg) { long a = (long)arg; emscripten_outf("Called clean-up handler with arg %ld", a); res -= a;