xref: /netbsd-src/tests/lib/libpthread/t_detach.c (revision 78c0e08e9b9c9768162917dd6c276237b7787d2a)
1*78c0e08eSchristos /* $NetBSD: t_detach.c,v 1.2 2017/01/16 16:29:54 christos Exp $ */
268f3621eSjruoho 
368f3621eSjruoho /*-
468f3621eSjruoho  * Copyright (c) 2011 The NetBSD Foundation, Inc.
568f3621eSjruoho  * All rights reserved.
668f3621eSjruoho  *
768f3621eSjruoho  * This code is derived from software contributed to The NetBSD Foundation
868f3621eSjruoho  * by Jukka Ruohonen.
968f3621eSjruoho  *
1068f3621eSjruoho  * Redistribution and use in source and binary forms, with or without
1168f3621eSjruoho  * modification, are permitted provided that the following conditions
1268f3621eSjruoho  * are met:
1368f3621eSjruoho  * 1. Redistributions of source code must retain the above copyright
1468f3621eSjruoho  *    notice, this list of conditions and the following disclaimer.
1568f3621eSjruoho  * 2. Redistributions in binary form must reproduce the above copyright
1668f3621eSjruoho  *    notice, this list of conditions and the following disclaimer in the
1768f3621eSjruoho  *    documentation and/or other materials provided with the distribution.
1868f3621eSjruoho  *
1968f3621eSjruoho  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2068f3621eSjruoho  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2168f3621eSjruoho  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2268f3621eSjruoho  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2368f3621eSjruoho  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2468f3621eSjruoho  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2568f3621eSjruoho  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2668f3621eSjruoho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2768f3621eSjruoho  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2868f3621eSjruoho  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2968f3621eSjruoho  * POSSIBILITY OF SUCH DAMAGE.
3068f3621eSjruoho  */
3168f3621eSjruoho #include <sys/cdefs.h>
32*78c0e08eSchristos __RCSID("$NetBSD: t_detach.c,v 1.2 2017/01/16 16:29:54 christos Exp $");
3368f3621eSjruoho 
3468f3621eSjruoho #include <errno.h>
35*78c0e08eSchristos #include <pthread.h>
36*78c0e08eSchristos #include <time.h>
3768f3621eSjruoho 
3868f3621eSjruoho #include <atf-c.h>
3968f3621eSjruoho 
4068f3621eSjruoho #include "h_common.h"
4168f3621eSjruoho 
4268f3621eSjruoho static void	*func(void *);
4368f3621eSjruoho 
4468f3621eSjruoho static void *
func(void * arg)4568f3621eSjruoho func(void *arg)
4668f3621eSjruoho {
47*78c0e08eSchristos 	sleep(2);
4868f3621eSjruoho 	return NULL;
4968f3621eSjruoho }
5068f3621eSjruoho 
5168f3621eSjruoho ATF_TC(pthread_detach);
ATF_TC_HEAD(pthread_detach,tc)5268f3621eSjruoho ATF_TC_HEAD(pthread_detach, tc)
5368f3621eSjruoho {
5468f3621eSjruoho 	atf_tc_set_md_var(tc, "descr", "A test of pthread_detach(3)");
5568f3621eSjruoho }
5668f3621eSjruoho 
ATF_TC_BODY(pthread_detach,tc)5768f3621eSjruoho ATF_TC_BODY(pthread_detach, tc)
5868f3621eSjruoho {
5968f3621eSjruoho 	const int state = PTHREAD_CREATE_JOINABLE;
6068f3621eSjruoho 	pthread_attr_t attr;
6168f3621eSjruoho 	pthread_t t;
6268f3621eSjruoho 	int rv;
6368f3621eSjruoho 
6468f3621eSjruoho 	/*
6568f3621eSjruoho 	 * Create a joinable thread.
6668f3621eSjruoho 	 */
6768f3621eSjruoho 	PTHREAD_REQUIRE(pthread_attr_init(&attr));
6868f3621eSjruoho 	PTHREAD_REQUIRE(pthread_attr_setdetachstate(&attr, state));
6968f3621eSjruoho 	PTHREAD_REQUIRE(pthread_create(&t, &attr, func, NULL));
7068f3621eSjruoho 
7168f3621eSjruoho 	/*
7268f3621eSjruoho 	 * Detach the thread and try to
7368f3621eSjruoho 	 * join it; EINVAL should follow.
7468f3621eSjruoho 	 */
7568f3621eSjruoho 	PTHREAD_REQUIRE(pthread_detach(t));
7668f3621eSjruoho 
77*78c0e08eSchristos 	sleep(1);
7868f3621eSjruoho 	rv = pthread_join(t, NULL);
7968f3621eSjruoho 	ATF_REQUIRE(rv == EINVAL);
8068f3621eSjruoho 
81*78c0e08eSchristos 	sleep(3);
82*78c0e08eSchristos 
8368f3621eSjruoho 	/*
8468f3621eSjruoho 	 * As usual, ESRCH should follow if
8568f3621eSjruoho 	 * we try to detach an invalid thread.
8668f3621eSjruoho 	 */
87*78c0e08eSchristos 	rv = pthread_cancel(t);
8868f3621eSjruoho 	ATF_REQUIRE(rv == ESRCH);
8968f3621eSjruoho }
9068f3621eSjruoho 
ATF_TP_ADD_TCS(tp)9168f3621eSjruoho ATF_TP_ADD_TCS(tp)
9268f3621eSjruoho {
9368f3621eSjruoho 	ATF_TP_ADD_TC(tp, pthread_detach);
9468f3621eSjruoho 
9568f3621eSjruoho 	return atf_no_error();
9668f3621eSjruoho }
97