1112918Sjeff/*
2153496Sdavidxu * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3153496Sdavidxu * Copyright (C) 2003 Daniel M. Eischen <deischen@freebsd.org>
4112918Sjeff * All rights reserved.
5112918Sjeff *
6112918Sjeff * Redistribution and use in source and binary forms, with or without
7112918Sjeff * modification, are permitted provided that the following conditions
8112918Sjeff * are met:
9112918Sjeff * 1. Redistributions of source code must retain the above copyright
10153496Sdavidxu *    notice unmodified, this list of conditions, and the following
11153496Sdavidxu *    disclaimer.
12112918Sjeff * 2. Redistributions in binary form must reproduce the above copyright
13112918Sjeff *    notice, this list of conditions and the following disclaimer in the
14112918Sjeff *    documentation and/or other materials provided with the distribution.
15112918Sjeff *
16153496Sdavidxu * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17153496Sdavidxu * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18153496Sdavidxu * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19153496Sdavidxu * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20153496Sdavidxu * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21153496Sdavidxu * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22153496Sdavidxu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23153496Sdavidxu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24153496Sdavidxu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25153496Sdavidxu * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26112918Sjeff *
27112918Sjeff * $FreeBSD: releng/10.3/lib/libthr/thread/thr_detach.c 212536 2010-09-13 07:03:01Z davidxu $
28153496Sdavidxu *
29112918Sjeff */
30144518Sdavidxu
31157457Sdavidxu#include "namespace.h"
32144518Sdavidxu#include <sys/types.h>
33112918Sjeff#include <errno.h>
34112918Sjeff#include <pthread.h>
35157457Sdavidxu#include "un-namespace.h"
36144518Sdavidxu
37112918Sjeff#include "thr_private.h"
38112918Sjeff
39112918Sjeff__weak_reference(_pthread_detach, pthread_detach);
40112918Sjeff
41112918Sjeffint
42112918Sjeff_pthread_detach(pthread_t pthread)
43112918Sjeff{
44144518Sdavidxu	struct pthread *curthread = _get_curthread();
45144518Sdavidxu	int rval;
46129484Smtm
47144518Sdavidxu	if (pthread == NULL)
48112918Sjeff		return (EINVAL);
49112918Sjeff
50144518Sdavidxu	if ((rval = _thr_find_thread(curthread, pthread,
51144518Sdavidxu			/*include dead*/1)) != 0) {
52144518Sdavidxu		return (rval);
53115693Smtm	}
54112918Sjeff
55144518Sdavidxu	/* Check if the thread is already detached or has a joiner. */
56212536Sdavidxu	if ((pthread->flags & THR_FLAGS_DETACHED) != 0 ||
57144518Sdavidxu	    (pthread->joiner != NULL)) {
58212536Sdavidxu		THR_THREAD_UNLOCK(curthread, pthread);
59144518Sdavidxu		return (EINVAL);
60112918Sjeff	}
61112918Sjeff
62144518Sdavidxu	/* Flag the thread as detached. */
63212536Sdavidxu	pthread->flags |= THR_FLAGS_DETACHED;
64212536Sdavidxu	_thr_try_gc(curthread, pthread); /* thread lock released */
65112918Sjeff
66112918Sjeff	return (0);
67112918Sjeff}
68