xref: /netbsd-src/tests/lib/libpthread/t_mutex.c (revision d99781d3c9f97c53ce1999acc9e5bfadbeddfd17)
1 /* $NetBSD: t_mutex.c,v 1.10 2016/07/31 13:01:29 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.10 2016/07/31 13:01:29 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_MSG(res, -1, "sched_protect returned %d", res);
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 	atf_tc_set_md_var(tc, "require.user", "root");
338 }
339 
340 ATF_TC_BODY(mutex5, tc)
341 {
342 	int res;
343 	struct sched_param param;
344 	pthread_t child;
345 
346 	min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
347 	max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
348 	printf("min prio for FIFO = %d\n", min_fifo_prio);
349 	param.sched_priority = min_fifo_prio;
350 
351 	/* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */
352 	res = sched_setscheduler(getpid(), SCHED_FIFO, &param);
353 	printf("previous policy used = %d\n", res);
354 
355 	res = sched_getscheduler(getpid());
356 	ATF_REQUIRE_EQ_MSG(res, SCHED_FIFO, "sched %d != FIFO %d", res,
357 	    SCHED_FIFO);
358 
359 	PTHREAD_REQUIRE(pthread_mutexattr_init(&attr5));
360 	PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&attr5,
361 	    PTHREAD_PRIO_PROTECT));
362 	PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&attr5,
363 	    max_fifo_prio));
364 
365 	PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5));
366 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex5));
367 	printf("enter critical section for main\n");
368 	PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL));
369 	printf("main starts to sleep\n");
370 	sleep(10);
371 	printf("main completes\n");
372 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
373 	PTHREAD_REQUIRE(pthread_join(child, NULL));
374 }
375 
376 static pthread_mutex_t mutex6;
377 static int start = 0;
378 static uintmax_t high_cnt = 0, low_cnt = 0, MAX_LOOP = 100000000;
379 
380 static void *
381 high_prio(void* arg)
382 {
383 	struct sched_param param;
384 	int policy;
385 	param.sched_priority = min_fifo_prio + 10;
386 	pthread_t childid = pthread_self();
387 
388 	PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, &param));
389 	PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, &param));
390 	printf("high protect = %d, prio = %d\n",
391 	    _sched_protect(-2), param.sched_priority);
392 	ATF_REQUIRE_EQ(policy, 1);
393 	printf("high prio = %d\n", param.sched_priority);
394 	sleep(1);
395 	long tmp = 0;
396 	for (int i = 0; i < 20; i++) {
397 		while (high_cnt < MAX_LOOP) {
398 			tmp += (123456789 % 1234) * (987654321 % 54321);
399 			high_cnt += 1;
400 		}
401 		high_cnt = 0;
402 		sleep(1);
403 	}
404 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex6));
405 	if (start == 0) start = 2;
406 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
407 
408 	return 0;
409 }
410 
411 static void *
412 low_prio(void* arg)
413 {
414 	struct sched_param param;
415 	int policy;
416 	param.sched_priority = min_fifo_prio;
417 	pthread_t childid = pthread_self();
418 	int res = _sched_protect(max_fifo_prio);
419 	ATF_REQUIRE_EQ(res, 0);
420 	PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, &param));
421 	PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, &param));
422 	printf("low protect = %d, prio = %d\n", _sched_protect(-2),
423 	    param.sched_priority);
424 	ATF_REQUIRE_EQ(policy, 1);
425 	printf("low prio = %d\n", param.sched_priority);
426 	sleep(1);
427 	long tmp = 0;
428 	for (int i = 0; i < 20; i++) {
429 		while (low_cnt < MAX_LOOP) {
430 			tmp += (123456789 % 1234) * (987654321 % 54321);
431 			low_cnt += 1;
432 		}
433 		low_cnt = 0;
434 		sleep(1);
435 	}
436 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex6));
437 	if (start == 0)
438 		start = 1;
439 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
440 
441 	return 0;
442 }
443 
444 ATF_TC(mutex6);
445 ATF_TC_HEAD(mutex6, tc)
446 {
447 	atf_tc_set_md_var(tc, "descr",
448 	    "Checks scheduling for priority ceiling");
449 	atf_tc_set_md_var(tc, "require.user", "root");
450 }
451 
452 /*
453  * 1. main thread sets itself to be a realtime task and launched two tasks,
454  *    one has higher priority and the other has lower priority.
455  * 2. each child thread(low and high priority thread) sets its scheduler and
456  *    priority.
457  * 3. each child thread did several rounds of computation, after each round it
458  *    sleep 1 second.
459  * 4. the child thread with low priority will call _sched_protect to increase
460  *    its protect priority.
461  * 5. We verify the thread with low priority runs first.
462  *
463  * Why does it work? From the main thread, we launched the high
464  * priority thread first. This gives this thread the benefit of
465  * starting first. The low priority thread did not call _sched_protect(2).
466  * The high priority thread should finish the task first. After each
467  * round of computation, we call sleep, to put the task into the
468  * sleep queue, and wake up again after the timer expires. This
469  * gives the scheduler the chance to decide which task to run. So,
470  * the thread with real high priority will always block the thread
471  * with real low priority.
472  *
473  */
474 ATF_TC_BODY(mutex6, tc)
475 {
476 	struct sched_param param;
477 	int res;
478 	pthread_t high, low;
479 
480 	min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
481 	max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
482 	PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
483 	printf("min_fifo_prio = %d, max_fifo_info = %d\n", min_fifo_prio,
484 	    max_fifo_prio);
485 
486 	param.sched_priority = min_fifo_prio;
487 	res = sched_setscheduler(getpid(), SCHED_FIFO, &param);
488 	printf("previous policy used = %d\n", res);
489 
490 	res = sched_getscheduler(getpid());
491 	ATF_REQUIRE_EQ(res, 1);
492 	PTHREAD_REQUIRE(pthread_create(&high, NULL, high_prio, NULL));
493 	PTHREAD_REQUIRE(pthread_create(&low, NULL, low_prio, NULL));
494 	sleep(5);
495 	PTHREAD_REQUIRE(pthread_join(low, NULL));
496 	PTHREAD_REQUIRE(pthread_join(high, NULL));
497 
498 	ATF_REQUIRE_EQ(start, 1);
499 }
500 
501 ATF_TC(mutexattr1);
502 ATF_TC_HEAD(mutexattr1, tc)
503 {
504 	atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
505 }
506 
507 ATF_TC_BODY(mutexattr1, tc)
508 {
509 	pthread_mutexattr_t mattr;
510 	int protocol, target;
511 
512 	PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
513 
514 	target = PTHREAD_PRIO_NONE;
515 	PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
516 	PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
517 	ATF_REQUIRE_EQ(protocol, target);
518 
519 	/*
520 	target = PTHREAD_PRIO_INHERIT;
521 	PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
522 	PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
523 	ATF_REQUIRE_EQ(protocol, target);
524 	*/
525 
526 	target = PTHREAD_PRIO_PROTECT;
527 	PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
528 	PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
529 	ATF_REQUIRE_EQ(protocol, target);
530 }
531 
532 ATF_TC(mutexattr2);
533 ATF_TC_HEAD(mutexattr2, tc)
534 {
535 	atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
536 }
537 
538 ATF_TC_BODY(mutexattr2, tc)
539 {
540 	pthread_mutexattr_t mattr;
541 
542 	PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
543 	int max_prio = sched_get_priority_max(SCHED_FIFO);
544 	int min_prio = sched_get_priority_min(SCHED_FIFO);
545 	for (int i = min_prio; i <= max_prio; i++) {
546 		int prioceiling;
547 		PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i));
548 		PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr,
549 		    &prioceiling));
550 		ATF_REQUIRE_EQ(i, prioceiling);
551 	}
552 }
553 
554 ATF_TP_ADD_TCS(tp)
555 {
556 	ATF_TP_ADD_TC(tp, mutex1);
557 	ATF_TP_ADD_TC(tp, mutex2);
558 	ATF_TP_ADD_TC(tp, mutex3);
559 	ATF_TP_ADD_TC(tp, mutex4);
560 	ATF_TP_ADD_TC(tp, mutex5);
561 	ATF_TP_ADD_TC(tp, mutex6);
562 	ATF_TP_ADD_TC(tp, mutexattr1);
563 	ATF_TP_ADD_TC(tp, mutexattr2);
564 
565 	return atf_no_error();
566 }
567