pthread thest from libc-testsuit
[libc-test] / src / thread / pthread.c
1 #define _POSIX_C_SOURCE 200809L
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <string.h>
8 #include "test.h"
9
10 #define TEST(r, f, x, m) ( \
11         ((r) = (f)) == (x) || \
12         (error("%s failed (" m ")\n", #f, r, x), 0) )
13
14 #define TEST_S(s, x, m) ( \
15         !strcmp((s),(x)) || \
16         (error("[%s] != [%s] (%s)\n", s, x, m), 0) )
17
18 static pthread_key_t k1, k2;
19
20 static void dtor(void *p)
21 {
22         *(int *)p = 1;
23 }
24
25 static void *start1(void *arg)
26 {
27         return arg;
28 }
29
30 static void *start2(void *arg)
31 {
32         int *p = arg;
33         if (pthread_setspecific(k1, p) || pthread_setspecific(k2, p+1))
34                 return arg;
35         return 0;
36 }
37
38 static void *start3(void *arg)
39 {
40         pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
41         pthread_barrier_wait(arg);
42         for (;;);
43         return 0;
44 }
45
46 static void cleanup4(void *arg)
47 {
48         *(int *)arg = 1;
49 }
50
51 static void *start4(void *arg)
52 {
53         pthread_cleanup_push(cleanup4, arg);
54         sleep(3);
55         pthread_cleanup_pop(0);
56         return 0;
57 }
58
59 static void cleanup4a2(void *arg)
60 {
61         *(int *)arg += 2;
62 }
63
64 static void cleanup4a3(void *arg)
65 {
66         *(int *)arg += 3;
67         pthread_exit(0);
68 }
69
70 static void cleanup4a4(void *arg)
71 {
72         *(int *)arg += 4;
73 }
74
75 static void *start4a(void *arg)
76 {
77         int *foo = arg;
78         pthread_cleanup_push(cleanup4, foo);
79         pthread_cleanup_push(cleanup4a2, foo+1);
80         pthread_cleanup_push(cleanup4a3, foo+2);
81         pthread_cleanup_push(cleanup4a4, foo+3);
82         sleep(3);
83         pthread_cleanup_pop(0);
84         pthread_cleanup_pop(0);
85         pthread_cleanup_pop(0);
86         pthread_cleanup_pop(0);
87         return 0;
88 }
89
90 static void *start5(void *arg)
91 {
92         pthread_mutex_lock(arg);
93         return 0;
94 }
95
96 static void *start6(void *arg)
97 {
98         void **args = arg;
99         pthread_mutex_lock(args[1]);
100         pthread_barrier_wait(args[0]);
101         nanosleep(&(struct timespec){ .tv_nsec = 10000000 }, 0);
102         return 0;
103 }
104
105 static void *start7(void *arg)
106 {
107         void **args = arg;
108         pthread_mutex_lock(args[1]);
109         pthread_cond_signal(args[0]);
110         pthread_mutex_unlock(args[1]);
111         return 0;
112 }
113
114
115 void test_pthread(void)
116 {
117         pthread_t td;
118         int r;
119         void *res;
120         int foo[4], bar[2];
121         pthread_barrier_t barrier2;
122 //      pthread_mutexattr_t mtx_a;
123 //      pthread_mutex_t *sh_mtx;
124 //      int fd;
125         pthread_mutex_t mtx;
126         pthread_cond_t cond;
127
128         TEST(r, pthread_barrier_init(&barrier2, 0, 2), 0, "creating barrier");
129
130         /* Test basic thread creation and joining */
131         TEST(r, pthread_create(&td, 0, start1, &res), 0, "failed to create thread");
132         res = 0;
133         TEST(r, pthread_join(td, &res), 0, "failed to join");
134         TEST(r, (res==&res), 1, "wrong result from join");
135
136         /* Test POSIX thread-specific data */
137         TEST(r, pthread_key_create(&k1, dtor), 0, "failed to create key");
138         TEST(r, pthread_key_create(&k2, dtor), 0, "failed to create key");
139         foo[0] = foo[1] = 0;
140         TEST(r, pthread_setspecific(k1, bar), 0, "failed to set tsd");
141         TEST(r, pthread_setspecific(k2, bar+1), 0, "failed to set tsd");
142         TEST(r, pthread_create(&td, 0, start2, foo), 0, "failed to create thread");
143         TEST(r, pthread_join(td, &res), 0, "failed to join");
144         TEST(res, res, 0, "pthread_setspecific failed in thread");
145         TEST(r, foo[0], 1, "dtor failed to run");
146         TEST(r, foo[1], 1, "dtor failed to run");
147         TEST(res, pthread_getspecific(k1), bar, "tsd corrupted");
148         TEST(res, pthread_getspecific(k2), bar+1, "tsd corrupted");
149         TEST(r, pthread_setspecific(k1, 0), 0, "failed to clear tsd");
150         TEST(r, pthread_setspecific(k2, 0), 0, "failed to clear tsd");
151         TEST(r, pthread_key_delete(k1), 0, "failed to destroy key");
152         TEST(r, pthread_key_delete(k2), 0, "failed to destroy key");
153
154         /* Asynchronous cancellation */
155         TEST(r, pthread_create(&td, 0, start3, &barrier2), 0, "failed to create thread");
156         pthread_barrier_wait(&barrier2);
157         TEST(r, pthread_cancel(td), 0, "canceling");
158         TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
159         TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status");
160
161         /* Cancellation cleanup handlers */
162         foo[0] = 0;
163         TEST(r, pthread_create(&td, 0, start4, foo), 0, "failed to create thread");
164         TEST(r, pthread_cancel(td), 0, "cancelling");
165         TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
166         TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status");
167         TEST(r, foo[0], 1, "cleanup handler failed to run");
168
169         /* Nested cleanup handlers */
170         memset(foo, 0, sizeof foo);
171         TEST(r, pthread_create(&td, 0, start4a, foo), 0, "failed to create thread");
172         TEST(r, pthread_cancel(td), 0, "cancelling");
173         TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
174         TEST(res, res, 0, "canceled thread exit status");
175         TEST(r, foo[0], 1, "cleanup handler failed to run");
176         TEST(r, foo[1], 2, "cleanup handler failed to run");
177         TEST(r, foo[2], 3, "cleanup handler failed to run");
178         TEST(r, foo[3], 4, "cleanup handler failed to run");
179
180         /* Robust mutexes */
181 /*
182         TEST(r, pthread_mutexattr_init(&mtx_a), 0, "initializing mutex attr");
183         TEST(r, pthread_mutexattr_setrobust(&mtx_a, PTHREAD_MUTEX_ROBUST), 0, "setting robust attribute");
184         TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex");
185         TEST(r, pthread_mutex_lock(&mtx), 0, "locking robust mutex");
186         TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking robust mutex");
187         TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread");
188         TEST(r, pthread_join(td, &res), 0, "joining thread");
189         TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d");
190         TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d");
191         TEST(r, pthread_mutex_lock(&mtx), ENOTRECOVERABLE, "re-locking orphaned robust mutex %d!=%d");
192         TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying unrecoverable mutex %d!=%d");
193
194         TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex");
195         TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread");
196         TEST(r, pthread_join(td, &res), 0, "joining thread");
197         TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d");
198         TEST(r, pthread_mutex_consistent(&mtx), 0, "%d!=%d");
199         TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d");
200         TEST(r, pthread_mutex_lock(&mtx), 0, "re-locking orphaned robust mutex %d!=%d");
201         TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying mutex %d!=%d");
202
203         TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "%d != %d");
204         TEST(r, pthread_create(&td, 0, start6, (void *[]){ &barrier2, &mtx }), 0, "%d != %d");
205         pthread_barrier_wait(&barrier2);
206         TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "%d != %d");
207         TEST(r, pthread_join(td, &res), 0, "%d != %d");
208         TEST(r, pthread_mutex_consistent(&mtx), 0, "%d != %d");
209         TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
210         TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
211 */
212         //TEST(r, (fd=open("/dev/zero", O_RDWR))>=0, 1, "opening zero page file");
213         //TEST(r, 
214
215         /* Condition variables */
216         TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d");
217         TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d");
218         TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
219         TEST(r, pthread_create(&td, 0, start7, (void *[]){ &cond, &mtx }), 0, "%d != %d");
220         TEST(r, pthread_cond_wait(&cond, &mtx), 0, "%d != %d");
221         TEST(r, pthread_join(td, &res), 0, "%d != %d");
222         TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
223         TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
224         TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d");
225 }