Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
#include <pthread.h>
#include <stdio.h>

#include <atomic>

pthread_t thread;

std::atomic<int> tries;
_Atomic int tries;

static const int EXPECTED_TRIES = 7;

Expand All @@ -21,7 +19,7 @@ void loop() {
printf("try...\n");
if (pthread_tryjoin_np(thread, &retval) == 0) {
emscripten_cancel_main_loop();
assert(tries.load() == EXPECTED_TRIES);
assert(tries == EXPECTED_TRIES);
emscripten_force_exit(2);
}
tries++;
Expand All @@ -31,7 +29,7 @@ void *ThreadMain(void *arg) {
#ifdef TRY_JOIN
// Delay to force the main thread to try and fail a few times before
// succeeding.
while (tries.load() < EXPECTED_TRIES) {}
while (tries < EXPECTED_TRIES) {}
#endif
pthread_exit((void*)0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#include <cassert>
#include <thread>
#include <assert.h>
#include <pthread.h>
#include <emscripten.h>
#include <emscripten/stack.h>

void thread(void) {
void* thread_main(void* arg) {
bool passed;
size_t stack_base = emscripten_stack_get_base();
size_t stack_max = emscripten_stack_get_end();
size_t current = (size_t) &passed;
assert(stack_base > current && current > stack_max);
emscripten_force_exit(0);
return 0;
}

pthread_t t;

int main(void) {
std::thread t(thread);
t.detach();
pthread_create(&t, NULL, thread_main, NULL);
emscripten_exit_with_live_runtime();
__builtin_trap();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <cassert>
#include <thread>
#include <cstdio>
#include <assert.h>
#include <threads.h>

thread_local int tls = 1330;
thread_local int tls2;
Expand Down
27 changes: 14 additions & 13 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3708,15 +3708,16 @@ def test_pthread_main_thread_blocking_join(self):
};
''')
# Test that we warn about blocking on the main thread in debug builds
self.btest('pthread/main_thread_join.cpp', expected='got_warn', cflags=['-sEXIT_RUNTIME', '-sASSERTIONS', '--pre-js', 'pre.js', '-pthread', '-sPTHREAD_POOL_SIZE'])
self.cflags.append('-D_GNU_SOURCE') # For pthread_tryjoin_np
self.btest('pthread/main_thread_join.c', expected='got_warn', cflags=['-sEXIT_RUNTIME', '-sASSERTIONS', '--pre-js', 'pre.js', '-pthread', '-sPTHREAD_POOL_SIZE'])
# Test that we do not warn about blocking on the main thread in release builds
self.btest_exit('pthread/main_thread_join.cpp', cflags=['-O3', '--pre-js', 'pre.js', '-pthread', '-sPTHREAD_POOL_SIZE'])
self.btest_exit('pthread/main_thread_join.c', cflags=['-O3', '--pre-js', 'pre.js', '-pthread', '-sPTHREAD_POOL_SIZE'])
# Test that tryjoin is fine, even if not ALLOW_BLOCKING_ON_MAIN_THREAD
self.btest_exit('pthread/main_thread_join.cpp', assert_returncode=2, cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE', '-g', '-DTRY_JOIN', '-sALLOW_BLOCKING_ON_MAIN_THREAD=0'])
self.btest_exit('pthread/main_thread_join.c', assert_returncode=2, cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE', '-g', '-DTRY_JOIN', '-sALLOW_BLOCKING_ON_MAIN_THREAD=0'])
# Test that tryjoin is fine, even if not ALLOW_BLOCKING_ON_MAIN_THREAD, and even without a pool
self.btest_exit('pthread/main_thread_join.cpp', assert_returncode=2, cflags=['-O3', '-pthread', '-g', '-DTRY_JOIN', '-sALLOW_BLOCKING_ON_MAIN_THREAD=0'])
self.btest_exit('pthread/main_thread_join.c', assert_returncode=2, cflags=['-O3', '-pthread', '-g', '-DTRY_JOIN', '-sALLOW_BLOCKING_ON_MAIN_THREAD=0'])
# Test that everything works ok when we are on a pthread
self.btest_exit('pthread/main_thread_join.cpp', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE', '-sPROXY_TO_PTHREAD', '-sALLOW_BLOCKING_ON_MAIN_THREAD=0'])
self.btest_exit('pthread/main_thread_join.c', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE', '-sPROXY_TO_PTHREAD', '-sALLOW_BLOCKING_ON_MAIN_THREAD=0'])

# Test the old GCC atomic __sync_fetch_and_op builtin operations.
@parameterized({
Expand Down Expand Up @@ -3855,15 +3856,15 @@ def test_pthread_malloc_free(self):

# Test that the pthread_barrier API works ok.
def test_pthread_barrier(self):
self.btest_exit('pthread/test_pthread_barrier.cpp', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8'])
self.btest_exit('pthread/test_pthread_barrier.c', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8'])

# Test the pthread_once() function.
def test_pthread_once(self):
self.btest_exit('pthread/test_pthread_once.c', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8'])

# Test against a certain thread exit time handling bug by spawning tons of threads.
def test_pthread_spawns(self):
self.btest_exit('pthread/test_pthread_spawns.cpp', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8', '--closure=1', '-sENVIRONMENT=web'])
self.btest_exit('pthread/test_pthread_spawns.c', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8', '--closure=1', '-sENVIRONMENT=web'])

# It is common for code to flip volatile global vars for thread control. This is a bit lax, but nevertheless, test whether that
# kind of scheme will work with Emscripten as well.
Expand All @@ -3876,11 +3877,11 @@ def test_pthread_volatile(self, args):

# Test thread-specific data (TLS).
def test_pthread_thread_local_storage(self):
self.btest_exit('pthread/test_pthread_thread_local_storage.cpp', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8', '-sASSERTIONS'])
self.btest_exit('pthread/test_pthread_thread_local_storage.c', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8', '-sASSERTIONS'])

# Test the pthread condition variable creation and waiting.
def test_pthread_condition_variable(self):
self.btest_exit('pthread/test_pthread_condition_variable.cpp', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8'])
self.btest_exit('pthread/test_pthread_condition_variable.c', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8'])

# Test that pthreads are able to do printf.
@parameterized({
Expand Down Expand Up @@ -3910,7 +3911,7 @@ def test_pthread_file_io(self):
# Test that the pthread_create() function operates benignly in the case that threading is not supported.
@parameterized({
'': ([],),
'mt': (['-pthread', '-sPTHREAD_POOL_SIZE=8'],),
'mt': (['-pthread', '-sPTHREAD_POOL_SIZE=1'],),
})
def test_pthread_supported(self, args):
self.btest_exit('pthread/test_pthread_supported.c', cflags=['-O3'] + args)
Expand All @@ -3922,7 +3923,7 @@ def test_pthread_dispatch_after_exit(self):
# needs it to do a proxied operation (before that pthread would wake up the
# main thread), that it's not a deadlock.
def test_pthread_proxying_in_futex_wait(self):
self.btest_exit('pthread/test_pthread_proxying_in_futex_wait.cpp', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE'])
self.btest_exit('pthread/test_pthread_proxying_in_futex_wait.c', cflags=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE'])

# Test that sbrk() operates properly in multithreaded conditions
@no_2gb('uses INITIAL_MEMORY')
Expand Down Expand Up @@ -4004,15 +4005,15 @@ def test_pthread_wake_all(self):

# Test that stack base and max correctly bound the stack on pthreads.
def test_pthread_stack_bounds(self):
self.btest_exit('pthread/test_pthread_stack_bounds.cpp', cflags=['-pthread'])
self.btest_exit('pthread/test_pthread_stack_bounds.c', cflags=['-pthread'])

# Test that real `thread_local` works.
def test_pthread_tls(self):
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):
self.btest_exit('pthread/test_pthread_tls_main.cpp', cflags=['-pthread'])
self.btest_exit('pthread/test_pthread_tls_main.c', cflags=['-pthread'])

def test_pthread_safe_stack(self):
# Note that as the test runs with PROXY_TO_PTHREAD, we set STACK_SIZE,
Expand Down
2 changes: 1 addition & 1 deletion test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2651,7 +2651,7 @@ def test_pthread_thread_local_storage(self):
self.set_setting('EXIT_RUNTIME')
if not self.has_changed_setting('INITIAL_MEMORY'):
self.set_setting('INITIAL_MEMORY', '300mb')
self.do_run_in_out_file_test('pthread/test_pthread_thread_local_storage.cpp')
self.do_run_in_out_file_test('pthread/test_pthread_thread_local_storage.c')

@requires_pthreads
def test_pthread_cleanup(self):
Expand Down
Loading