1 #define _POSIX_C_SOURCE 200809L
11 #define TEST(r, f, x, m) ( \
12 msg = #f, ((r) = (f)) == (x) || \
13 (error("%s failed (" m ")\n", #f, r, x), 0) )
15 #define TEST_S(s, x, m) ( \
17 (error("[%s] != [%s] (%s)\n", s, x, m), 0) )
19 static volatile char *msg = "";
21 static void alarmhandler(int sig) {
22 error("timeout in %s\n", msg);
26 static pthread_key_t k1, k2;
28 static void dtor(void *p)
33 static void *start1(void *arg)
38 static void *start2(void *arg)
41 if (pthread_setspecific(k1, p) || pthread_setspecific(k2, p+1))
46 static void *start3(void *arg)
48 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
49 pthread_barrier_wait(arg);
54 static void cleanup4(void *arg)
59 static void *start4(void *arg)
61 pthread_cleanup_push(cleanup4, arg);
63 pthread_cleanup_pop(0);
67 static void cleanup4a2(void *arg)
72 static void cleanup4a3(void *arg)
78 static void cleanup4a4(void *arg)
83 static void *start4a(void *arg)
86 pthread_cleanup_push(cleanup4, foo);
87 pthread_cleanup_push(cleanup4a2, foo+1);
88 pthread_cleanup_push(cleanup4a3, foo+2);
89 pthread_cleanup_push(cleanup4a4, foo+3);
91 pthread_cleanup_pop(0);
92 pthread_cleanup_pop(0);
93 pthread_cleanup_pop(0);
94 pthread_cleanup_pop(0);
98 static void *start5(void *arg)
100 pthread_mutex_lock(arg);
104 static void *start6(void *arg)
107 pthread_mutex_lock(args[1]);
108 pthread_barrier_wait(args[0]);
109 nanosleep(&(struct timespec){ .tv_nsec = 10000000 }, 0);
113 static void *start7(void *arg)
116 pthread_mutex_lock(args[1]);
117 pthread_cond_signal(args[0]);
118 pthread_mutex_unlock(args[1]);
123 void test_pthread(void)
129 pthread_barrier_t barrier2;
130 // pthread_mutexattr_t mtx_a;
131 // pthread_mutex_t *sh_mtx;
136 signal(SIGALRM, alarmhandler);
139 TEST(r, pthread_barrier_init(&barrier2, 0, 2), 0, "creating barrier");
141 /* Test basic thread creation and joining */
142 TEST(r, pthread_create(&td, 0, start1, &res), 0, "failed to create thread");
144 TEST(r, pthread_join(td, &res), 0, "failed to join");
145 TEST(r, (res==&res), 1, "wrong result from join");
147 /* Test POSIX thread-specific data */
148 TEST(r, pthread_key_create(&k1, dtor), 0, "failed to create key");
149 TEST(r, pthread_key_create(&k2, dtor), 0, "failed to create key");
151 TEST(r, pthread_setspecific(k1, bar), 0, "failed to set tsd");
152 TEST(r, pthread_setspecific(k2, bar+1), 0, "failed to set tsd");
153 TEST(r, pthread_create(&td, 0, start2, foo), 0, "failed to create thread");
154 TEST(r, pthread_join(td, &res), 0, "failed to join");
155 TEST(res, res, 0, "pthread_setspecific failed in thread");
156 TEST(r, foo[0], 1, "dtor failed to run");
157 TEST(r, foo[1], 1, "dtor failed to run");
158 TEST(res, pthread_getspecific(k1), bar, "tsd corrupted");
159 TEST(res, pthread_getspecific(k2), bar+1, "tsd corrupted");
160 TEST(r, pthread_setspecific(k1, 0), 0, "failed to clear tsd");
161 TEST(r, pthread_setspecific(k2, 0), 0, "failed to clear tsd");
162 TEST(r, pthread_key_delete(k1), 0, "failed to destroy key");
163 TEST(r, pthread_key_delete(k2), 0, "failed to destroy key");
165 /* Asynchronous cancellation */
166 TEST(r, pthread_create(&td, 0, start3, &barrier2), 0, "failed to create thread");
167 pthread_barrier_wait(&barrier2);
168 TEST(r, pthread_cancel(td), 0, "canceling");
169 TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
170 TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status");
172 /* Cancellation cleanup handlers */
174 TEST(r, pthread_create(&td, 0, start4, foo), 0, "failed to create thread");
175 TEST(r, pthread_cancel(td), 0, "cancelling");
176 TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
177 TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status");
178 TEST(r, foo[0], 1, "cleanup handler failed to run");
180 /* Nested cleanup handlers */
181 memset(foo, 0, sizeof foo);
182 TEST(r, pthread_create(&td, 0, start4a, foo), 0, "failed to create thread");
183 TEST(r, pthread_cancel(td), 0, "cancelling");
184 TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
185 TEST(res, res, 0, "canceled thread exit status");
186 TEST(r, foo[0], 1, "cleanup handler failed to run");
187 TEST(r, foo[1], 2, "cleanup handler failed to run");
188 TEST(r, foo[2], 3, "cleanup handler failed to run");
189 TEST(r, foo[3], 4, "cleanup handler failed to run");
193 TEST(r, pthread_mutexattr_init(&mtx_a), 0, "initializing mutex attr");
194 TEST(r, pthread_mutexattr_setrobust(&mtx_a, PTHREAD_MUTEX_ROBUST), 0, "setting robust attribute");
195 TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex");
196 TEST(r, pthread_mutex_lock(&mtx), 0, "locking robust mutex");
197 TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking robust mutex");
198 TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread");
199 TEST(r, pthread_join(td, &res), 0, "joining thread");
200 TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d");
201 TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d");
202 TEST(r, pthread_mutex_lock(&mtx), ENOTRECOVERABLE, "re-locking orphaned robust mutex %d!=%d");
203 TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying unrecoverable mutex %d!=%d");
205 TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex");
206 TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread");
207 TEST(r, pthread_join(td, &res), 0, "joining thread");
208 TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d");
209 TEST(r, pthread_mutex_consistent(&mtx), 0, "%d!=%d");
210 TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d");
211 TEST(r, pthread_mutex_lock(&mtx), 0, "re-locking orphaned robust mutex %d!=%d");
212 TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying mutex %d!=%d");
214 TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "%d != %d");
215 TEST(r, pthread_create(&td, 0, start6, (void *[]){ &barrier2, &mtx }), 0, "%d != %d");
216 pthread_barrier_wait(&barrier2);
217 TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "%d != %d");
218 TEST(r, pthread_join(td, &res), 0, "%d != %d");
219 TEST(r, pthread_mutex_consistent(&mtx), 0, "%d != %d");
220 TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
221 TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
223 //TEST(r, (fd=open("/dev/zero", O_RDWR))>=0, 1, "opening zero page file");
226 /* Condition variables */
227 TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d");
228 TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d");
229 TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
230 TEST(r, pthread_create(&td, 0, start7, (void *[]){ &cond, &mtx }), 0, "%d != %d");
231 TEST(r, pthread_cond_wait(&cond, &mtx), 0, "%d != %d");
232 TEST(r, pthread_join(td, &res), 0, "%d != %d");
233 TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
234 TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
235 TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d");