Home | History | Annotate | Line # | Download | only in libpthread
      1 /* $NetBSD: t_join.c,v 1.10 2020/01/29 13:40:23 kamil Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jukka Ruohonen.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: t_join.c,v 1.10 2020/01/29 13:40:23 kamil Exp $");
     33 
     34 #include <errno.h>
     35 #include <pthread.h>
     36 #include <stdint.h>
     37 
     38 #include <atf-c.h>
     39 
     40 #include "h_common.h"
     41 
     42 #ifdef CHECK_STACK_ALIGNMENT
     43 extern int check_stack_alignment(void);
     44 #endif
     45 
     46 #define STACKSIZE	65536
     47 
     48 static bool error;
     49 
     50 static void *threadfunc1(void *);
     51 static void *threadfunc2(void *);
     52 
     53 ATF_TC(pthread_join);
     54 ATF_TC_HEAD(pthread_join, tc)
     55 {
     56 
     57 	atf_tc_set_md_var(tc, "descr",
     58 	    "Checks basic error conditions in pthread_join(3)");
     59 }
     60 
     61 ATF_TC_BODY(pthread_join, tc)
     62 {
     63 	pthread_t thread;
     64 
     65 	PTHREAD_REQUIRE(pthread_create(&thread, NULL, threadfunc1, NULL));
     66 	PTHREAD_REQUIRE(pthread_join(thread, NULL));
     67 }
     68 
     69 static void *
     70 threadfunc1(void *arg)
     71 {
     72 	pthread_t thread[25];
     73 	pthread_t caller;
     74 	void *val = NULL;
     75 	uintptr_t i;
     76 	int rv;
     77 	pthread_attr_t attr;
     78 
     79 	caller = pthread_self();
     80 
     81 #ifdef CHECK_STACK_ALIGNMENT
     82 	/*
     83 	 * Check alignment of thread stack, if supported.
     84 	 */
     85 	ATF_REQUIRE(check_stack_alignment());
     86 #endif
     87 
     88 	/*
     89 	 * The behavior is undefined, but should error
     90 	 * out, if we try to join the calling thread.
     91 	 */
     92 	rv = pthread_join(caller, NULL);
     93 
     94 	/*
     95 	 * The specification recommends EDEADLK.
     96 	 */
     97 	ATF_REQUIRE(rv != 0);
     98 	ATF_REQUIRE_EQ(rv, EDEADLK);
     99 
    100 	ATF_REQUIRE(pthread_attr_init(&attr) == 0);
    101 
    102 	for (i = 0; i < __arraycount(thread); i++) {
    103 
    104 		error = true;
    105 
    106 		ATF_REQUIRE(pthread_attr_setstacksize(&attr, STACKSIZE * (i + 1)) == 0);
    107 		ATF_REQUIRE(pthread_attr_setguardsize(&attr, STACKSIZE * (i + 2)) == 0);
    108 
    109 		rv = pthread_create(&thread[i], &attr, threadfunc2, (void *)i);
    110 
    111 		ATF_REQUIRE_EQ(rv, 0);
    112 
    113 		/*
    114 		 * Check join and exit condition.
    115 		 */
    116 		PTHREAD_REQUIRE(pthread_join(thread[i], &val));
    117 
    118 		ATF_REQUIRE_EQ(error, false);
    119 
    120 		ATF_REQUIRE(val != NULL);
    121 		ATF_REQUIRE(val == (void *)(i + 1));
    122 
    123 		/*
    124 		 * Once the thread has returned, ESRCH should
    125 		 * again follow if we try to join it again.
    126 		 */
    127 		rv = pthread_join(thread[i], NULL);
    128 
    129 		ATF_REQUIRE_EQ(rv, ESRCH);
    130 
    131 		/*
    132 		 * Try to detach the exited thread.
    133 		 */
    134 		rv = pthread_detach(thread[i]);
    135 
    136 		ATF_REQUIRE(rv != 0);
    137 	}
    138 
    139 	ATF_REQUIRE(pthread_attr_destroy(&attr) == 0);
    140 
    141 	pthread_exit(NULL);
    142 
    143 	return NULL;
    144 }
    145 
    146 static void *
    147 threadfunc2(void *arg)
    148 {
    149 	static uintptr_t i = 0;
    150 	uintptr_t j;
    151 	pthread_attr_t attr;
    152 	size_t stacksize, guardsize;
    153 
    154 	j = (uintptr_t)arg;
    155 
    156 	ATF_REQUIRE(pthread_getattr_np(pthread_self(), &attr) == 0);
    157 	ATF_REQUIRE(pthread_attr_getstacksize(&attr, &stacksize) == 0);
    158 	ATF_REQUIRE(stacksize == STACKSIZE * (j + 1));
    159 	ATF_REQUIRE(pthread_attr_getguardsize(&attr, &guardsize) == 0);
    160 	ATF_REQUIRE(guardsize == STACKSIZE * (j + 2));
    161 	ATF_REQUIRE(pthread_attr_destroy(&attr) == 0);
    162 
    163 	if (i++ == j)
    164 		error = false;
    165 
    166 	pthread_exit((void *)i);
    167 
    168 	return NULL;
    169 }
    170 
    171 ATF_TP_ADD_TCS(tp)
    172 {
    173 
    174 	ATF_TP_ADD_TC(tp, pthread_join);
    175 
    176 	return atf_no_error();
    177 }
    178