forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pthread_create.cpp
More file actions
108 lines (92 loc) · 2.6 KB
/
test_pthread_create.cpp
File metadata and controls
108 lines (92 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <emscripten.h>
#include <emscripten/threading.h>
#include <assert.h>
#define NUM_THREADS 8
int fib(int n)
{
if (n <= 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n-2);
}
unsigned int global_shared_data[NUM_THREADS];
void *ThreadMain(void *arg)
{
int idx = (int)arg;
unsigned int param = global_shared_data[idx];
#define N 100
EM_ASM_INT( { Module['printErr']('Thread idx '+$0+': sorting ' + $1 + ' numbers with param ' + $2 + '.') }, idx, N, param);
unsigned int n[N];
for(unsigned int i = 0; i < N; ++i)
n[i] = (i + param) % N; // Create a shifted increasing sequence of numbers [0, N-1[
// Sort the sequence to ordered [0, N[
for(unsigned int i = 0; i < N; ++i)
for(unsigned int j = i; j < N; ++j)
{
if (n[i] > n[j])
{
unsigned int t = n[i];
n[i] = n[j];
n[j] = t;
}
}
// Ensure all elements are in place.
int numGood = 0;
for(unsigned int i = 0; i < N; ++i)
if (n[i] == i) ++numGood;
else EM_ASM_INT( { Module['printErr']('n['+$0+']='+$1); }, i, n[i]);
EM_ASM_INT( { Module['print']('Thread idx ' + $0 + ' with param '+$1+': all done with result '+$2+'.'); }, idx, param, numGood);
pthread_exit((void*)numGood);
}
pthread_t thread[NUM_THREADS];
int numThreadsToCreate = 1000;
void CreateThread(int i)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
static int counter = 1;
global_shared_data[i] = (counter++ * 12141231) & 0x7FFFFFFF; // Arbitrary random'ish data for perturbing the sort for this thread task.
// EM_ASM_INT( { Module['print']('Main: Creating thread idx ' + $0 + ' (param ' + $1 + ')'); }, i, global_shared_data[i]);
int rc = pthread_create(&thread[i], &attr, ThreadMain, (void*)i);
assert(rc == 0);
pthread_attr_destroy(&attr);
}
int main()
{
int result = 0;
if (!emscripten_has_threading_support())
{
#ifdef REPORT_RESULT
REPORT_RESULT();
#endif
printf("Skipped: Threading is not supported.\n");
return 0;
}
// Create initial threads.
for(int i = 0; i < NUM_THREADS; ++i)
CreateThread(i);
// Join all threads and create more.
for(int i = 0; i < NUM_THREADS; ++i)
{
if (thread[i])
{
int status;
int rc = pthread_join(thread[i], (void**)&status);
assert(rc == 0);
EM_ASM_INT( { Module['printErr']('Main: Joined thread idx ' + $0 + ' (param ' + $1 + ') with status ' + $2); }, i, global_shared_data[i], (int)status);
assert(status == N);
thread[i] = 0;
if (numThreadsToCreate > 0)
{
--numThreadsToCreate;
CreateThread(i);
}
}
}
#ifdef REPORT_RESULT
REPORT_RESULT();
#endif
}