From 25da8821d275a8e8132dac71b8beaf99607f79fe Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 7 Jul 2025 10:18:56 -0700 Subject: [PATCH] [test] Simplify test_pthread_tls. NFC - Convert to C - Run in core tests under node. - Remove redundant test added in #24646 --- test/pthread/test_pthread_c_thread_local.c | 35 ------------------- ...est_pthread_tls.cpp => test_pthread_tls.c} | 20 ++++++----- test/test_browser.py | 2 +- test/test_core.py | 14 ++++---- 4 files changed, 19 insertions(+), 52 deletions(-) delete mode 100644 test/pthread/test_pthread_c_thread_local.c rename test/pthread/{test_pthread_tls.cpp => test_pthread_tls.c} (54%) diff --git a/test/pthread/test_pthread_c_thread_local.c b/test/pthread/test_pthread_c_thread_local.c deleted file mode 100644 index 2212d36304796..0000000000000 --- a/test/pthread/test_pthread_c_thread_local.c +++ /dev/null @@ -1,35 +0,0 @@ -// 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_tls.cpp b/test/pthread/test_pthread_tls.c similarity index 54% rename from test/pthread/test_pthread_tls.cpp rename to test/pthread/test_pthread_tls.c index 8a06f4bb25d68..a43c43ff74075 100644 --- a/test/pthread/test_pthread_tls.cpp +++ b/test/pthread/test_pthread_tls.c @@ -1,15 +1,15 @@ -#include -#include -#include +#include +#include +#include -thread_local int tls; -thread_local struct { +_Thread_local int tls; +_Thread_local struct { int a; double b; } data = {1, 2}; -thread_local int array[10]; +_Thread_local int array[10]; -void thread(void) { +void* thread_main(void* arg) { ++tls; data.a = 3; data.b = 4; @@ -17,12 +17,14 @@ void thread(void) { assert(data.a == 3); assert(data.b == 4); assert(array[9] == 0); + return NULL; } int main(void) { array[9] = 1337; - std::thread t(thread); - t.join(); + pthread_t t; + pthread_create(&t, NULL, thread_main, NULL); + pthread_join(t, NULL); assert(tls == 0); assert(data.a == 1); assert(data.b == 2); diff --git a/test/test_browser.py b/test/test_browser.py index a934191bc7ddb..3da5983a2d2a9 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -4068,7 +4068,7 @@ def test_pthread_stack_bounds(self): # Test that real `thread_local` works. def test_pthread_tls(self): - self.btest_exit('pthread/test_pthread_tls.cpp', cflags=['-sPROXY_TO_PTHREAD', '-pthread']) + self.btest_exit('pthread/test_pthread_tls.c', cflags=['-sPROXY_TO_PTHREAD', '-pthread']) # Test that real `thread_local` works in main thread without PROXY_TO_PTHREAD. def test_pthread_tls_main(self): diff --git a/test/test_core.py b/test/test_core.py index d4374ed35dc07..40f7a200901f8 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -2636,13 +2636,6 @@ 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) @@ -2714,6 +2707,13 @@ def test_pthread_tls_dylink(self): self.cflags.append('-Wno-experimental') self.do_run_in_out_file_test('pthread/test_pthread_tls_dylink.c') + @node_pthreads + @also_with_minimal_runtime + def test_pthread_tls(self): + if self.get_setting('MINIMAL_RUNTIME') and is_sanitizing(self.cflags): + self.skipTest('MINIMAL_RUNTIME + threads + asan does not work') + self.do_runf('pthread/test_pthread_tls.c') + @no_modularize_instance('uses global Module objecgt') def test_pthread_run_script(self): shutil.copy(test_file('pthread/foo.js'), '.')