t_detach.c revision 276478
1423SN/A/* $NetBSD: t_detach.c,v 1.1 2011/03/24 13:52:04 jruoho Exp $ */
21929Sihse
3423SN/A/*-
4423SN/A * Copyright (c) 2011 The NetBSD Foundation, Inc.
5423SN/A * All rights reserved.
6423SN/A *
7423SN/A * This code is derived from software contributed to The NetBSD Foundation
8423SN/A * by Jukka Ruohonen.
9423SN/A *
10423SN/A * Redistribution and use in source and binary forms, with or without
11423SN/A * modification, are permitted provided that the following conditions
12423SN/A * are met:
13423SN/A * 1. Redistributions of source code must retain the above copyright
14423SN/A *    notice, this list of conditions and the following disclaimer.
15423SN/A * 2. Redistributions in binary form must reproduce the above copyright
16423SN/A *    notice, this list of conditions and the following disclaimer in the
17423SN/A *    documentation and/or other materials provided with the distribution.
18423SN/A *
19423SN/A * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20423SN/A * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21423SN/A * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22423SN/A * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23423SN/A * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24423SN/A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25423SN/A * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26423SN/A * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27423SN/A * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28423SN/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29423SN/A * POSSIBILITY OF SUCH DAMAGE.
301120Schegar */
311120Schegar#include <sys/cdefs.h>
321120Schegar__RCSID("$NetBSD: t_detach.c,v 1.1 2011/03/24 13:52:04 jruoho Exp $");
33837SN/A
34837SN/A#include <pthread.h>
35423SN/A#include <errno.h>
36423SN/A
371448Serikj#include <atf-c.h>
381735Sbobv
391735Sbobv#include "h_common.h"
401735Sbobv
411735Sbobvstatic void	*func(void *);
421735Sbobv
431735Sbobvstatic void *
441735Sbobvfunc(void *arg)
451735Sbobv{
461735Sbobv	return NULL;
471735Sbobv}
481735Sbobv
491735SbobvATF_TC(pthread_detach);
501735SbobvATF_TC_HEAD(pthread_detach, tc)
511735Sbobv{
521735Sbobv	atf_tc_set_md_var(tc, "descr", "A test of pthread_detach(3)");
531735Sbobv}
541735Sbobv
551735SbobvATF_TC_BODY(pthread_detach, tc)
561735Sbobv{
571735Sbobv	const int state = PTHREAD_CREATE_JOINABLE;
581735Sbobv	pthread_attr_t attr;
591735Sbobv	pthread_t t;
601735Sbobv	int rv;
611735Sbobv
621735Sbobv	/*
631448Serikj	 * Create a joinable thread.
641448Serikj	 */
651448Serikj	PTHREAD_REQUIRE(pthread_attr_init(&attr));
661448Serikj	PTHREAD_REQUIRE(pthread_attr_setdetachstate(&attr, state));
671448Serikj	PTHREAD_REQUIRE(pthread_create(&t, &attr, func, NULL));
681448Serikj
691448Serikj	/*
701448Serikj	 * Detach the thread and try to
711448Serikj	 * join it; EINVAL should follow.
721448Serikj	 */
731448Serikj	PTHREAD_REQUIRE(pthread_detach(t));
741448Serikj
751448Serikj	rv = pthread_join(t, NULL);
761448Serikj	ATF_REQUIRE(rv == EINVAL);
771961Salanb
781664Serikj#ifdef __FreeBSD__
791448Serikj	atf_tc_expect_fail("PR # 191906: fails with EINVAL, not ESRCH");
801448Serikj#endif
811448Serikj
821448Serikj	/*
831448Serikj	 * As usual, ESRCH should follow if
841448Serikj	 * we try to detach an invalid thread.
851448Serikj	 */
861448Serikj	rv = pthread_cancel(NULL);
871448Serikj	ATF_REQUIRE(rv == ESRCH);
881448Serikj}
891448Serikj
901448SerikjATF_TP_ADD_TCS(tp)
911448Serikj{
921448Serikj	ATF_TP_ADD_TC(tp, pthread_detach);
931448Serikj
941961Salanb	return atf_no_error();
951664Serikj}
961448Serikj