Skip to content

Commit 814ec05

Browse files
authored
Fix __errno_location under wasm workers. NFC (#22744)
Previously the wasm workers build of `__errno_location` was using the single threaded code path which doesn't use a thead local location. In addition this change avoids the use of `__EMSCRIPTEN_PTHREADS__` in `__errno_location.c` which is helpful for unifying the shared memory libc build. See #22735.
1 parent a98af2b commit 814ec05

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
#include <errno.h>
22
#include "pthread_impl.h"
33

4-
#if __EMSCRIPTEN_PTHREADS__
5-
// for pthreads, use the proper location on the thread info, so each
6-
// thread has its own errno
7-
int *__errno_location(void)
8-
{
9-
return &__pthread_self()->errno_val;
10-
}
11-
#else
12-
// for single-threaded mode, avoid linking in pthreads support code
13-
// just for this
14-
static int __errno_storage = 0;
4+
#if __EMSCRIPTEN__
5+
// For emscripten we use TLS here instead of `__pthread_self`, so that in single
6+
// threaded builds this gets lowered away to normal global variable.
7+
static _Thread_local int __errno_storage = 0;
8+
#endif
159

1610
int *__errno_location(void)
1711
{
12+
#if __EMSCRIPTEN__
1813
return &__errno_storage;
19-
}
14+
#else
15+
return &__pthread_self()->errno_val;
2016
#endif
17+
}
2118

2219
weak_alias(__errno_location, ___errno_location);

test/code_size/hello_wasm_worker_wasm.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"a.js.gz": 455,
66
"a.ww.js": 115,
77
"a.ww.js.gz": 127,
8-
"a.wasm": 1850,
9-
"a.wasm.gz": 1050,
10-
"total": 3248,
11-
"total_gz": 2016
8+
"a.wasm": 1894,
9+
"a.wasm.gz": 1077,
10+
"total": 3292,
11+
"total_gz": 2043
1212
}

test/wasm_worker/thread_stack.c

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <emscripten/console.h>
22
#include <emscripten/wasm_worker.h>
33
#include <emscripten/stack.h>
4+
#include <emscripten/console.h>
45
#include <stdio.h>
56
#include <assert.h>
67
#include <stdlib.h>
78

9+
#define ALIGN(x,y) ((x)+(y)-1 & -(y))
810
#define THREAD_STACK_SIZE 2048
911
#define NUM_THREADS 2
1012
void *thread_stack[NUM_THREADS];
@@ -18,7 +20,11 @@ void test_stack(int i) {
1820
(void*)emscripten_stack_get_end(),
1921
THREAD_STACK_SIZE);
2022
assert(emscripten_stack_get_base() == (uintptr_t)thread_stack[i] + THREAD_STACK_SIZE);
21-
assert(emscripten_stack_get_end() == (uintptr_t)thread_stack[i]);
23+
emscripten_outf("__builtin_wasm_tls_size: %lu", __builtin_wasm_tls_size());
24+
// The stack region in wasm workers also incldues TLS, so we need to take
25+
// this into account when calulating our expected stack end.
26+
size_t expected_stack_end = (uintptr_t)thread_stack[i] + ALIGN(__builtin_wasm_tls_size(), 16);
27+
assert(emscripten_stack_get_end() == expected_stack_end);
2228

2329
int ok = __sync_fetch_and_add(&threadsOk, 1);
2430
emscripten_outf("%d", ok);
@@ -36,7 +42,7 @@ int main() {
3642
for (int i = 0; i < NUM_THREADS; ++i) {
3743
thread_stack[i] = memalign(16, THREAD_STACK_SIZE);
3844
emscripten_wasm_worker_t worker = emscripten_create_wasm_worker(thread_stack[i], THREAD_STACK_SIZE);
39-
emscripten_outf("Created thread %d with stack ptr=%p, size=%x", i, thread_stack[i], THREAD_STACK_SIZE);
45+
emscripten_outf("Created thread %d with stack ptr=%p, end=%p, size=%x", i, thread_stack[i], thread_stack[i] + THREAD_STACK_SIZE, THREAD_STACK_SIZE);
4046
emscripten_wasm_worker_post_function_vi(worker, test_stack, i);
4147
}
4248
}

0 commit comments

Comments
 (0)