Skip to content

Commit d246366

Browse files
authored
Cleanup pthread_cancel tests. NFC (emscripten-core#26536)
- Use boolean flags - Avoid magic numbers - Cleanup formatting
1 parent a95d758 commit d246366

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

test/pthread/test_pthread_cancel.c

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,44 @@
55

66
#include <pthread.h>
77
#include <sys/types.h>
8+
#include <stdbool.h>
89
#include <stdio.h>
910
#include <stdlib.h>
1011
#include <assert.h>
1112
#include <unistd.h>
1213
#include <errno.h>
1314
#include <emscripten/console.h>
1415

15-
_Atomic long res = 43;
16-
static void cleanup_handler(void *arg)
17-
{
16+
_Atomic bool done_cleanup = false;
17+
18+
void cleanup_handler(void *arg) {
1819
long a = (long)arg;
1920
emscripten_outf("Called clean-up handler with arg %ld", a);
20-
res -= a;
21+
assert(a == 42);
22+
done_cleanup = true;
2123
}
2224

23-
static void *thread_start(void *arg)
24-
{
25+
void *thread_start(void *arg) {
2526
pthread_cleanup_push(cleanup_handler, (void*)42);
2627
emscripten_out("Thread started!");
27-
for(;;)
28-
{
28+
while (1) {
2929
pthread_testcancel();
3030
}
31-
res = 1000; // Shouldn't ever reach here.
31+
assert(false);
3232
pthread_cleanup_pop(0);
3333
}
3434

35-
pthread_t thr;
36-
37-
int main()
38-
{
35+
int main() {
36+
pthread_t thr;
3937
int s = pthread_create(&thr, NULL, thread_start, (void*)0);
4038
assert(s == 0);
4139
emscripten_out("Canceling thread..");
4240
s = pthread_cancel(thr);
4341
assert(s == 0);
4442

45-
for(;;)
46-
{
47-
int result = res;
48-
if (result == 1)
49-
{
50-
emscripten_outf("After canceling, shared variable = %d", result);
51-
return 0;
52-
}
43+
while (!done_cleanup) {
5344
}
5445

55-
__builtin_trap();
46+
emscripten_outf("After canceling, cleanup complete");
47+
return 0;
5648
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Canceling thread..
22
Thread started!
33
Called clean-up handler with arg 42
4-
After canceling, shared variable = 1
4+
After canceling, cleanup complete

test/pthread/test_pthread_cancel_async.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,31 @@
1111
#include <assert.h>
1212
#include <unistd.h>
1313
#include <errno.h>
14+
15+
#ifdef __EMSCRIPTEN__
1416
#include <emscripten/console.h>
17+
#else
18+
void emscripten_out(const char* msg) {
19+
printf("%s\n", msg);
20+
}
21+
22+
void emscripten_outf(const char* msg, ...) {
23+
printf("%s\n", msg);
24+
}
25+
#endif
1526

1627
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
17-
_Atomic long res = 43;
18-
_Atomic int started = false;
28+
_Atomic bool started = false;
29+
_Atomic bool done_cleanup = false;
1930

20-
static void cleanup_handler(void *arg)
21-
{
31+
void cleanup_handler(void *arg) {
2232
long a = (long)arg;
2333
emscripten_outf("Called clean-up handler with arg %ld", a);
24-
res -= a;
34+
assert(a == 42);
35+
done_cleanup = true;
2536
}
2637

27-
static void *thread_start(void *arg) {
38+
void *thread_start(void *arg) {
2839
// Setup thread for async cancellation only
2940
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
3041
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
@@ -61,7 +72,7 @@ int main() {
6172
s = pthread_cancel(thr);
6273
assert(s == 0);
6374
// Busy wait until thread cancel handler has been run
64-
while (res != 1) {
75+
while (!done_cleanup) {
6576
sched_yield();
6677
}
6778

0 commit comments

Comments
 (0)