Skip to content

Commit 0400ca2

Browse files
committed
Simplify some pthreads tests. NFC
- Convert tests from C++ to C where possible - Remove the non-threaded code paths in tests that we always run threaded
1 parent 71d627a commit 0400ca2

20 files changed

Lines changed: 357 additions & 385 deletions
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
#include <pthread.h>
99
#include <stdio.h>
1010

11-
#include <atomic>
12-
1311
pthread_t thread;
1412

15-
std::atomic<int> tries;
13+
_Atomic int tries;
1614

1715
static const int EXPECTED_TRIES = 7;
1816

@@ -21,7 +19,7 @@ void loop() {
2119
printf("try...\n");
2220
if (pthread_tryjoin_np(thread, &retval) == 0) {
2321
emscripten_cancel_main_loop();
24-
assert(tries.load() == EXPECTED_TRIES);
22+
assert(tries == EXPECTED_TRIES);
2523
emscripten_force_exit(2);
2624
}
2725
tries++;
@@ -31,7 +29,7 @@ void *ThreadMain(void *arg) {
3129
#ifdef TRY_JOIN
3230
// Delay to force the main thread to try and fail a few times before
3331
// succeeding.
34-
while (tries.load() < EXPECTED_TRIES) {}
32+
while (tries < EXPECTED_TRIES) {}
3533
#endif
3634
pthread_exit((void*)0);
3735
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2015 The Emscripten Authors. All rights reserved.
2+
// Emscripten is available under two separate licenses, the MIT license and the
3+
// University of Illinois/NCSA Open Source License. Both these licenses can be
4+
// found in the LICENSE file.
5+
6+
// This file tests pthread barrier usage.
7+
8+
#include <pthread.h>
9+
#include <stdlib.h>
10+
#include <stdio.h>
11+
#include <assert.h>
12+
13+
#define N 100
14+
#define THREADS 8
15+
16+
int matrix[N][N] = {};
17+
int intermediate[N] = {};
18+
19+
// Barrier variable
20+
pthread_barrier_t barr;
21+
22+
// Sums a single row of a matrix.
23+
int sum_row(long r) {
24+
int sum = 0;
25+
for (int i = 0; i < N; ++i) {
26+
sum += matrix[r][i];
27+
}
28+
return sum;
29+
}
30+
31+
void* thread_main(void* arg) {
32+
// Each thread sums individual rows.
33+
long id = (long)arg;
34+
for (long i = id; i < N; i += THREADS) {
35+
intermediate[i] = sum_row(i);
36+
}
37+
38+
// Synchronization point
39+
int rc = pthread_barrier_wait(&barr);
40+
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD) {
41+
printf("Could not wait on barrier\n");
42+
exit(-1);
43+
}
44+
45+
// Then each thread sums the one intermediate vector.
46+
intptr_t totalSum = 0;
47+
for (int i = 0; i < N; ++i) {
48+
totalSum += intermediate[i];
49+
}
50+
51+
pthread_exit((void*)totalSum);
52+
}
53+
54+
int main(int argc, char** argv) {
55+
pthread_t thr[THREADS];
56+
57+
// Create the matrix and compute the expected result.
58+
int expectedTotalSum = 0;
59+
for (int i = 0; i < N; ++i) {
60+
for (int j = 0; j < N; ++j) {
61+
matrix[i][j] = rand();
62+
expectedTotalSum += matrix[i][j];
63+
}
64+
}
65+
printf("The sum of the matrix is %d.\n", expectedTotalSum);
66+
67+
// Barrier initialization
68+
int ret = pthread_barrier_init(&barr, NULL, THREADS);
69+
assert(ret == 0);
70+
71+
for (intptr_t i = 0; i < THREADS; ++i) {
72+
pthread_create(&thr[i], NULL, &thread_main, (void*)i);
73+
}
74+
75+
for (int i = 0; i < THREADS; ++i) {
76+
int totalSum = 0;
77+
pthread_join(thr[i], (void**)&totalSum);
78+
assert(totalSum == expectedTotalSum);
79+
}
80+
81+
return 0;
82+
}

test/pthread/test_pthread_barrier.cpp

Lines changed: 0 additions & 82 deletions
This file was deleted.

test/pthread/test_pthread_condition_variable.cpp renamed to test/pthread/test_pthread_condition_variable.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ int thread_ids[3] = {0,1,2};
1818
pthread_mutex_t count_mutex;
1919
pthread_cond_t count_threshold_cv;
2020

21-
void *inc_count(void *t)
22-
{
21+
void* inc_count(void* t) {
2322
int i;
2423
long my_id = (long)t;
2524

@@ -44,8 +43,7 @@ void *inc_count(void *t)
4443
pthread_exit(NULL);
4544
}
4645

47-
void *watch_count(void *t)
48-
{
46+
void *watch_count(void *t) {
4947
long my_id = (long)t;
5048

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

71-
int main (int argc, char *argv[])
72-
{
69+
int main (int argc, char *argv[]) {
7370
int i, rc;
7471
long t1=1, t2=2, t3=3;
7572
pthread_t threads[3];

test/pthread/test_pthread_gcc_atomics.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// See https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Atomic-Builtins.html
1414

1515
#define NUM_THREADS 8
16+
#define NUM_THREADS_ODD (NUM_THREADS-1)
1617

1718
#define T int
1819

@@ -71,13 +72,12 @@ int main() {
7172
T y = nand_and_fetch(&x, 9);
7273
assert(y == -2);
7374
assert(x == -2);
74-
const int oddNThreads = NUM_THREADS-1;
7575
for (int x = 0; x < 100; ++x) { // Test a few times for robustness, since this test is so short-lived.
7676
nand_and_fetch_data = 0;
7777
__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.
7878
if (emscripten_has_threading_support()) {
79-
for (int i = 0; i < oddNThreads; ++i) pthread_create(&thread[i], NULL, thread_nand_and_fetch, (void*)-1);
80-
for (int i = 0; i < oddNThreads; ++i) pthread_join(thread[i], NULL);
79+
for (int i = 0; i < NUM_THREADS_ODD; ++i) pthread_create(&thread[i], NULL, thread_nand_and_fetch, (void*)-1);
80+
for (int i = 0; i < NUM_THREADS_ODD; ++i) pthread_join(thread[i], NULL);
8181
assert(nand_and_fetch_data == -1);
8282
}
8383
}
@@ -87,12 +87,11 @@ int main() {
8787
T y = nand_and_fetch_bool(&x, 9);
8888
assert(y == -2);
8989
assert(x == -2);
90-
const int oddNThreads = NUM_THREADS-1;
9190
for (int x = 0; x < 100; ++x) { // Test a few times for robustness, since this test is so short-lived.
9291
nand_and_fetch_data = 0;
9392
if (emscripten_has_threading_support()) {
94-
for (int i = 0; i < oddNThreads; ++i) pthread_create(&thread[i], NULL, thread_nand_and_fetch_bool, (void*)-1);
95-
for (int i = 0; i < oddNThreads; ++i) pthread_join(thread[i], NULL);
93+
for (int i = 0; i < NUM_THREADS_ODD; ++i) pthread_create(&thread[i], NULL, thread_nand_and_fetch_bool, (void*)-1);
94+
for (int i = 0; i < NUM_THREADS_ODD; ++i) pthread_join(thread[i], NULL);
9695
assert(nand_and_fetch_data == -1);
9796
}
9897
}

test/pthread/test_pthread_locale.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ int main (int argc, char *argv[]) {
3232
locale_t main_loc = do_test();
3333
locale_t child_loc;
3434

35-
if (emscripten_has_threading_support()) {
36-
long id = 1;
37-
pthread_t thread;
35+
pthread_t thread;
36+
int rtn;
3837

39-
pthread_create(&thread, NULL, thread_test, (void *)id);
38+
rtn = pthread_create(&thread, NULL, thread_test, (void *)NULL);
39+
printf("create: %d\n", rtn);
40+
assert(!rtn);
41+
rtn = pthread_join(thread, (void**)&child_loc);
42+
assert(!rtn);
4043

41-
pthread_join(thread, (void**)&child_loc);
42-
assert(main_loc == child_loc);
43-
}
44+
assert(main_loc == child_loc);
4445

4546
return 0;
4647
}

test/pthread/test_pthread_once.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
#include <emscripten.h>
1010
#include <emscripten/threading.h>
1111

12-
_Atomic int numInitialized = 0;
12+
int numInitialized = 0;
1313

1414
void once_init() {
1515
numInitialized++;
1616
}
1717

1818
#define NUM_THREADS 8
1919

20-
void *thread_main(void *arg)
21-
{
20+
void *thread_main(void *arg) {
2221
static pthread_once_t control = PTHREAD_ONCE_INIT;
2322
pthread_once(&control, &once_init);
2423
assert(numInitialized == 1);
@@ -33,10 +32,10 @@ int main() {
3332
pthread_create(&thread[i], NULL, thread_main, 0);
3433
}
3534

36-
if (emscripten_has_threading_support()) {
37-
for(int i = 0; i < NUM_THREADS; ++i) pthread_join(thread[i], NULL);
38-
assert(numInitialized == 1);
35+
for (int i = 0; i < NUM_THREADS; ++i) {
36+
pthread_join(thread[i], NULL);
3937
}
38+
assert(numInitialized == 1);
4039

4140
return 0;
4241
}

0 commit comments

Comments
 (0)