Skip to content

Commit 6814b6d

Browse files
authored
Cache the result of pthread_self in low level loops. NFC (#26531)
1 parent 3726672 commit 6814b6d

4 files changed

Lines changed: 13 additions & 11 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,13 @@ int __timedwait_cp(volatile int *addr, int val,
6767
// cp suffix in the function name means "cancellation point", so this wait can be cancelled
6868
// by the users unless current threads cancelability is set to PTHREAD_CANCEL_DISABLE
6969
// which may be either done by the user of __timedwait() function.
70+
pthread_t self = pthread_self();
7071
if (is_runtime_thread ||
71-
pthread_self()->canceldisable != PTHREAD_CANCEL_DISABLE ||
72-
pthread_self()->cancelasync) {
72+
self->canceldisable != PTHREAD_CANCEL_DISABLE ||
73+
self->cancelasync) {
7374
double sleepUntilTime = emscripten_get_now() + msecsToSleep;
7475
do {
75-
if (pthread_self()->cancel) {
76+
if (self->cancel) {
7677
// The thread was canceled by pthread_cancel().
7778
// In the case of cancelasync or PTHREAD_CANCEL_ENABLE we can just call
7879
// __pthread_testcancel(), which won't return at all.

system/lib/pthread/emscripten_futex_wait.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ int emscripten_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms)
130130
emscripten_conditional_set_current_thread_status(EM_THREAD_STATUS_RUNNING, EM_THREAD_STATUS_WAITFUTEX);
131131

132132
#ifdef __EMSCRIPTEN_PTHREADS__
133-
bool cancelable = pthread_self()->cancelasync == PTHREAD_CANCEL_ASYNCHRONOUS;
133+
pthread_t self = pthread_self();
134+
bool cancelable = self->cancelasync == PTHREAD_CANCEL_ASYNCHRONOUS;
134135
#else
135136
bool cancelable = false;
136137
#endif
@@ -199,19 +200,19 @@ int emscripten_futex_wait(volatile void *addr, uint32_t val, double max_wait_ms)
199200
// the queue, either from the call here directly after setting `sleeping` to
200201
// 1, or from another callsite (e.g. the one in `emscripten_yield`).
201202
if (!is_runtime_thread) {
202-
__pthread_self()->sleeping = 1;
203+
self->sleeping = 1;
203204
_emscripten_process_dlopen_queue();
204205
}
205206
#endif
206207
ret = __builtin_wasm_memory_atomic_wait32((int*)addr, val, max_wait_ns);
207208
#ifdef EMSCRIPTEN_DYNAMIC_LINKING
208209
if (!is_runtime_thread) {
209-
__pthread_self()->sleeping = 0;
210+
self->sleeping = 0;
210211
_emscripten_process_dlopen_queue();
211212
}
212213
#endif
213214
#ifdef __EMSCRIPTEN_PTHREADS__
214-
if (cancelable && ret == ATOMICS_WAIT_TIMED_OUT && pthread_self()->cancel) {
215+
if (cancelable && ret == ATOMICS_WAIT_TIMED_OUT && self->cancel) {
215216
// Break out of the loop early if we were cancelled
216217
break;
217218
}

test/codesize/test_codesize_minimal_pthreads.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"a.out.js": 7364,
33
"a.out.js.gz": 3607,
4-
"a.out.nodebug.wasm": 19265,
4+
"a.out.nodebug.wasm": 19263,
55
"a.out.nodebug.wasm.gz": 8934,
6-
"total": 26629,
6+
"total": 26627,
77
"total_gz": 12541,
88
"sent": [
99
"a (memory)",

test/codesize/test_codesize_minimal_pthreads_memgrowth.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"a.out.js": 7766,
33
"a.out.js.gz": 3812,
4-
"a.out.nodebug.wasm": 19266,
4+
"a.out.nodebug.wasm": 19264,
55
"a.out.nodebug.wasm.gz": 8935,
6-
"total": 27032,
6+
"total": 27030,
77
"total_gz": 12747,
88
"sent": [
99
"a (memory)",

0 commit comments

Comments
 (0)