forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pthread_join.cpp
58 lines (51 loc) · 1.38 KB
/
test_pthread_join.cpp
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
#include <pthread.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <emscripten.h>
#include <emscripten/threading.h>
int fib(int n)
{
if (n <= 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n-2);
}
static void *thread_start(void *arg)
{
int n = (int)arg;
EM_ASM_INT( { Module['print']('Thread: Computing fib('+$0+')...'); }, n);
int fibn = fib(n);
EM_ASM_INT( { Module['print']('Thread: Computation done. fib('+$0+') = '+$1+'.'); }, n, fibn);
pthread_exit((void*)fibn);
}
int main()
{
// Test existence of nanosleep(), https://github.com/kripken/emscripten/issues/4578
struct timespec ts = { 1, 0 };
nanosleep(&ts, 0);
int result = 0;
if (!emscripten_has_threading_support())
{
#ifdef REPORT_RESULT
result = 6765;
REPORT_RESULT();
#endif
printf("Skipped: Threading is not supported.\n");
return 0;
}
pthread_t thr;
int n = 20;
EM_ASM_INT( { Module['print']('Main: Spawning thread to compute fib('+$0+')...'); }, n);
int s = pthread_create(&thr, NULL, thread_start, (void*)n);
assert(s == 0);
EM_ASM(Module['print']('Main: Waiting for thread to join.'););
s = pthread_join(thr, (void**)&result);
assert(s == 0);
EM_ASM_INT( { Module['print']('Main: Thread joined with result: '+$0+'.'); }, result);
#ifdef REPORT_RESULT
REPORT_RESULT();
#endif
}