xref: /freebsd/lib/libthr/thread/thr_join.c (revision 9dba3024c3f1a2df6f42689aac5a2ab4acc7561d)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by John Birrell.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 
35 #include <errno.h>
36 #include <pthread.h>
37 
38 #include "thr_private.h"
39 
40 __weak_reference(_pthread_join, pthread_join);
41 
42 static void backout_join(void *arg)
43 {
44 	struct pthread *curthread = _get_curthread();
45 	struct pthread *pthread = (struct pthread *)arg;
46 
47 	THREAD_LIST_LOCK(curthread);
48 	pthread->joiner = NULL;
49 	THREAD_LIST_UNLOCK(curthread);
50 }
51 
52 int
53 _pthread_join(pthread_t pthread, void **thread_return)
54 {
55 	struct pthread *curthread = _get_curthread();
56 	void *tmp;
57 	long state;
58 	int oldcancel;
59 	int ret = 0;
60 
61 	if (pthread == NULL)
62 		return (EINVAL);
63 
64 	if (pthread == curthread)
65 		return (EDEADLK);
66 
67 	THREAD_LIST_LOCK(curthread);
68 	if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0) {
69 		ret = ESRCH;
70 	} else if ((pthread->tlflags & TLFLAGS_DETACHED) != 0) {
71 		ret = ESRCH;
72 	} else if (pthread->joiner != NULL) {
73 		/* Multiple joiners are not supported. */
74 		ret = ENOTSUP;
75 	}
76 	if (ret) {
77 		THREAD_LIST_UNLOCK(curthread);
78 		return (ret);
79 	}
80 	/* Set the running thread to be the joiner: */
81 	pthread->joiner = curthread;
82 
83 	THREAD_LIST_UNLOCK(curthread);
84 
85 	THR_CLEANUP_PUSH(curthread, backout_join, pthread);
86 	oldcancel = _thr_cancel_enter(curthread);
87 
88 	while ((state = pthread->state) != PS_DEAD) {
89 		_thr_umtx_wait(&pthread->state, state, NULL);
90 	}
91 
92 	_thr_cancel_leave(curthread, oldcancel);
93 	THR_CLEANUP_POP(curthread, 0);
94 
95 	tmp = pthread->ret;
96 	THREAD_LIST_LOCK(curthread);
97 	pthread->tlflags |= TLFLAGS_DETACHED;
98 	THR_GCLIST_ADD(pthread);
99 	THREAD_LIST_UNLOCK(curthread);
100 
101 	if (thread_return != NULL)
102 		*thread_return = tmp;
103 
104 	return (ret);
105 }
106