Skip to content

Commit

Permalink
Cleanup test/core/pthread/create. NFC.
Browse files Browse the repository at this point in the history
Mostly convert to C to match the other tests here.

See emscripten-core#3494
  • Loading branch information
sbc100 committed Jan 18, 2024
1 parent fa47840 commit 3c4ceee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 10 additions & 9 deletions test/core/pthread/create.cpp → test/core/pthread/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,36 @@
// 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++) {
// wait for a change, so we see interleaved processing.
int last = ++sum;
while (sum.load() == last) {}
while (sum == last) {}
}
pthread_exit((void*)TOTAL);
}

pthread_t thread[NUM_THREADS];

void CreateThread(long i)
{
int rc = pthread_create(&thread[i], nullptr, ThreadMain, (void*)i);
void CreateThread(long 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++ - main_adds++;
printf("main iter %d : %d\n", main_adds, worker_adds);
Expand All @@ -55,9 +54,11 @@ int main() {
// 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
return 0;
}
8 changes: 4 additions & 4 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9102,7 +9102,7 @@ def test_pthread_create(self):
# works with pthreads (even though we did not specify 'node,worker')
self.set_setting('ENVIRONMENT', 'node')
self.set_setting('STRICT_JS')
self.do_run_in_out_file_test('core/pthread/create.cpp')
self.do_run_in_out_file_test('core/pthread/create.c')

@node_pthreads
@parameterized({
Expand Down Expand Up @@ -9149,23 +9149,23 @@ def test_pthread_create_pool(self):
self.set_setting('PTHREAD_POOL_SIZE', 2)
self.set_setting('EXIT_RUNTIME')
self.emcc_args += ['-DALLOW_SYNC']
self.do_run_in_out_file_test('core/pthread/create.cpp')
self.do_run_in_out_file_test('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')
self.set_setting('EXIT_RUNTIME')
self.emcc_args += ['-DALLOW_SYNC']
self.do_run_in_out_file_test('core/pthread/create.cpp')
self.do_run_in_out_file_test('core/pthread/create.c')

@node_pthreads
def test_pthread_create_embind_stack_check(self):
# embind should work with stack overflow checks (see #12356)
self.set_setting('STACK_OVERFLOW_CHECK', 2)
self.set_setting('EXIT_RUNTIME')
self.emcc_args += ['-lembind']
self.do_run_in_out_file_test('core/pthread/create.cpp')
self.do_run_in_out_file_test('core/pthread/create.c', emcc_args=['-sDEFAULT_TO_CXX'])

@node_pthreads
def test_pthread_exceptions(self):
Expand Down

0 comments on commit 3c4ceee

Please sign in to comment.