thr_detach.c revision 212536
190075Sobrien/*
290075Sobrien * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
390075Sobrien * Copyright (C) 2003 Daniel M. Eischen <deischen@freebsd.org>
490075Sobrien * All rights reserved.
590075Sobrien *
690075Sobrien * Redistribution and use in source and binary forms, with or without
790075Sobrien * modification, are permitted provided that the following conditions
890075Sobrien * are met:
990075Sobrien * 1. Redistributions of source code must retain the above copyright
1090075Sobrien *    notice unmodified, this list of conditions, and the following
1190075Sobrien *    disclaimer.
1290075Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1390075Sobrien *    notice, this list of conditions and the following disclaimer in the
1490075Sobrien *    documentation and/or other materials provided with the distribution.
1590075Sobrien *
1690075Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1790075Sobrien * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1890075Sobrien * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1990075Sobrien * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2090075Sobrien * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2190075Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2290075Sobrien * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2390075Sobrien * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2490075Sobrien * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2590075Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2690075Sobrien *
2790075Sobrien * $FreeBSD: head/lib/libthr/thread/thr_detach.c 212536 2010-09-13 07:03:01Z davidxu $
2890075Sobrien *
2990075Sobrien */
3090075Sobrien
3190075Sobrien#include "namespace.h"
3290075Sobrien#include <sys/types.h>
3390075Sobrien#include <errno.h>
3490075Sobrien#include <pthread.h>
3590075Sobrien#include "un-namespace.h"
3690075Sobrien
3790075Sobrien#include "thr_private.h"
3890075Sobrien
3990075Sobrien__weak_reference(_pthread_detach, pthread_detach);
4090075Sobrien
4190075Sobrienint
4290075Sobrien_pthread_detach(pthread_t pthread)
4390075Sobrien{
4490075Sobrien	struct pthread *curthread = _get_curthread();
4590075Sobrien	int rval;
4690075Sobrien
4790075Sobrien	if (pthread == NULL)
4890075Sobrien		return (EINVAL);
4990075Sobrien
5090075Sobrien	if ((rval = _thr_find_thread(curthread, pthread,
5190075Sobrien			/*include dead*/1)) != 0) {
5290075Sobrien		return (rval);
5390075Sobrien	}
5490075Sobrien
5590075Sobrien	/* Check if the thread is already detached or has a joiner. */
5690075Sobrien	if ((pthread->flags & THR_FLAGS_DETACHED) != 0 ||
5790075Sobrien	    (pthread->joiner != NULL)) {
5890075Sobrien		THR_THREAD_UNLOCK(curthread, pthread);
5990075Sobrien		return (EINVAL);
6090075Sobrien	}
6190075Sobrien
6290075Sobrien	/* Flag the thread as detached. */
6390075Sobrien	pthread->flags |= THR_FLAGS_DETACHED;
6490075Sobrien	_thr_try_gc(curthread, pthread); /* thread lock released */
6590075Sobrien
6690075Sobrien	return (0);
6790075Sobrien}
6890075Sobrien