Skip to content

Commit db2f234

Browse files
authored
Convert all tests/pthreads/* to use btest_exit (#14727)
This makes them more readable/portable and avoids the need for `REPORT_RESULT` macro. As a followup I hope to convert all browser tests but this is major step in that direction.
1 parent 0bbe271 commit db2f234

File tree

59 files changed

+251
-426
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+251
-426
lines changed

src/library_html5.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -2771,17 +2771,27 @@ var LibraryJSEvents = {
27712771
clearTimeout(id);
27722772
},
27732773

2774+
emscripten_set_timeout_loop__deps: ['$callUserCallback',
2775+
#if !MINIMAL_RUNTIME
2776+
'$runtimeKeepalivePush', '$runtimeKeepalivePop',
2777+
#endif
2778+
],
27742779
emscripten_set_timeout_loop: function(cb, msecs, userData) {
27752780
function tick() {
27762781
var t = performance.now();
27772782
var n = t + msecs;
2778-
if ({{{ makeDynCall('idi', 'cb') }}}(t, userData)) {
2779-
// Save a little bit of code space: modern browsers should treat
2780-
// negative setTimeout as timeout of 0
2781-
// (https://stackoverflow.com/questions/8430966/is-calling-settimeout-with-a-negative-delay-ok)
2782-
setTimeout(tick, n - performance.now());
2783-
}
2783+
{{{ runtimeKeepalivePop() }}}
2784+
callUserCallback(function() {
2785+
if ({{{ makeDynCall('idi', 'cb') }}}(t, userData)) {
2786+
// Save a little bit of code space: modern browsers should treat
2787+
// negative setTimeout as timeout of 0
2788+
// (https://stackoverflow.com/questions/8430966/is-calling-settimeout-with-a-negative-delay-ok)
2789+
{{{ runtimeKeepalivePush() }}}
2790+
setTimeout(tick, n - performance.now());
2791+
}
2792+
});
27842793
}
2794+
{{{ runtimeKeepalivePush() }}}
27852795
return setTimeout(tick, 0);
27862796
},
27872797

tests/common.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -1515,16 +1515,16 @@ def compile_btest(self, args, reporting=Reporting.FULL):
15151515
self.run_process([EMCC] + self.get_emcc_args() + args)
15161516

15171517
def btest_exit(self, filename, assert_returncode=0, *args, **kwargs):
1518-
"""Special case of btest that reports its result solely via exiting
1519-
with a give result code.
1520-
1521-
In this case we set EXIT_RUNTIME and we don't need to provide the
1522-
REPORT_RESULT macro to the C code.
1523-
"""
1524-
self.set_setting('EXIT_RUNTIME')
1525-
kwargs['reporting'] = Reporting.JS_ONLY
1526-
kwargs['expected'] = 'exit:%d' % assert_returncode
1527-
return self.btest(filename, *args, **kwargs)
1518+
"""Special case of btest that reports its result solely via exiting
1519+
with a given result code.
1520+
1521+
In this case we set EXIT_RUNTIME and we don't need to provide the
1522+
REPORT_RESULT macro to the C code.
1523+
"""
1524+
self.set_setting('EXIT_RUNTIME')
1525+
kwargs['reporting'] = Reporting.JS_ONLY
1526+
kwargs['expected'] = 'exit:%d' % assert_returncode
1527+
return self.btest(filename, *args, **kwargs)
15281528

15291529
def btest(self, filename, expected=None, reference=None,
15301530
reference_slack=0, manual_reference=False, post_build=None,

tests/pthread/call_async.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <stdio.h>
99
#include <assert.h>
10+
#include <emscripten.h>
1011
#include <emscripten/threading.h>
1112

1213
int state = 0;
@@ -16,9 +17,8 @@ void increment() {
1617
}
1718

1819
void finish() {
19-
#ifdef REPORT_RESULT
20-
REPORT_RESULT(1);
21-
#endif
20+
assert(state == 2);
21+
emscripten_force_exit(0);
2222
}
2323

2424
int main() {
@@ -32,4 +32,6 @@ int main() {
3232
emscripten_dispatch_to_thread_async(emscripten_main_browser_thread_id(), EM_FUNC_SIG_V, &increment, 0);
3333
assert(state == 1);
3434
emscripten_dispatch_to_thread_async(emscripten_main_browser_thread_id(), EM_FUNC_SIG_V, &finish, 0);
35+
emscripten_exit_with_live_runtime();
36+
__builtin_unreachable();
3537
}

tests/pthread/call_sync_on_main_thread.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,5 @@ int main()
3232
assert(inWorker1 == PROXY_TO_PTHREAD);
3333
assert(inWorker2 == 0);
3434
assert(returnedInt == 42 + 4);
35-
#ifdef REPORT_RESULT
36-
REPORT_RESULT(1);
37-
#endif
35+
return 0;
3836
}

tests/pthread/emscripten_thread_sleep.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ void *thread_main(void *arg)
2424
Sleep(1000);
2525
Sleep(5000);
2626

27-
#ifdef REPORT_RESULT
28-
REPORT_RESULT(1);
29-
#endif
30-
return 0;
27+
emscripten_force_exit(0);
28+
return NULL;
3129
}
3230

3331
int main()
@@ -41,5 +39,5 @@ int main()
4139
pthread_t thread;
4240
pthread_create(&thread, NULL, thread_main, NULL);
4341
emscripten_exit_with_live_runtime();
44-
return 0;
42+
__builtin_unreachable();
4543
}

tests/pthread/main_thread_join.cpp

+3-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <assert.h>
77
#include <emscripten.h>
8+
#include <emscripten/threading.h>
89
#include <pthread.h>
910
#include <stdio.h>
1011

@@ -22,9 +23,7 @@ void loop() {
2223
if (pthread_tryjoin_np(thread, &retval) == 0) {
2324
emscripten_cancel_main_loop();
2425
assert(tries.load() == EXPECTED_TRIES);
25-
#ifdef REPORT_RESULT
26-
REPORT_RESULT(2);
27-
#endif
26+
emscripten_force_exit(2);
2827
}
2928
tries++;
3029
}
@@ -47,9 +46,6 @@ pthread_t CreateThread() {
4746

4847
int main() {
4948
if (!emscripten_has_threading_support()) {
50-
#ifdef REPORT_RESULT
51-
REPORT_RESULT(0);
52-
#endif
5349
printf("Skipped: Threading is not supported.\n");
5450
return 0;
5551
}
@@ -63,9 +59,7 @@ int main() {
6359
puts("trying to block...");
6460
pthread_join(thread, (void**)&status);
6561
puts("blocked ok.");
66-
#ifdef REPORT_RESULT
67-
REPORT_RESULT(1);
68-
#endif
62+
return 0;
6963
#endif // TRY_JOIN
7064
}
7165

tests/pthread/main_thread_wait.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
int main() {
1212
if (!emscripten_has_threading_support())
1313
{
14-
#ifdef REPORT_RESULT
15-
REPORT_RESULT(0);
16-
#endif
1714
printf("Skipped: Threading is not supported.\n");
1815
return 0;
1916
}
@@ -24,8 +21,6 @@ int main() {
2421
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
2522
pthread_cond_wait(&cv, &lock);
2623
puts("blocked ok.");
27-
#ifdef REPORT_RESULT
28-
REPORT_RESULT(1);
29-
#endif
24+
return 0;
3025
}
3126

tests/pthread/test_futex_wake_all.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdlib.h>
88
#include <pthread.h>
99
#include <emscripten/threading.h>
10+
#include <emscripten/emscripten.h>
1011
#include <assert.h>
1112
#include <math.h>
1213
#include <limits.h>
@@ -17,6 +18,10 @@ unsigned int futexVal = 0;
1718

1819
unsigned int numAwoken = 0;
1920

21+
pthread_t wakingThread;
22+
23+
pthread_t waitingThreads[NUM_THREADS];
24+
2025
void *WakingThread(void *arg)
2126
{
2227
// Wake all threads waiting for the futex address
@@ -31,7 +36,6 @@ void *WaitingThread(void *arg)
3136
// Last waiting thread creates the waking thread - this simplifies mutual synchronization needs
3237
if ((long)arg == NUM_THREADS-1)
3338
{
34-
pthread_t wakingThread;
3539
pthread_create(&wakingThread, 0, WakingThread, 0);
3640
}
3741

@@ -45,9 +49,7 @@ void *WaitingThread(void *arg)
4549
uint32_t old = emscripten_atomic_add_u32(&numAwoken, 1);
4650
if (old + 1 == NUM_THREADS)
4751
{
48-
#ifdef REPORT_RESULT
49-
REPORT_RESULT(0);
50-
#endif
52+
emscripten_force_exit(0);
5153
}
5254

5355
return 0;
@@ -57,16 +59,13 @@ int main()
5759
{
5860
if (!emscripten_has_threading_support())
5961
{
60-
#ifdef REPORT_RESULT
61-
REPORT_RESULT(0);
62-
#endif
6362
printf("Skipped: Threading is not supported.\n");
6463
return 0;
6564
}
6665

67-
pthread_t waitingThreads[NUM_THREADS];
6866
for(int i = 0; i < NUM_THREADS; ++i)
6967
{
7068
pthread_create(waitingThreads+i, 0, WaitingThread, (void*)i);
7169
}
70+
emscripten_exit_with_live_runtime();
7271
}

tests/pthread/test_large_pthread_allocation.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ int main()
3636
{
3737
if (!emscripten_has_threading_support())
3838
{
39-
#ifdef REPORT_RESULT
40-
REPORT_RESULT(0);
41-
#endif
4239
printf("Skipped: Threading is not supported.\n");
4340
return 0;
4441
}
@@ -62,8 +59,5 @@ int main()
6259
}
6360

6461
printf("Final average %f ms.\n", total / 10.0);
65-
66-
#ifdef REPORT_RESULT
67-
REPORT_RESULT(0);
68-
#endif
62+
return 0;
6963
}

tests/pthread/test_pthread_64bit_atomics.cpp

+2-8
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,6 @@ int main()
141141

142142
if (!emscripten_has_threading_support())
143143
{
144-
#ifdef REPORT_RESULT
145-
REPORT_RESULT(0);
146-
#endif
147144
printf("Skipped: Threading is not supported.\n");
148145
return 0;
149146
}
@@ -164,10 +161,7 @@ int main()
164161
printf("totalRead: %llu, totalWritten: %llu\n", totalRead, totalWritten);
165162
else
166163
printf("64-bit CAS test failed! totalRead != totalWritten (%llu != %llu)\n", totalRead, totalWritten);
167-
#ifdef REPORT_RESULT
168-
int result = (totalRead != totalWritten) ? 1 : 0;
169-
REPORT_RESULT(result);
170-
#else
164+
assert(totalRead == totalWritten);
171165
EM_ASM(out('Main: Test successfully finished.'));
172-
#endif
166+
return 0;
173167
}

tests/pthread/test_pthread_64bit_cxx11_atomics.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,5 @@ int main(void)
7171
printf("subbed3=%llx\n", c.load(std::memory_order_relaxed));
7272
assert(c.load(std::memory_order_relaxed) == 0x0E0D0C0B0A090807ULL);
7373

74-
#ifdef REPORT_RESULT
75-
REPORT_RESULT(0);
76-
#endif
77-
7874
return 0;
7975
}

tests/pthread/test_pthread_atomics.cpp

+3-9
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ int main()
161161

162162
if (!emscripten_has_threading_support())
163163
{
164-
#ifdef REPORT_RESULT
165-
REPORT_RESULT(0);
166-
#endif
167164
printf("Skipped: Threading is not supported.\n");
168165
return 0;
169166
}
@@ -184,10 +181,7 @@ int main()
184181
printf("totalRead: %llu, totalWritten: %llu\n", totalRead, totalWritten);
185182
else
186183
printf("32-bit CAS test failed! totalRead != totalWritten (%llu != %llu)\n", totalRead, totalWritten);
187-
#ifdef REPORT_RESULT
188-
int result = (totalRead != totalWritten) ? 1 : 0;
189-
REPORT_RESULT(result);
190-
#else
191-
EM_ASM(out('Main: Test successfully finished.'));
192-
#endif
184+
printf("Main: Test successfully finished.\n");
185+
assert(totalRead == totalWritten);
186+
return 0;
193187
}

tests/pthread/test_pthread_attr_getstack.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ int main()
4444
{
4545
if (!emscripten_has_threading_support())
4646
{
47-
#ifdef REPORT_RESULT
48-
REPORT_RESULT(0);
49-
#endif
5047
printf("Skipped: Threading is not supported.\n");
5148
return 0;
5249
}
@@ -60,8 +57,6 @@ int main()
6057
rc = pthread_join(thread, (void**)&result);
6158
assert(rc == 0);
6259

63-
#ifdef REPORT_RESULT
64-
REPORT_RESULT(result);
65-
#endif
66-
return result;
60+
assert(result == 0);
61+
return 0;
6762
}

tests/pthread/test_pthread_barrier.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,5 @@ int main(int argc, char **argv)
8282
}
8383
}
8484

85-
#ifdef REPORT_RESULT
86-
REPORT_RESULT(0);
87-
#endif
85+
return 0;
8886
}

tests/pthread/test_pthread_c11_threads.c

-4
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ int main(int argc, char* argv[]) {
9191
assert(thrd_create(&t6, thread_main, NULL) == thrd_success);
9292
assert(thrd_detach(t6) == thrd_success);
9393

94-
95-
#ifdef REPORT_RESULT
96-
REPORT_RESULT(0);
97-
#endif
9894
printf("done!\n");
9995
return 0;
10096
}

tests/pthread/test_pthread_cancel.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ int main()
3939
{
4040
if (!emscripten_has_threading_support())
4141
{
42-
#ifdef REPORT_RESULT
43-
REPORT_RESULT(1);
44-
#endif
4542
printf("Skipped: Threading is not supported.\n");
4643
return 0;
4744
}
@@ -58,10 +55,9 @@ int main()
5855
if (result == 1)
5956
{
6057
EM_ASM_INT( { out('After canceling, shared variable = ' + $0 + '.'); }, result);
61-
#ifdef REPORT_RESULT
62-
REPORT_RESULT(1);
63-
#endif
6458
return 0;
6559
}
6660
}
61+
62+
__builtin_unreachable();
6763
}

tests/pthread/test_pthread_clock_drift.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// found in the LICENSE file.
55

66
#include <pthread.h>
7+
#include <cassert>
78
#include <emscripten.h>
89
#include <emscripten/threading.h>
910
#include <math.h>
@@ -33,9 +34,9 @@ void *thread_main(void *arg)
3334
double timeDifference = pthreadTime - mainThreadTime;
3435
printf("Time difference between pthread and main thread is %f msecs.\n", timeDifference);
3536

36-
#ifdef REPORT_RESULT
37-
REPORT_RESULT(fabs(timeDifference) < 200); // The time difference here should be well less than 1 msec, but test against 200msecs to be super-sure.
38-
#endif
37+
// The time difference here should be well less than 1 msec, but test against 200msecs to be super-sure.
38+
assert(fabs(timeDifference) < 200);
39+
emscripten_force_exit(0);
3940
return 0;
4041
}
4142

0 commit comments

Comments
 (0)