diff --git a/src/postamble_minimal.js b/src/postamble_minimal.js index 687059c472c5f..247d1a4c765e3 100644 --- a/src/postamble_minimal.js +++ b/src/postamble_minimal.js @@ -51,7 +51,8 @@ function initRuntime(wasmExports) { #endif #if PTHREADS - if (ENVIRONMENT_IS_PTHREAD) return + PThread.tlsInitFunctions.push(wasmExports['_emscripten_tls_init']); + if (ENVIRONMENT_IS_PTHREAD) return; #endif #if WASM_WORKERS @@ -66,10 +67,6 @@ function initRuntime(wasmExports) { writeStackCookie(); #endif -#if PTHREADS - PThread.tlsInitFunctions.push(wasmExports['_emscripten_tls_init']); -#endif - <<< ATINITS >>> #if hasExportedSymbol('__wasm_call_ctors') diff --git a/test/pthread/test_pthread_c_thread_local.c b/test/pthread/test_pthread_c_thread_local.c new file mode 100644 index 0000000000000..2212d36304796 --- /dev/null +++ b/test/pthread/test_pthread_c_thread_local.c @@ -0,0 +1,35 @@ +// Copyright 2025 The Emscripten Authors. All rights reserved. +// Emscripten is available under two separate licenses, the MIT license and the +// University of Illinois/NCSA Open Source License. Both these licenses can be +// found in the LICENSE file. + +#include +#include +#include +#include + +static _Thread_local int thread_local = 0; + +void *thread_start(void *arg) { + thread_local = 1; + assert(thread_local == 1); + printf("thread_local=%d\n", thread_local); + return NULL; +} + +int main() { + thread_local = 2; + pthread_t thread; + int rc; + + rc = pthread_create(&thread, NULL, thread_start, NULL); + assert(rc == 0); + + rc = pthread_join(thread, NULL); + assert(rc == 0); + + printf("thread_local=%d\n", thread_local); + assert(thread_local == 2); + printf("done\n"); + return 0; +} diff --git a/test/pthread/test_pthread_c_thread_local.out b/test/pthread/test_pthread_c_thread_local.out new file mode 100644 index 0000000000000..50443a5530ef4 --- /dev/null +++ b/test/pthread/test_pthread_c_thread_local.out @@ -0,0 +1,3 @@ +thread_local=1 +thread_local=2 +done diff --git a/test/test_core.py b/test/test_core.py index c28ce4cbf3c68..d4374ed35dc07 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -2636,6 +2636,13 @@ def test_pthread_thread_local_storage(self): self.set_setting('INITIAL_MEMORY', '300mb') self.do_run_in_out_file_test('pthread/test_pthread_thread_local_storage.cpp') + @node_pthreads + @also_with_minimal_runtime + def test_pthread_c_thread_local(self): + if self.get_setting('MINIMAL_RUNTIME') and is_sanitizing(self.cflags): + self.skipTest('MINIMAL_RUNTIME + threads + asan does not work') + self.do_run_in_out_file_test('pthread/test_pthread_c_thread_local.c') + @node_pthreads def test_pthread_cleanup(self): self.set_setting('PTHREAD_POOL_SIZE', 4)