t_mutex.c revision 1.8 1 /* $NetBSD: t_mutex.c,v 1.8 2016/07/03 14:24:59 christos Exp $ */
2
3 /*
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __COPYRIGHT("@(#) Copyright (c) 2008\
31 The NetBSD Foundation, inc. All rights reserved.");
32 __RCSID("$NetBSD: t_mutex.c,v 1.8 2016/07/03 14:24:59 christos Exp $");
33
34 #include <pthread.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <errno.h>
38 #include <unistd.h>
39 #include <sys/sched.h>
40 #include <sys/param.h>
41
42 #include <atf-c.h>
43
44 #include "h_common.h"
45
46 static pthread_mutex_t mutex;
47 static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
48 static int global_x;
49
50 static void *
51 mutex1_threadfunc(void *arg)
52 {
53 int *param;
54
55 printf("2: Second thread.\n");
56
57 param = arg;
58 printf("2: Locking mutex\n");
59 pthread_mutex_lock(&mutex);
60 printf("2: Got mutex. *param = %d\n", *param);
61 ATF_REQUIRE_EQ(*param, 20);
62 (*param)++;
63
64 pthread_mutex_unlock(&mutex);
65
66 return param;
67 }
68
69 ATF_TC(mutex1);
70 ATF_TC_HEAD(mutex1, tc)
71 {
72 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
73 }
74 ATF_TC_BODY(mutex1, tc)
75 {
76 int x;
77 pthread_t new;
78 void *joinval;
79
80 printf("1: Mutex-test 1\n");
81
82 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
83 x = 1;
84 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
85 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x));
86 printf("1: Before changing the value.\n");
87 sleep(2);
88 x = 20;
89 printf("1: Before releasing the mutex.\n");
90 sleep(2);
91 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
92 printf("1: After releasing the mutex.\n");
93 PTHREAD_REQUIRE(pthread_join(new, &joinval));
94
95 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
96 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
97 x, *(int *)joinval);
98 ATF_REQUIRE_EQ(x, 21);
99 ATF_REQUIRE_EQ(*(int *)joinval, 21);
100 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
101 }
102
103 static void *
104 mutex2_threadfunc(void *arg)
105 {
106 long count = *(int *)arg;
107
108 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
109
110 while (count--) {
111 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
112 global_x++;
113 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
114 }
115
116 return (void *)count;
117 }
118
119 ATF_TC(mutex2);
120 ATF_TC_HEAD(mutex2, tc)
121 {
122 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
123 #if defined(__powerpc__)
124 atf_tc_set_md_var(tc, "timeout", "40");
125 #endif
126 }
127 ATF_TC_BODY(mutex2, tc)
128 {
129 int count, count2;
130 pthread_t new;
131 void *joinval;
132
133 printf("1: Mutex-test 2\n");
134
135 #if defined(__powerpc__)
136 atf_tc_expect_timeout("PR port-powerpc/44387");
137 #endif
138
139 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
140
141 global_x = 0;
142 count = count2 = 10000000;
143
144 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
145 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
146
147 printf("1: Thread %p\n", pthread_self());
148
149 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
150
151 while (count--) {
152 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
153 global_x++;
154 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
155 }
156
157 PTHREAD_REQUIRE(pthread_join(new, &joinval));
158
159 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
160 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
161 global_x, (long)joinval);
162 ATF_REQUIRE_EQ(global_x, 20000000);
163
164 #if defined(__powerpc__)
165 /* XXX force a timeout in ppc case since an un-triggered race
166 otherwise looks like a "failure" */
167 /* We sleep for longer than the timeout to make ATF not
168 complain about unexpected success */
169 sleep(41);
170 #endif
171 }
172
173 static void *
174 mutex3_threadfunc(void *arg)
175 {
176 long count = *(int *)arg;
177
178 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
179
180 while (count--) {
181 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
182 global_x++;
183 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
184 }
185
186 return (void *)count;
187 }
188
189 ATF_TC(mutex3);
190 ATF_TC_HEAD(mutex3, tc)
191 {
192 atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
193 "initializer");
194 #if defined(__powerpc__)
195 atf_tc_set_md_var(tc, "timeout", "40");
196 #endif
197 }
198 ATF_TC_BODY(mutex3, tc)
199 {
200 int count, count2;
201 pthread_t new;
202 void *joinval;
203
204 printf("1: Mutex-test 3\n");
205
206 #if defined(__powerpc__)
207 atf_tc_expect_timeout("PR port-powerpc/44387");
208 #endif
209
210 global_x = 0;
211 count = count2 = 10000000;
212
213 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
214 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
215
216 printf("1: Thread %p\n", pthread_self());
217
218 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
219
220 while (count--) {
221 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
222 global_x++;
223 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
224 }
225
226 PTHREAD_REQUIRE(pthread_join(new, &joinval));
227
228 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
229 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
230 global_x, (long)joinval);
231 ATF_REQUIRE_EQ(global_x, 20000000);
232
233 #if defined(__powerpc__)
234 /* XXX force a timeout in ppc case since an un-triggered race
235 otherwise looks like a "failure" */
236 /* We sleep for longer than the timeout to make ATF not
237 complain about unexpected success */
238 sleep(41);
239 #endif
240 }
241
242 static void *
243 mutex4_threadfunc(void *arg)
244 {
245 int *param;
246
247 printf("2: Second thread.\n");
248
249 param = arg;
250 printf("2: Locking mutex\n");
251 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
252 printf("2: Got mutex. *param = %d\n", *param);
253 (*param)++;
254
255 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
256
257 return param;
258 }
259
260 ATF_TC(mutex4);
261 ATF_TC_HEAD(mutex4, tc)
262 {
263 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
264 }
265 ATF_TC_BODY(mutex4, tc)
266 {
267 int x;
268 pthread_t new;
269 pthread_mutexattr_t mattr;
270 void *joinval;
271
272 printf("1: Mutex-test 4\n");
273
274 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
275 PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
276
277 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
278
279 PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
280
281 x = 1;
282 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
283 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
284
285 printf("1: Before recursively acquiring the mutex.\n");
286 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
287
288 printf("1: Before releasing the mutex once.\n");
289 sleep(2);
290 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
291 printf("1: After releasing the mutex once.\n");
292
293 x = 20;
294
295 printf("1: Before releasing the mutex twice.\n");
296 sleep(2);
297 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
298 printf("1: After releasing the mutex twice.\n");
299
300 PTHREAD_REQUIRE(pthread_join(new, &joinval));
301
302 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
303 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
304 x, *(int *)joinval);
305 ATF_REQUIRE_EQ(x, 21);
306 ATF_REQUIRE_EQ(*(int *)joinval, 21);
307 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
308 }
309
310 static pthread_mutexattr_t attr5;
311 static pthread_mutex_t mutex5;
312 static int min_fifo_prio, max_fifo_prio;
313
314 static void *
315 child_func(void* arg)
316 {
317 int res;
318
319 printf("child is waiting\n");
320 res = _sched_protect(-2);
321 ATF_REQUIRE_EQ(res, -1);
322 ATF_REQUIRE_EQ(errno, ENOENT);
323 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex5));
324 printf("child is owning resource\n");
325 res = _sched_protect(-2);
326 ATF_REQUIRE_EQ(res, max_fifo_prio);
327 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
328 printf("child is done\n");
329
330 return 0;
331 }
332
333 ATF_TC(mutex5);
334 ATF_TC_HEAD(mutex5, tc)
335 {
336 atf_tc_set_md_var(tc, "descr", "Checks mutexes for priority setting");
337 }
338
339 ATF_TC_BODY(mutex5, tc)
340 {
341 int res;
342 struct sched_param param;
343 pthread_t child;
344
345 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
346 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
347 printf("min prio for FIFO = %d\n", min_fifo_prio);
348 param.sched_priority = min_fifo_prio;
349 /* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */
350 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
351 printf("previous policy used = %d\n", res);
352
353 res = sched_getscheduler(getpid());
354 ATF_REQUIRE_EQ(res, 1);
355
356 PTHREAD_REQUIRE(pthread_mutexattr_init(&attr5));
357 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&attr5,
358 PTHREAD_PRIO_PROTECT));
359 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&attr5,
360 max_fifo_prio));
361
362 PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5));
363 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex5));
364 printf("enter critical section for main\n");
365 PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL));
366 printf("main starts to sleep\n");
367 sleep(10);
368 printf("main completes\n");
369 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
370 PTHREAD_REQUIRE(pthread_join(child, NULL));
371 }
372
373 static pthread_mutex_t mutex6;
374 static int start = 0;
375 static uintmax_t high_cnt = 0, low_cnt = 0, MAX_LOOP = 100000000;
376
377 static void *
378 high_prio(void* arg)
379 {
380 struct sched_param param;
381 int policy;
382 param.sched_priority = min_fifo_prio + 10;
383 pthread_t childid = pthread_self();
384
385 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
386 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
387 printf("high protect = %d, prio = %d\n",
388 _sched_protect(-2), param.sched_priority);
389 ATF_REQUIRE_EQ(policy, 1);
390 printf("high prio = %d\n", param.sched_priority);
391 sleep(1);
392 int i;
393 long tmp = 0;
394 for (i = 0; i<20; i++) {
395 while (high_cnt < MAX_LOOP) {
396 tmp += (123456789 % 1234) * (987654321 % 54321);
397 high_cnt += 1;
398 }
399 high_cnt = 0;
400 sleep(1);
401 }
402 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex6));
403 if (start == 0) start = 2;
404 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
405
406 return 0;
407 }
408
409 static void *
410 low_prio(void* arg)
411 {
412 struct sched_param param;
413 int policy;
414 param.sched_priority = min_fifo_prio;
415 pthread_t childid = pthread_self();
416 int res = _sched_protect(max_fifo_prio);
417 ATF_REQUIRE_EQ(res, 0);
418 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
419 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
420 printf("low protect = %d, prio = %d\n", _sched_protect(-2),
421 param.sched_priority);
422 ATF_REQUIRE_EQ(policy, 1);
423 printf("low prio = %d\n", param.sched_priority);
424 sleep(1);
425 for (int i = 0; i < 20; i++) {
426 while (low_cnt < MAX_LOOP) {
427 long tmp;
428 tmp += (123456789 % 1234) * (987654321 % 54321);
429 low_cnt += 1;
430 }
431 low_cnt = 0;
432 sleep(1);
433 }
434 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex6));
435 if (start == 0)
436 start = 1;
437 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
438
439 return 0;
440 }
441
442 ATF_TC(mutex6);
443 ATF_TC_HEAD(mutex6, tc)
444 {
445 atf_tc_set_md_var(tc, "descr",
446 "Checks scheduling for priority ceiling");
447 }
448
449 /*
450 * 1. main thread sets itself to be a realtime task and launched two tasks,
451 * one has higher priority and the other has lower priority.
452 * 2. each child thread(low and high priority thread) sets its scheduler and
453 * priority.
454 * 3. each child thread did several rounds of computation, after each round it
455 * sleep 1 second.
456 * 4. the child thread with low priority will call _sched_protect to increase
457 * its protect priority.
458 * 5. We verify the thread with low priority runs first.
459 *
460 * Why does it work? From the main thread, we launched the high
461 * priority thread first. This gives this thread the benefit of
462 * starting first. The low priority thread did not call _sched_protect(2).
463 * The high priority thread should finish the task first. After each
464 * round of computation, we call sleep, to put the task into the
465 * sleep queue, and wake up again after the timer expires. This
466 * gives the scheduler the chance to decide which task to run. So,
467 * the thread with real high priority will always block the thread
468 * with real low priority.
469 *
470 */
471 ATF_TC_BODY(mutex6, tc)
472 {
473 int res;
474 pthread_t high, low;
475 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
476 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
477 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
478 printf("min_fifo_prio = %d, max_fifo_info = %d\n", min_fifo_prio,
479 max_fifo_prio);
480 struct sched_param param;
481 param.sched_priority = min_fifo_prio;
482 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
483 printf("previous policy used = %d\n", res);
484
485 res = sched_getscheduler(getpid());
486 ATF_REQUIRE_EQ(res, 1);
487 PTHREAD_REQUIRE(pthread_create(&high, NULL, high_prio, NULL));
488 PTHREAD_REQUIRE(pthread_create(&low, NULL, low_prio, NULL));
489 sleep(5);
490 PTHREAD_REQUIRE(pthread_join(low, NULL));
491 PTHREAD_REQUIRE(pthread_join(high, NULL));
492
493 ATF_REQUIRE_EQ(start, 1);
494 }
495
496 ATF_TC(mutexattr1);
497 ATF_TC_HEAD(mutexattr1, tc)
498 {
499 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
500 }
501
502 ATF_TC_BODY(mutexattr1, tc)
503 {
504 pthread_mutexattr_t mattr;
505 int protocol, target;
506
507 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
508
509 target = PTHREAD_PRIO_NONE;
510 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
511 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
512 ATF_REQUIRE_EQ(protocol, target);
513
514 /*
515 target = PTHREAD_PRIO_INHERIT;
516 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
517 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
518 ATF_REQUIRE_EQ(protocol, target);
519 */
520
521 target = PTHREAD_PRIO_PROTECT;
522 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
523 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
524 ATF_REQUIRE_EQ(protocol, target);
525 }
526
527 ATF_TC(mutexattr2);
528 ATF_TC_HEAD(mutexattr2, tc)
529 {
530 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
531 }
532
533 ATF_TC_BODY(mutexattr2, tc)
534 {
535 pthread_mutexattr_t mattr;
536
537 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
538 int max_prio = sched_get_priority_max(SCHED_FIFO);
539 int min_prio = sched_get_priority_min(SCHED_FIFO);
540 for (int i = min_prio; i <= max_prio; i++) {
541 int prioceiling;
542 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i));
543 PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr,
544 &prioceiling));
545 ATF_REQUIRE_EQ(i, prioceiling);
546 }
547 }
548
549 ATF_TP_ADD_TCS(tp)
550 {
551 ATF_TP_ADD_TC(tp, mutex1);
552 ATF_TP_ADD_TC(tp, mutex2);
553 ATF_TP_ADD_TC(tp, mutex3);
554 ATF_TP_ADD_TC(tp, mutex4);
555 ATF_TP_ADD_TC(tp, mutex5);
556 ATF_TP_ADD_TC(tp, mutex6);
557 ATF_TP_ADD_TC(tp, mutexattr1);
558 ATF_TP_ADD_TC(tp, mutexattr2);
559
560 return atf_no_error();
561 }
562