3c25c5741f884284aee051223c14f4511bf34c1e
[libc-test] / src / functional / pthread.c
1 #include <pthread.h>
2 #include <semaphore.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8 #include <signal.h>
9 #include "test.h"
10
11 #define TEST(r, f, x, m) ( \
12         msg = #f, ((r) = (f)) == (x) || \
13         (error("%s failed (" m ")\n", #f, r, x), 0) )
14
15 #define TEST_S(s, x, m) ( \
16         !strcmp((s),(x)) || \
17         (error("[%s] != [%s] (%s)\n", s, x, m), 0) )
18
19 static volatile char *msg = "";
20
21 static void alarmhandler(int sig) {
22         error("timeout in %s\n", msg);
23         _Exit(1);
24 }
25
26 static pthread_key_t k1, k2;
27
28 static void dtor(void *p)
29 {
30         *(int *)p = 1;
31 }
32
33 static void *start1(void *arg)
34 {
35         return arg;
36 }
37
38 static void *start2(void *arg)
39 {
40         int *p = arg;
41         if (pthread_setspecific(k1, p) || pthread_setspecific(k2, p+1))
42                 return arg;
43         return 0;
44 }
45
46 static void *start3(void *arg)
47 {
48         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
49         sem_post(arg);
50         for (;;);
51         return 0;
52 }
53
54 static void cleanup4(void *arg)
55 {
56         *(int *)arg = 1;
57 }
58
59 static void *start4(void *arg)
60 {
61         pthread_cleanup_push(cleanup4, arg);
62         sleep(3);
63         pthread_cleanup_pop(0);
64         return 0;
65 }
66
67 static void cleanup4a2(void *arg)
68 {
69         *(int *)arg += 2;
70 }
71
72 static void cleanup4a3(void *arg)
73 {
74         *(int *)arg += 3;
75 }
76
77 static void cleanup4a4(void *arg)
78 {
79         *(int *)arg += 4;
80 }
81
82 static void *start4a(void *arg)
83 {
84         int *foo = arg;
85         pthread_cleanup_push(cleanup4, foo);
86         pthread_cleanup_push(cleanup4a2, foo+1);
87         pthread_cleanup_push(cleanup4a3, foo+2);
88         pthread_cleanup_push(cleanup4a4, foo+3);
89         sleep(3);
90         pthread_cleanup_pop(0);
91         pthread_cleanup_pop(0);
92         pthread_cleanup_pop(0);
93         pthread_cleanup_pop(0);
94         return 0;
95 }
96
97 static void *start5(void *arg)
98 {
99         pthread_mutex_lock(arg);
100         return 0;
101 }
102
103 static void *start6(void *arg)
104 {
105         void **args = arg;
106         pthread_mutex_lock(args[1]);
107         pthread_barrier_wait(args[0]);
108         nanosleep(&(struct timespec){ .tv_nsec = 10000000 }, 0);
109         return 0;
110 }
111
112 static void *start7(void *arg)
113 {
114         void **args = arg;
115         pthread_mutex_lock(args[1]);
116         pthread_cond_signal(args[0]);
117         pthread_mutex_unlock(args[1]);
118         return 0;
119 }
120
121 static void *start8(void *arg)
122 {
123         void **args = arg;
124         pthread_mutex_t *m = args[1];
125         pthread_cond_t *c = args[0];
126         int *x = args[2];
127
128         pthread_mutex_lock(m);
129         while (*x) pthread_cond_wait(c, m);
130         pthread_mutex_unlock(m);
131
132         return 0;
133 }
134
135 int main(void)
136 {
137         pthread_t td, td1, td2, td3;
138         int r;
139         void *res;
140         int foo[4], bar[2];
141         pthread_barrier_t barrier2;
142         pthread_mutexattr_t mtx_a;
143         sem_t sem1;
144         pthread_mutex_t mtx;
145         pthread_cond_t cond;
146
147         signal(SIGALRM, alarmhandler);
148         alarm(10);
149
150         TEST(r, pthread_barrier_init(&barrier2, 0, 2), 0, "creating barrier");
151         TEST(r, sem_init(&sem1, 0, 0), 0, "creating semaphore");
152
153         /* Test basic thread creation and joining */
154         TEST(r, pthread_create(&td, 0, start1, &res), 0, "failed to create thread");
155         res = 0;
156         TEST(r, pthread_join(td, &res), 0, "failed to join");
157         TEST(r, (res==&res), 1, "wrong result from join");
158
159         /* Test POSIX thread-specific data */
160         TEST(r, pthread_key_create(&k1, dtor), 0, "failed to create key");
161         TEST(r, pthread_key_create(&k2, dtor), 0, "failed to create key");
162         foo[0] = foo[1] = 0;
163         TEST(r, pthread_setspecific(k1, bar), 0, "failed to set tsd");
164         TEST(r, pthread_setspecific(k2, bar+1), 0, "failed to set tsd");
165         TEST(r, pthread_create(&td, 0, start2, foo), 0, "failed to create thread");
166         TEST(r, pthread_join(td, &res), 0, "failed to join");
167         TEST(res, res, 0, "pthread_setspecific failed in thread");
168         TEST(r, foo[0], 1, "dtor failed to run");
169         TEST(r, foo[1], 1, "dtor failed to run");
170         TEST(res, pthread_getspecific(k1), bar, "tsd corrupted");
171         TEST(res, pthread_getspecific(k2), bar+1, "tsd corrupted");
172         TEST(r, pthread_setspecific(k1, 0), 0, "failed to clear tsd");
173         TEST(r, pthread_setspecific(k2, 0), 0, "failed to clear tsd");
174         TEST(r, pthread_key_delete(k1), 0, "failed to destroy key");
175         TEST(r, pthread_key_delete(k2), 0, "failed to destroy key");
176
177         /* Asynchronous cancellation */
178         TEST(r, pthread_create(&td, 0, start3, &sem1), 0, "failed to create thread");
179         while (sem_wait(&sem1));
180         TEST(r, pthread_cancel(td), 0, "canceling");
181         TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
182         TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status");
183
184         /* Cancellation cleanup handlers */
185         foo[0] = 0;
186         TEST(r, pthread_create(&td, 0, start4, foo), 0, "failed to create thread");
187         TEST(r, pthread_cancel(td), 0, "cancelling");
188         TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
189         TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status");
190         TEST(r, foo[0], 1, "cleanup handler failed to run");
191
192         /* Nested cleanup handlers */
193         memset(foo, 0, sizeof foo);
194         TEST(r, pthread_create(&td, 0, start4a, foo), 0, "failed to create thread");
195         TEST(r, pthread_cancel(td), 0, "cancelling");
196         TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
197         TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status");
198         TEST(r, foo[0], 1, "cleanup handler failed to run");
199         TEST(r, foo[1], 2, "cleanup handler failed to run");
200         TEST(r, foo[2], 3, "cleanup handler failed to run");
201         TEST(r, foo[3], 4, "cleanup handler failed to run");
202
203         /* Robust mutexes */
204         TEST(r, pthread_mutexattr_init(&mtx_a), 0, "initializing mutex attr");
205         TEST(r, pthread_mutexattr_setrobust(&mtx_a, PTHREAD_MUTEX_ROBUST), 0, "setting robust attribute");
206         TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex");
207         TEST(r, pthread_mutex_lock(&mtx), 0, "locking robust mutex");
208         TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking robust mutex");
209         TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread");
210         TEST(r, pthread_join(td, &res), 0, "joining thread");
211         TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d");
212         TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d");
213         TEST(r, pthread_mutex_lock(&mtx), ENOTRECOVERABLE, "re-locking orphaned robust mutex %d!=%d");
214         TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying unrecoverable mutex %d!=%d");
215
216         TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex");
217         TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread");
218         TEST(r, pthread_join(td, &res), 0, "joining thread");
219         TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d");
220         TEST(r, pthread_mutex_consistent(&mtx), 0, "%d!=%d");
221         TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d");
222         TEST(r, pthread_mutex_lock(&mtx), 0, "re-locking orphaned robust mutex %d!=%d");
223         TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying mutex %d!=%d");
224
225         TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "%d != %d");
226         TEST(r, pthread_create(&td, 0, start6, (void *[]){ &barrier2, &mtx }), 0, "%d != %d");
227         pthread_barrier_wait(&barrier2);
228         TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "%d != %d");
229         TEST(r, pthread_join(td, &res), 0, "%d != %d");
230         TEST(r, pthread_mutex_consistent(&mtx), 0, "%d != %d");
231         TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
232         TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
233
234         //TEST(r, (fd=open("/dev/zero", O_RDWR))>=0, 1, "opening zero page file");
235         //TEST(r, 
236
237         /* Condition variables */
238         TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d");
239         TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d");
240         TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
241         TEST(r, pthread_create(&td, 0, start7, (void *[]){ &cond, &mtx }), 0, "%d != %d");
242         TEST(r, pthread_cond_wait(&cond, &mtx), 0, "%d != %d");
243         TEST(r, pthread_join(td, &res), 0, "%d != %d");
244         TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
245         TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
246         TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d");
247
248         /* Condition variables with multiple waiters */
249         TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d");
250         TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d");
251         TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
252         foo[0] = 1;
253         TEST(r, pthread_create(&td1, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
254         TEST(r, pthread_create(&td2, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
255         TEST(r, pthread_create(&td3, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
256         TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
257         nanosleep(&(struct timespec){.tv_nsec=1000000}, 0);
258         foo[0] = 0;
259         TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
260         TEST(r, pthread_cond_signal(&cond), 0, "%d != %d");
261         TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
262         TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
263         TEST(r, pthread_cond_signal(&cond), 0, "%d != %d");
264         TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
265         TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
266         TEST(r, pthread_cond_signal(&cond), 0, "%d != %d");
267         TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
268         TEST(r, pthread_join(td1, 0), 0, "%d != %d");
269         TEST(r, pthread_join(td2, 0), 0, "%d != %d");
270         TEST(r, pthread_join(td3, 0), 0, "%d != %d");
271         TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
272         TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d");
273
274         /* Condition variables with broadcast signals */
275         TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d");
276         TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d");
277         TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
278         foo[0] = 1;
279         TEST(r, pthread_create(&td1, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
280         TEST(r, pthread_create(&td2, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
281         TEST(r, pthread_create(&td3, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
282         TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
283         nanosleep(&(struct timespec){.tv_nsec=1000000}, 0);
284         TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
285         foo[0] = 0;
286         TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
287         TEST(r, pthread_cond_broadcast(&cond), 0, "%d != %d");
288         TEST(r, pthread_join(td1, 0), 0, "%d != %d");
289         TEST(r, pthread_join(td2, 0), 0, "%d != %d");
290         TEST(r, pthread_join(td3, 0), 0, "%d != %d");
291         TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
292         TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d");
293         return test_status;
294 }