forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pthread_run_on_main_thread_flood.cpp
85 lines (74 loc) · 1.74 KB
/
test_pthread_run_on_main_thread_flood.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
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
#include <emscripten/threading.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
volatile int func_called = 0;
void v()
{
emscripten_atomic_add_u32((void*)&func_called, 1);
}
void test_sync()
{
printf("Testing sync proxied runs:\n");
emscripten_atomic_store_u32((void*)&func_called, 0);
for(int i = 0; i < 1000; ++i)
{
emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, v);
assert(emscripten_atomic_load_u32((void*)&func_called) == i+1);
}
}
void test_async()
{
printf("Testing async proxied runs:\n");
emscripten_atomic_store_u32((void*)&func_called, 0);
for(int i = 0; i < 1000; ++i)
{
emscripten_async_run_in_main_runtime_thread(EM_FUNC_SIG_V, v);
}
while(emscripten_atomic_load_u32((void*)&func_called) != 1000)
;
}
void test_async_waitable()
{
printf("Testing waitable async proxied runs:\n");
emscripten_atomic_store_u32((void*)&func_called, 0);
for(int i = 0; i < 1000; ++i)
{
em_queued_call *c = emscripten_async_waitable_run_in_main_runtime_thread(EM_FUNC_SIG_V, v);
if (i == 999)
{
EMSCRIPTEN_RESULT r = emscripten_wait_for_call_v(c, INFINITY);
assert(r == EMSCRIPTEN_RESULT_SUCCESS);
}
emscripten_async_waitable_close(c);
}
assert(func_called == 1000);
}
void *thread_main(void*)
{
test_sync();
test_async();
test_async_waitable();
pthread_exit(0);
}
int main()
{
if (emscripten_has_threading_support())
{
test_sync();
test_async_waitable();
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_t thread;
int rc = pthread_create(&thread, &attr, thread_main, 0);
assert(rc == 0);
rc = pthread_join(thread, 0);
assert(rc == 0);
}
test_async();
#ifdef REPORT_RESULT
int result = 0;
REPORT_RESULT();
#endif
}