diff --git a/tests/pthread/test_pthread_gcc_spinlock.cpp b/tests/pthread/test_pthread_gcc_spinlock.cpp index ec9c8a2e6..5b23a5ed9 100644 --- a/tests/pthread/test_pthread_gcc_spinlock.cpp +++ b/tests/pthread/test_pthread_gcc_spinlock.cpp @@ -1,19 +1,25 @@ #include #include #include -#include +#include +#include #include // usleep #include volatile int counter = 0; // Shared data volatile int lock = 0; // spinlock "mutex" variable + void *ThreadMain(void *arg) { printf("Thread started.\n"); for(int i = 0; i < 100; ++i) { +#ifdef USE_EMSCRIPTEN_INTRINSICS + while(emscripten_atomic_exchange_u32((void*)&lock, 1)) +#else while(__sync_lock_test_and_set(&lock, 1)) +#endif { /*nop*/; } @@ -21,7 +27,11 @@ void *ThreadMain(void *arg) usleep(5 * 1000); // Create contention on the lock. ++c; counter = c; +#ifdef USE_EMSCRIPTEN_INTRINSICS + emscripten_atomic_store_u32((void*)&lock, 0); +#else __sync_lock_release(&lock); +#endif } printf("Thread done.\n"); pthread_exit(0); diff --git a/tests/test_browser.py b/tests/test_browser.py index 00c537944..61b31a185 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -2542,7 +2542,8 @@ window.close = function() { # Test the __sync_lock_test_and_set and __sync_lock_release primitives. def test_pthread_gcc_spinlock(self): - self.btest(path_from_root('tests', 'pthread', 'test_pthread_gcc_spinlock.cpp'), expected='800', args=['-lpthread', '-s', 'PTHREAD_POOL_SIZE=8']) + for arg in [[], ['-DUSE_EMSCRIPTEN_INTRINSICS']]: + self.btest(path_from_root('tests', 'pthread', 'test_pthread_gcc_spinlock.cpp'), expected='800', args=['-lpthread', '-s', 'PTHREAD_POOL_SIZE=8'] + arg) # Test that basic thread creation works. def test_pthread_create(self): @@ -2575,7 +2576,7 @@ window.close = function() { # Tests the pthread mutex api. def test_pthread_mutex(self): for arg in [[], ['-DSPINLOCK_TEST']]: - self.btest(path_from_root('tests', 'pthread', 'test_pthread_mutex.cpp'), expected='50', args=['-lpthread', '-s', 'PTHREAD_POOL_SIZE=8']) + self.btest(path_from_root('tests', 'pthread', 'test_pthread_mutex.cpp'), expected='50', args=['-lpthread', '-s', 'PTHREAD_POOL_SIZE=8'] + arg) # Test that memory allocation is thread-safe. def test_pthread_malloc(self):