Cleanup tests/core/pthread/create. NFC. Mostly convert to C to match the other tests here. See #3494
diff --git a/tests/core/pthread/create.cpp b/tests/core/pthread/create.c similarity index 72% rename from tests/core/pthread/create.cpp rename to tests/core/pthread/create.c index 5608dc8..7b35eca 100644 --- a/tests/core/pthread/create.cpp +++ b/tests/core/pthread/create.c
@@ -3,25 +3,25 @@ // University of Illinois/NCSA Open Source License. Both these licenses can be // found in the LICENSE file. +#include <stdatomic.h> #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <assert.h> #include <emscripten.h> -#include <atomic> #define NUM_THREADS 2 #define TOTAL 100 -static std::atomic<int> sum; +static _Atomic int sum; void *ThreadMain(void *arg) { for (int i = 0; i < TOTAL; i++) { sum++; // wait for a change, so we see interleaved processing. - int last = sum.load(); - while (sum.load() == last) {} + int last = sum; + while (sum == last) {} } pthread_exit((void*)TOTAL); } @@ -30,38 +30,39 @@ void CreateThread(int i) { - static int counter = 1; - int rc = pthread_create(&thread[i], nullptr, ThreadMain, (void*)i); + int rc = pthread_create(&thread[i], NULL, ThreadMain, (void*)i); assert(rc == 0); } -void mainn() { +void main_iter() { static int main_adds = 0; - int worker_adds = sum.load() - main_adds; + int worker_adds = sum - main_adds; sum++; main_adds++; printf("main iter %d : %d\n", main_adds, worker_adds); if (worker_adds == NUM_THREADS * TOTAL) { printf("done!\n"); #ifndef ALLOW_SYNC - emscripten_cancel_main_loop(); + emscripten_cancel_main_loop(); #else - exit(0); + exit(0); #endif } } int main() { // Create initial threads. - for(int i = 0; i < NUM_THREADS; ++i) { + for (int i = 0; i < NUM_THREADS; ++i) { CreateThread(i); } // if we don't allow sync pthread creation, the event loop must be reached for // the worker to start up. #ifndef ALLOW_SYNC - emscripten_set_main_loop(mainn, 0, 0); + emscripten_set_main_loop(main_iter, 0, 0); #else - while (1) mainn(); + while (1) { + main_iter(); + } #endif }
diff --git a/tests/test_core.py b/tests/test_core.py index af177d2..633c377 100644 --- a/tests/test_core.py +++ b/tests/test_core.py
@@ -8103,28 +8103,30 @@ @node_pthreads def test_pthread_create(self): - self.do_run_in_out_file_test('tests', 'core', 'pthread', 'create.cpp') + self.do_run_in_out_file_test('tests', 'core', 'pthread', 'create.c') @node_pthreads def test_pthread_create_pool(self): # with a pool, we can synchronously depend on workers being available self.set_setting('PTHREAD_POOL_SIZE', '2') self.emcc_args += ['-DALLOW_SYNC'] - self.do_run_in_out_file_test('tests', 'core', 'pthread', 'create.cpp') + self.do_run_in_out_file_test('tests', 'core', 'pthread', 'create.c') @node_pthreads def test_pthread_create_proxy(self): # with PROXY_TO_PTHREAD, we can synchronously depend on workers being available self.set_setting('PROXY_TO_PTHREAD', '1') self.emcc_args += ['-DALLOW_SYNC'] - self.do_run_in_out_file_test('tests', 'core', 'pthread', 'create.cpp') + self.do_run_in_out_file_test('tests', 'core', 'pthread', 'create.c') @node_pthreads - def test_pthread_create_embind_stack_check(self): + def test_pthread_embind_stack_check(self): # embind should work with stack overflow checks (see #12356) self.set_setting('STACK_OVERFLOW_CHECK', 2) self.emcc_args += ['--bind'] - self.do_run_in_out_file_test('tests', 'core', 'pthread', 'create.cpp') + # compile with C++ compiler since bind needs C++ stdlibs. + shutil.copyfile(path_from_root('tests', 'core', 'pthread', 'create.c'), 'create.cpp') + self.do_runf('create.cpp', 'done!') @node_pthreads def test_pthread_exceptions(self): @@ -8135,13 +8137,11 @@ def test_emscripten_atomics_stub(self): self.do_run_in_out_file_test('tests', 'core', 'pthread', 'emscripten_atomics.c') - @no_asan('incompatibility with atomics') @node_pthreads def test_emscripten_atomics(self): self.set_setting('USE_PTHREADS', '1') self.do_run_in_out_file_test('tests', 'core', 'pthread', 'emscripten_atomics.c') - @no_asan('incompatibility with atomics') @node_pthreads def test_emscripten_futexes(self): self.set_setting('USE_PTHREADS', '1')