157718be8SEnji Cooper /* $NetBSD: t_detach.c,v 1.1 2011/03/24 13:52:04 jruoho Exp $ */ 257718be8SEnji Cooper 357718be8SEnji Cooper /*- 457718be8SEnji Cooper * Copyright (c) 2011 The NetBSD Foundation, Inc. 557718be8SEnji Cooper * All rights reserved. 657718be8SEnji Cooper * 757718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation 857718be8SEnji Cooper * by Jukka Ruohonen. 957718be8SEnji Cooper * 1057718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without 1157718be8SEnji Cooper * modification, are permitted provided that the following conditions 1257718be8SEnji Cooper * are met: 1357718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright 1457718be8SEnji Cooper * notice, this list of conditions and the following disclaimer. 1557718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright 1657718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the 1757718be8SEnji Cooper * documentation and/or other materials provided with the distribution. 1857718be8SEnji Cooper * 1957718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2057718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2157718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2257718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2357718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2457718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2557718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2657718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2757718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2857718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2957718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE. 3057718be8SEnji Cooper */ 3157718be8SEnji Cooper #include <sys/cdefs.h> 3257718be8SEnji Cooper __RCSID("$NetBSD: t_detach.c,v 1.1 2011/03/24 13:52:04 jruoho Exp $"); 3357718be8SEnji Cooper 3457718be8SEnji Cooper #include <pthread.h> 3557718be8SEnji Cooper #include <errno.h> 3657718be8SEnji Cooper 3757718be8SEnji Cooper #include <atf-c.h> 3857718be8SEnji Cooper 3957718be8SEnji Cooper #include "h_common.h" 4057718be8SEnji Cooper 41*0c76184cSEnji Cooper #ifdef __FreeBSD__ 42*0c76184cSEnji Cooper #include <time.h> 43*0c76184cSEnji Cooper #endif 44*0c76184cSEnji Cooper 4557718be8SEnji Cooper static void *func(void *); 4657718be8SEnji Cooper 4757718be8SEnji Cooper static void * 4857718be8SEnji Cooper func(void *arg) 4957718be8SEnji Cooper { 50*0c76184cSEnji Cooper #ifdef __FreeBSD__ 51*0c76184cSEnji Cooper sleep(2); 52*0c76184cSEnji Cooper #endif 5357718be8SEnji Cooper return NULL; 5457718be8SEnji Cooper } 5557718be8SEnji Cooper 5657718be8SEnji Cooper ATF_TC(pthread_detach); 5757718be8SEnji Cooper ATF_TC_HEAD(pthread_detach, tc) 5857718be8SEnji Cooper { 5957718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "A test of pthread_detach(3)"); 6057718be8SEnji Cooper } 6157718be8SEnji Cooper 6257718be8SEnji Cooper ATF_TC_BODY(pthread_detach, tc) 6357718be8SEnji Cooper { 6457718be8SEnji Cooper const int state = PTHREAD_CREATE_JOINABLE; 6557718be8SEnji Cooper pthread_attr_t attr; 6657718be8SEnji Cooper pthread_t t; 6757718be8SEnji Cooper int rv; 6857718be8SEnji Cooper 6957718be8SEnji Cooper /* 7057718be8SEnji Cooper * Create a joinable thread. 7157718be8SEnji Cooper */ 7257718be8SEnji Cooper PTHREAD_REQUIRE(pthread_attr_init(&attr)); 7357718be8SEnji Cooper PTHREAD_REQUIRE(pthread_attr_setdetachstate(&attr, state)); 7457718be8SEnji Cooper PTHREAD_REQUIRE(pthread_create(&t, &attr, func, NULL)); 7557718be8SEnji Cooper 7657718be8SEnji Cooper /* 7757718be8SEnji Cooper * Detach the thread and try to 7857718be8SEnji Cooper * join it; EINVAL should follow. 7957718be8SEnji Cooper */ 8057718be8SEnji Cooper PTHREAD_REQUIRE(pthread_detach(t)); 8157718be8SEnji Cooper 82*0c76184cSEnji Cooper #ifdef __FreeBSD__ 83*0c76184cSEnji Cooper sleep(1); 84*0c76184cSEnji Cooper #endif 8557718be8SEnji Cooper rv = pthread_join(t, NULL); 8657718be8SEnji Cooper ATF_REQUIRE(rv == EINVAL); 8757718be8SEnji Cooper 88ff0ba872SEnji Cooper #ifdef __FreeBSD__ 89*0c76184cSEnji Cooper sleep(3); 90ff9d081dSEnji Cooper #endif 91ff9d081dSEnji Cooper 9257718be8SEnji Cooper /* 9357718be8SEnji Cooper * As usual, ESRCH should follow if 9457718be8SEnji Cooper * we try to detach an invalid thread. 9557718be8SEnji Cooper */ 96*0c76184cSEnji Cooper #ifdef __NetBSD__ 9757718be8SEnji Cooper rv = pthread_cancel(NULL); 98*0c76184cSEnji Cooper #else 99*0c76184cSEnji Cooper rv = pthread_cancel(t); 100*0c76184cSEnji Cooper #endif 10157718be8SEnji Cooper ATF_REQUIRE(rv == ESRCH); 10257718be8SEnji Cooper } 10357718be8SEnji Cooper 10457718be8SEnji Cooper ATF_TP_ADD_TCS(tp) 10557718be8SEnji Cooper { 10657718be8SEnji Cooper ATF_TP_ADD_TC(tp, pthread_detach); 10757718be8SEnji Cooper 10857718be8SEnji Cooper return atf_no_error(); 10957718be8SEnji Cooper } 110