Skip to content

Commit d68d47f

Browse files
committed
Avoid use of __EMSCRIPTEN_PTHREADS__ in __errno_location.c
Instead we can use TLS which works for both single and multithreaded builds and also works with wasm workers. This is also needed for emscripten-core#22735
1 parent 1dfe990 commit d68d47f

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed
Lines changed: 9 additions & 12 deletions
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 get 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

Lines changed: 4 additions & 4 deletions
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/test_sockets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def test_nodejs_sockets_echo_subprotocol(self):
328328
# NOTE: Shared buffer is not allowed for websocket sending.
329329
@parameterized({
330330
'': [[]],
331-
'shared': [['-sSHARED_MEMORY']],
331+
'pthread': [['-pthread']],
332332
})
333333
def test_websocket_send(self, args):
334334
with NodeJsWebSocketEchoServerProcess():

0 commit comments

Comments
 (0)