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