Skip to content

Commit

Permalink
Cleanup tests/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 #3494
  • Loading branch information
sbc100 committed Nov 10, 2020
1 parent 8091b98 commit d33e03d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2117,7 +2117,7 @@ def compile_source_file(i, input_file):
if not shared.Settings.SIDE_MODULE: # shared libraries/side modules link no C libraries, need them in parent
extra_files_to_link = system_libs.get_ports(shared.Settings)
if '-nostdlib' not in newargs and '-nodefaultlibs' not in newargs:
link_as_cxx = run_via_emxx
link_as_cxx = run_via_emxx or shared.Settings.EMBIND
# Traditionally we always link as C++. For compatibility we continue to do that,
# unless running in strict mode.
if not shared.Settings.STRICT and '-nostdlib++' not in newargs:
Expand Down
27 changes: 14 additions & 13 deletions tests/core/pthread/create.cpp → tests/core/pthread/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -30,38 +30,39 @@ pthread_t thread[NUM_THREADS];

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
}
10 changes: 4 additions & 6 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8097,28 +8097,28 @@ def test_fpic_static(self):

@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):
# 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')
self.do_run_in_out_file_test('tests', 'core', 'pthread', 'create.c')

@node_pthreads
def test_pthread_exceptions(self):
Expand All @@ -8129,13 +8129,11 @@ def test_pthread_exceptions(self):
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')
Expand Down

0 comments on commit d33e03d

Please sign in to comment.