Skip to content

Commit 905d8d4

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 a56a99f commit 905d8d4

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,16 @@ 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.
70-
if (is_runtime_thread ||
71-
pthread_self()->canceldisable != PTHREAD_CANCEL_DISABLE ||
72-
pthread_self()->cancelasync) {
66+
pthread_t self = pthread_self();
67+
if (!self->canceldisable) {
68+
double max_ms_slice_to_sleep = 100;
7369
double sleepUntilTime = emscripten_get_now() + msecsToSleep;
7470
do {
75-
if (pthread_self()->cancel) {
71+
if (self->cancel) {
7672
// The thread was canceled by pthread_cancel().
7773
// In the case of cancelasync or PTHREAD_CANCEL_ENABLE we can just call
7874
// __pthread_testcancel(), which won't return at all.

system/lib/pthread/emscripten_futex_wait.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ static int futex_wait_main_browser_thread(volatile void* addr,
4848

4949
while (1) {
5050
#ifdef __EMSCRIPTEN_PTHREADS__
51-
if (cancelable && pthread_self()->cancel) {
51+
if (cancelable) {
52+
__pthread_testcancel();
5253
return -ETIMEDOUT;
5354
}
5455
#endif
@@ -130,7 +131,8 @@ int emscripten_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms)
130131
emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS_RUNNING, EM_THREAD_STATUS_WAITFUTEX);
131132

132133
#ifdef __EMSCRIPTEN_PTHREADS__
133-
bool cancelable = pthread_self()->cancelasync == PTHREAD_CANCEL_ASYNCHRONOUS;
134+
pthread_t self = pthread_self();
135+
bool cancelable = self->cancelasync;
134136
#else
135137
bool cancelable = false;
136138
#endif
@@ -211,8 +213,8 @@ int emscripten_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms)
211213
}
212214
#endif
213215
#ifdef __EMSCRIPTEN_PTHREADS__
214-
if (cancelable && ret == ATOMICS_WAIT_TIMED_OUT && pthread_self()->cancel) {
215-
// Break out of the loop early if we were cancelled
216+
if (cancelable && ret == ATOMICS_WAIT_TIMED_OUT) {
217+
__pthread_testcancel();
216218
break;
217219
}
218220
// 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": 7364,
33
"a.out.js.gz": 3607,
4-
"a.out.nodebug.wasm": 19265,
5-
"a.out.nodebug.wasm.gz": 8934,
6-
"total": 26629,
7-
"total_gz": 12541,
4+
"a.out.nodebug.wasm": 19231,
5+
"a.out.nodebug.wasm.gz": 8905,
6+
"total": 26595,
7+
"total_gz": 12512,
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": 7766,
33
"a.out.js.gz": 3812,
4-
"a.out.nodebug.wasm": 19266,
5-
"a.out.nodebug.wasm.gz": 8935,
6-
"total": 27032,
7-
"total_gz": 12747,
4+
"a.out.nodebug.wasm": 19234,
5+
"a.out.nodebug.wasm.gz": 8919,
6+
"total": 27000,
7+
"total_gz": 12731,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",

test/pthread/test_pthread_cancel_async.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,24 @@
1111
#include <assert.h>
1212
#include <unistd.h>
1313
#include <errno.h>
14+
15+
#ifdef __EMSCRIPTEN__
1416
#include <emscripten/console.h>
17+
#else
18+
void emscripten_out(const char* msg) {
19+
printf("%s\n", msg);
20+
}
21+
22+
void emscripten_outf(const char* msg, ...) {
23+
printf("%s\n", msg);
24+
}
25+
#endif
1526

1627
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1728
_Atomic long res = 43;
1829
_Atomic int started = false;
1930

20-
static void cleanup_handler(void *arg)
21-
{
31+
static void cleanup_handler(void *arg) {
2232
long a = (long)arg;
2333
emscripten_outf("Called clean-up handler with arg %ld", a);
2434
res -= a;

0 commit comments

Comments
 (0)