Skip to content

Commit

Permalink
Avoid use of __EMSCRIPTEN_PTHREADS__ in __errno_location.c
Browse files Browse the repository at this point in the history
Instead we can use TLS which works for both single and multithreaded
builds and also works with wasm workers.

This is also needed for #22735
  • Loading branch information
sbc100 committed Oct 15, 2024
1 parent 1dfe990 commit d68d47f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
21 changes: 9 additions & 12 deletions system/lib/libc/musl/src/errno/__errno_location.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#include <errno.h>
#include "pthread_impl.h"

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

int *__errno_location(void)
{
#if __EMSCRIPTEN__
return &__errno_storage;
}
#else
return &__pthread_self()->errno_val;
#endif
}

weak_alias(__errno_location, ___errno_location);
8 changes: 4 additions & 4 deletions test/code_size/hello_wasm_worker_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"a.js.gz": 455,
"a.ww.js": 115,
"a.ww.js.gz": 127,
"a.wasm": 1850,
"a.wasm.gz": 1050,
"total": 3248,
"total_gz": 2016
"a.wasm": 1894,
"a.wasm.gz": 1077,
"total": 3292,
"total_gz": 2043
}
2 changes: 1 addition & 1 deletion test/test_sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def test_nodejs_sockets_echo_subprotocol(self):
# NOTE: Shared buffer is not allowed for websocket sending.
@parameterized({
'': [[]],
'shared': [['-sSHARED_MEMORY']],
'pthread': [['-pthread']],
})
def test_websocket_send(self, args):
with NodeJsWebSocketEchoServerProcess():
Expand Down

0 comments on commit d68d47f

Please sign in to comment.