Skip to content
Open
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
82 changes: 82 additions & 0 deletions test/pthread/test_pthread_barrier.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2015 The Emscripten Authors. All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License. Both these licenses can be
// found in the LICENSE file.

// This file tests pthread barrier usage.

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#define N 100
#define THREADS 8

int matrix[N][N] = {};
int intermediate[N] = {};

// Barrier variable
pthread_barrier_t barr;

// Sums a single row of a matrix.
int sum_row(long r) {
int sum = 0;
for (int i = 0; i < N; ++i) {
sum += matrix[r][i];
}
return sum;
}

void* thread_main(void* arg) {
// Each thread sums individual rows.
long id = (long)arg;
for (long i = id; i < N; i += THREADS) {
intermediate[i] = sum_row(i);
}

// Synchronization point
int rc = pthread_barrier_wait(&barr);
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) {
printf("Could not wait on barrier\n");
exit(-1);
}

// Then each thread sums the one intermediate vector.
intptr_t totalSum = 0;
for (int i = 0; i < N; ++i) {
totalSum += intermediate[i];
}

pthread_exit((void*)totalSum);
}

int main(int argc, char** argv) {
pthread_t thr[THREADS];

// Create the matrix and compute the expected result.
int expectedTotalSum = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
matrix[i][j] = rand();
expectedTotalSum += matrix[i][j];
}
}
printf("The sum of the matrix is %d.\n", expectedTotalSum);

// Barrier initialization
int ret = pthread_barrier_init(&barr, NULL, THREADS);
assert(ret == 0);

for (intptr_t i = 0; i < THREADS; ++i) {
pthread_create(&thr[i], NULL, &thread_main, (void*)i);
}

for (int i = 0; i < THREADS; ++i) {
int totalSum = 0;
pthread_join(thr[i], (void**)&totalSum);
assert(totalSum == expectedTotalSum);
}

return 0;
}
82 changes: 0 additions & 82 deletions test/pthread/test_pthread_barrier.cpp

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ int thread_ids[3] = {0,1,2};
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;

void *inc_count(void *t)
{
void* inc_count(void* t) {
int i;
long my_id = (long)t;

Expand All @@ -44,8 +43,7 @@ void *inc_count(void *t)
pthread_exit(NULL);
}

void *watch_count(void *t)
{
void *watch_count(void *t) {
long my_id = (long)t;

emscripten_outf("Starting watch_count(): thread %ld\n", my_id);
Expand All @@ -68,8 +66,7 @@ void *watch_count(void *t)
pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
int main (int argc, char *argv[]) {
int i, rc;
long t1=1, t2=2, t3=3;
pthread_t threads[3];
Expand Down
11 changes: 5 additions & 6 deletions test/pthread/test_pthread_gcc_atomics.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Atomic-Builtins.html

#define NUM_THREADS 8
#define NUM_THREADS_ODD (NUM_THREADS-1)

#define T int

Expand Down Expand Up @@ -71,13 +72,12 @@ int main() {
T y = nand_and_fetch(&x, 9);
assert(y == -2);
assert(x == -2);
const int oddNThreads = NUM_THREADS-1;
for (int x = 0; x < 100; ++x) { // Test a few times for robustness, since this test is so short-lived.
nand_and_fetch_data = 0;
__sync_synchronize(); // This has no effect in this code, but called in here just to test that the compiler generates a valid expression for this.
if (emscripten_has_threading_support()) {
for (int i = 0; i < oddNThreads; ++i) pthread_create(&thread[i], NULL, thread_nand_and_fetch, (void*)-1);
for (int i = 0; i < oddNThreads; ++i) pthread_join(thread[i], NULL);
for (int i = 0; i < NUM_THREADS_ODD; ++i) pthread_create(&thread[i], NULL, thread_nand_and_fetch, (void*)-1);
for (int i = 0; i < NUM_THREADS_ODD; ++i) pthread_join(thread[i], NULL);
assert(nand_and_fetch_data == -1);
}
}
Expand All @@ -87,12 +87,11 @@ int main() {
T y = nand_and_fetch_bool(&x, 9);
assert(y == -2);
assert(x == -2);
const int oddNThreads = NUM_THREADS-1;
for (int x = 0; x < 100; ++x) { // Test a few times for robustness, since this test is so short-lived.
nand_and_fetch_data = 0;
if (emscripten_has_threading_support()) {
for (int i = 0; i < oddNThreads; ++i) pthread_create(&thread[i], NULL, thread_nand_and_fetch_bool, (void*)-1);
for (int i = 0; i < oddNThreads; ++i) pthread_join(thread[i], NULL);
for (int i = 0; i < NUM_THREADS_ODD; ++i) pthread_create(&thread[i], NULL, thread_nand_and_fetch_bool, (void*)-1);
for (int i = 0; i < NUM_THREADS_ODD; ++i) pthread_join(thread[i], NULL);
assert(nand_and_fetch_data == -1);
}
}
Expand Down
15 changes: 8 additions & 7 deletions test/pthread/test_pthread_locale.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ int main (int argc, char *argv[]) {
locale_t main_loc = do_test();
locale_t child_loc;

if (emscripten_has_threading_support()) {
long id = 1;
pthread_t thread;
pthread_t thread;
int rtn;

pthread_create(&thread, NULL, thread_test, (void *)id);
rtn = pthread_create(&thread, NULL, thread_test, (void *)NULL);
printf("create: %d\n", rtn);
assert(!rtn);
rtn = pthread_join(thread, (void**)&child_loc);
assert(!rtn);

pthread_join(thread, (void**)&child_loc);
assert(main_loc == child_loc);
}
assert(main_loc == child_loc);

return 0;
}
11 changes: 5 additions & 6 deletions test/pthread/test_pthread_once.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
#include <emscripten.h>
#include <emscripten/threading.h>

_Atomic int numInitialized = 0;
int numInitialized = 0;

void once_init() {
numInitialized++;
}

#define NUM_THREADS 8

void *thread_main(void *arg)
{
void *thread_main(void *arg) {
static pthread_once_t control = PTHREAD_ONCE_INIT;
pthread_once(&control, &once_init);
assert(numInitialized == 1);
Expand All @@ -33,10 +32,10 @@ int main() {
pthread_create(&thread[i], NULL, thread_main, 0);
}

if (emscripten_has_threading_support()) {
for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
assert(numInitialized == 1);
for (int i = 0; i < NUM_THREADS; ++i) {
pthread_join(thread[i], NULL);
}
assert(numInitialized == 1);

return 0;
}
Loading
Loading