FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
emscripten/tests/pthread/test_pthread_atomics.cpp at incoming · JavaScriptIOT/emscripten · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
JavaScriptIOT
/
emscripten
Public
forked from
emscripten-core/emscripten
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
emscripten
/
tests
/
pthread
/
test_pthread_atomics.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
185 lines (162 loc) · 4.88 KB
Breadcrumbs
emscripten
/
tests
/
pthread
/
test_pthread_atomics.cpp
Copy path
File metadata and controls
185 lines (162 loc) · 4.88 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#
include
<
stdio.h
>
#
include
<
stdlib.h
>
#
include
<
memory.h
>
#
include
<
pthread.h
>
#
include
<
emscripten.h
>
#
include
<
emscripten/threading.h
>
#
include
<
assert.h
>
#
if
!defined(__EMSCRIPTEN_PTHREADS__) || __EMSCRIPTEN_PTHREADS__ != 1
#
error
__EMSCRIPTEN_PTHREADS__ should have been defined to be equal to 1 when building with pthreads support enabled!
#
endif
#
define
NUM_THREADS
8
volatile
unsigned
char
globalUchar =
0
;
volatile
unsigned
short
globalUshort =
0
;
volatile
unsigned
int
globalUint =
0
;
volatile
float
globalFloat =
0
.
0f
;
volatile
double
globalDouble =
0.0
;
volatile
uint64_t
globalU64 =
0
;
const
int
N =
10
;
int
sharedData[N] = {};
struct
Test
{
int
op;
int
threadId;
};
uint64_t
threadCasAccumulatedWrittenData[
NUM_THREADS
] = {};
uint64_t
threadCasAccumulatedReadData[
NUM_THREADS
] = {};
int
rand_32
()
{
return
(
int
)(
emscripten_random
() *
0x3FFFFFFF
);
}
void
*
ThreadMain
(
void
*arg)
{
assert
(
pthread_self
() !=
0
);
assert
(globalUchar ==
5
);
assert
(globalUshort ==
5
);
assert
(globalUint ==
5
);
assert
(globalFloat ==
5
.
0f
);
assert
(globalDouble ==
5.0
);
assert
(globalU64 ==
5
);
struct
Test
*t = (
struct
Test
*)arg;
EM_ASM
(Module[
'
print
'
](
'
Thread
'
+ $
0
+
'
for test
'
+ $
1
+
'
: starting computation.
'
), t->
threadId
, t->
op
);
for
(
int
i =
0
; i <
99999
; ++i)
for
(
int
j =
0
; j < N; ++j)
{
switch
(t->
op
)
{
case
0
:
emscripten_atomic_add_u32
(&sharedData[j],
1
);
break
;
case
1
:
emscripten_atomic_sub_u32
(&sharedData[j],
1
);
break
;
case
2
:
emscripten_atomic_and_u32
(&sharedData[j], ~(
1UL
<< t->
threadId
));
break
;
case
3
:
emscripten_atomic_or_u32
(&sharedData[j],
1UL
<< t->
threadId
);
break
;
case
4
:
emscripten_atomic_xor_u32
(&sharedData[j],
1UL
<< t->
threadId
);
break
;
case
5
:
{
//
Atomically load and store data, and test that each individual u8 is the same.
int
data =
emscripten_atomic_load_u32
(&sharedData[j]);
uint8_t
dataU8[
4
];
memcpy
(dataU8, &data,
4
);
assert
(dataU8[
0
] >=
10
&& dataU8[
0
] <
10
+
NUM_THREADS
);
assert
(dataU8[
0
] == dataU8[
1
] && dataU8[
0
] == dataU8[
2
] && dataU8[
0
] == dataU8[
3
]);
dataU8[
0
] = dataU8[
1
] = dataU8[
2
] = dataU8[
3
] =
10
+ t->
threadId
;
memcpy
(&data, dataU8,
4
);
emscripten_atomic_store_u32
(&sharedData[j], data);
}
break
;
case
6
:
{
int
newData =
rand_32
();
int
data;
int
prevData;
do
{
data =
emscripten_atomic_load_u32
(&sharedData[j]);
prevData =
emscripten_atomic_cas_u32
(&sharedData[j], data, newData);
}
while
(prevData != data);
threadCasAccumulatedReadData[t->
threadId
] += data;
threadCasAccumulatedWrittenData[t->
threadId
] += newData;
}
break
;
}
}
EM_ASM
(Module[
'
print
'
](
'
Thread
'
+ $
0
+
'
for test
'
+ $
1
+
'
: finished, exit()ing.
'
), t->
threadId
, t->
op
);
pthread_exit
(
0
);
}
struct
Test
t[
NUM_THREADS
] = {};
pthread_t
thread[
NUM_THREADS
];
void
RunTest
(
int
test)
{
pthread_attr_t
attr;
pthread_attr_init
(&attr);
pthread_attr_setdetachstate
(&attr,
PTHREAD_CREATE_JOINABLE
);
pthread_attr_setstacksize
(&attr,
4
*
1024
);
printf
(
"
Main thread has thread ID %d
\n
"
, (
int
)
pthread_self
());
assert
(
pthread_self
() !=
0
);
switch
(test)
{
case
2
:
memset
(sharedData,
0xFF
,
sizeof
(sharedData));
break
;
case
5
:
memset
(sharedData,
0x10
,
sizeof
(sharedData));
break
;
default
:
memset
(sharedData,
0
,
sizeof
(sharedData));
break
;
}
EM_ASM
(Module[
'
print
'
](
'
Main: Starting test
'
+ $
0
), test);
for
(
int
i =
0
; i <
NUM_THREADS
; ++i)
{
t[i].
op
= test;
t[i].
threadId
= i;
int
rc =
pthread_create
(&thread[i], &attr, ThreadMain, &t[i]);
assert
(rc ==
0
);
}
pthread_attr_destroy
(&attr);
for
(
int
i =
0
; i <
NUM_THREADS
; ++i)
{
int
status =
1
;
int
rc =
pthread_join
(thread[i], (
void
**)&status);
assert
(rc ==
0
);
assert
(status ==
0
);
}
int
val = sharedData[
0
];
EM_ASM
(Module[
'
print
'
](
'
Main: Test
'
+ $
0
+
'
finished. Result:
'
+ $
1
), test, val);
if
(test !=
6
)
{
for
(
int
i =
1
; i < N; ++i)
assert
(sharedData[i] == sharedData[
0
]);
}
}
int
main
()
{
globalUchar =
5
;
globalUshort =
5
;
globalUint =
5
;
globalFloat =
5
.
0f
;
globalDouble =
5.0
;
globalU64 =
5
;
emscripten_atomic_fence
();
__sync_synchronize
();
if
(!
emscripten_has_threading_support
())
{
#
ifdef
REPORT_RESULT
REPORT_RESULT
(
0
);
#
endif
printf
(
"
Skipped: Threading is not supported.
\n
"
);
return
0
;
}
for
(
int
i =
0
; i <
7
; ++i)
RunTest
(i);
uint64_t
totalRead =
0
;
uint64_t
totalWritten =
0
;
for
(
int
i =
0
; i <
NUM_THREADS
; ++i)
{
totalRead += threadCasAccumulatedReadData[i];
totalWritten += threadCasAccumulatedWrittenData[i];
}
for
(
int
i =
0
; i < N; ++i)
totalRead += sharedData[i];
if
(totalRead == totalWritten)
printf
(
"
totalRead: %llu, totalWritten: %llu
\n
"
, totalRead, totalWritten);
else
printf
(
"
32-bit CAS test failed! totalRead != totalWritten (%llu != %llu)
\n
"
, totalRead, totalWritten);
#
ifdef
REPORT_RESULT
int
result = (totalRead != totalWritten) ?
1
:
0
;
REPORT_RESULT
(result);
#
else
EM_ASM
(Module[
'
print
'
](
'
Main: Test successfully finished.
'
));
#
endif
}
Back
|
FazBrowse Home
|
New Git URL