aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/pthread_cancel.c
blob: ca4ca346c383d6b29be980d3e8d740e0679c4aaf (plain)
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
#include <assert.h>
#include <pthread.h>
#include <unistd.h>

_Atomic int worker_ready = 0;
_Atomic int main_ready = 0;

static void *worker1(void *arg) {
	(void)arg;
	assert(!pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL));
	assert(!pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL));

	worker_ready = 1;

	while (1) sleep(1);

	return NULL;
}

static void *worker2(void *arg) {
	(void) arg;
	assert(!pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL));

	worker_ready = 1;

	while(!main_ready);

	// Cancellation point - we should cancel right now
	sleep(1);

	assert(!"Expected to be cancelled!");
	__builtin_unreachable();
}

static void *worker3(void *arg) {
	(void) arg;
	assert(!pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL));

	worker_ready = 1;

	while(!main_ready);

	// Cancellation point - we should cancel right now
	pthread_testcancel();

	assert(!"Expected to be cancelled!");
	__builtin_unreachable();
}

static void *worker4(void *arg) {
	(void) arg;
	assert(!pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL));

	worker_ready = 1;
	sleep(1);

	// We expect to be canceled during the sleep

	assert(!"Expected to be cancelled!");
	__builtin_unreachable();
}

static void *worker5(void *arg) {
	assert(!pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL));

	worker_ready = 1;

	while(!main_ready);

	// Cancellation point - we should NOT cancel right now
	pthread_testcancel();

	int *arg_int = (int*)arg;
	*arg_int = 1;

	return NULL;
}

static void check_result(pthread_t thread) {

	void *ret_val = NULL;
	int ret = pthread_join(thread, &ret_val);
	assert(!ret);
	assert(ret_val == PTHREAD_CANCELED);
}

int main() {
	pthread_t thread;
	int ret = pthread_create(&thread, NULL, &worker1, NULL);
	assert(!ret);

	while (!worker_ready);
	ret = pthread_cancel(thread);
	assert(!ret);
	check_result(thread);

	main_ready = 0;
	worker_ready = 0;
	main_ready = 0;
	ret = pthread_create(&thread, NULL, &worker2, NULL);
	assert(!ret);

	while(!worker_ready);
	ret = pthread_cancel(thread);
	assert(!ret);
	main_ready = 1;
	check_result(thread);

	main_ready = 0;
	worker_ready = 0;
	main_ready = 0;
	ret = pthread_create(&thread, NULL, &worker3, NULL);
	assert(!ret);

	while(!worker_ready);
	ret = pthread_cancel(thread);
	assert(!ret);
	main_ready = 1;
	check_result(thread);

	worker_ready = 0;
	main_ready = 0;
	ret = pthread_create(&thread, NULL, &worker4, NULL);
	assert(!ret);

	while(!worker_ready);
	ret = pthread_cancel(thread);
	assert(!ret);
	main_ready = 1;
	check_result(thread);

	// Test for bug where pthread_testcancel() was not checking if
	// cancellation was triggered properly.
	worker_ready = 0;
	int pthread_arg = 0;
	main_ready = 0;
	ret = pthread_create(&thread, NULL, &worker5, &pthread_arg);
	assert(!ret);

	while(!worker_ready);
	main_ready = 1;

	void *ret_val = NULL;
	ret = pthread_join(thread, &ret_val);
	assert(!ret);
	assert(!ret_val);
	assert(pthread_arg == 1);

	return 0;
}