]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkse/thread/thr_detach.c
Set the tcb (thread control block) in the child process after a fork.
[FreeBSD/FreeBSD.git] / lib / libkse / thread / thr_detach.c
1 /*
2  * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include "namespace.h"
32 #include <sys/types.h>
33 #include <machine/atomic.h>
34 #include <errno.h>
35 #include <pthread.h>
36 #include "un-namespace.h"
37 #include "thr_private.h"
38
39 LT10_COMPAT_PRIVATE(_pthread_detach);
40 LT10_COMPAT_DEFAULT(pthread_detach);
41
42 __weak_reference(_pthread_detach, pthread_detach);
43
44 int
45 _pthread_detach(pthread_t pthread)
46 {
47         struct pthread *curthread = _get_curthread();
48         struct kse_mailbox *kmbx = NULL;
49         struct pthread *joiner;
50         int rval = 0;
51
52         /* Check for invalid calling parameters: */
53         if (pthread == NULL || pthread->magic != THR_MAGIC)
54                 /* Return an invalid argument error: */
55                 rval = EINVAL;
56
57         else if ((rval = _thr_ref_add(curthread, pthread,
58             /*include dead*/1)) != 0) {
59                 /* Return an error: */
60         }
61
62         /* Check if the thread is already detached: */
63         else if ((pthread->attr.flags & PTHREAD_DETACHED) != 0) {
64                 /* Return an error: */
65                 _thr_ref_delete(curthread, pthread);
66                 rval = EINVAL;
67         } else {
68                 /* Lock the detached thread: */
69                 THR_SCHED_LOCK(curthread, pthread);
70
71                 /* Flag the thread as detached: */
72                 pthread->attr.flags |= PTHREAD_DETACHED;
73
74                 /* Retrieve any joining thread and remove it: */
75                 joiner = pthread->joiner;
76                 if ((joiner != NULL) && (joiner->kseg == pthread->kseg)) {
77                         /*
78                          * We already own the scheduler lock for the joiner.
79                          * Take advantage of that and make the joiner runnable.
80                          */
81                         if (joiner->join_status.thread == pthread) {
82                                 /*
83                                  * Set the return value for the woken thread:
84                                  */
85                                 joiner->join_status.error = ESRCH;
86                                 joiner->join_status.ret = NULL;
87                                 joiner->join_status.thread = NULL;
88
89                                 kmbx = _thr_setrunnable_unlocked(joiner);
90                         }
91                         joiner = NULL;
92                 }
93                 THR_SCHED_UNLOCK(curthread, pthread);
94                 /* See if there is a thread waiting in pthread_join(): */
95                 if ((joiner != NULL) &&
96                     (_thr_ref_add(curthread, joiner, 0) == 0)) {
97                         /* Lock the joiner before fiddling with it. */
98                         THR_SCHED_LOCK(curthread, joiner);
99                         if (joiner->join_status.thread == pthread) {
100                                 /*
101                                  * Set the return value for the woken thread:
102                                  */
103                                 joiner->join_status.error = ESRCH;
104                                 joiner->join_status.ret = NULL;
105                                 joiner->join_status.thread = NULL;
106
107                                 kmbx = _thr_setrunnable_unlocked(joiner);
108                         }
109                         THR_SCHED_UNLOCK(curthread, joiner);
110                         _thr_ref_delete(curthread, joiner);
111                 }
112                 _thr_ref_delete(curthread, pthread);
113                 if (kmbx != NULL)
114                         kse_wakeup(kmbx);
115         }
116
117         /* Return the completion status: */
118         return (rval);
119 }