xref: /freebsd/lib/libthr/thread/thr_join.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "namespace.h"
30 #include <errno.h>
31 #include <pthread.h>
32 #include "un-namespace.h"
33 
34 #include "thr_private.h"
35 
36 int	_pthread_peekjoin_np(pthread_t pthread, void **thread_return);
37 int	_pthread_timedjoin_np(pthread_t pthread, void **thread_return,
38 	    const struct timespec *abstime);
39 static int join_common(pthread_t, void **, const struct timespec *, bool peek);
40 
41 __weak_reference(_thr_join, pthread_join);
42 __weak_reference(_thr_join, _pthread_join);
43 __weak_reference(_pthread_timedjoin_np, pthread_timedjoin_np);
44 __weak_reference(_pthread_peekjoin_np, pthread_peekjoin_np);
45 
46 static void backout_join(void *arg)
47 {
48 	struct pthread *pthread = (struct pthread *)arg;
49 	struct pthread *curthread = _get_curthread();
50 
51 	THR_THREAD_LOCK(curthread, pthread);
52 	pthread->joiner = NULL;
53 	THR_THREAD_UNLOCK(curthread, pthread);
54 }
55 
56 int
57 _thr_join(pthread_t pthread, void **thread_return)
58 {
59 	return (join_common(pthread, thread_return, NULL, false));
60 }
61 
62 int
63 _pthread_timedjoin_np(pthread_t pthread, void **thread_return,
64 	const struct timespec *abstime)
65 {
66 	if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
67 	    abstime->tv_nsec >= 1000000000)
68 		return (EINVAL);
69 
70 	return (join_common(pthread, thread_return, abstime, false));
71 }
72 
73 int
74 _pthread_peekjoin_np(pthread_t pthread, void **thread_return)
75 {
76 	return (join_common(pthread, thread_return, NULL, true));
77 }
78 
79 /*
80  * Cancellation behavior:
81  *   if the thread is canceled, joinee is not recycled.
82  */
83 static int
84 join_common(pthread_t pthread, void **thread_return,
85     const struct timespec *abstime, bool peek)
86 {
87 	struct pthread *curthread = _get_curthread();
88 	struct timespec ts, ts2, *tsp;
89 	void *tmp;
90 	long tid;
91 	int ret;
92 
93 	if (pthread == NULL)
94 		return (EINVAL);
95 
96 	if (pthread == curthread)
97 		return (EDEADLK);
98 
99 	if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0)
100 		return (ESRCH);
101 
102 	if ((pthread->flags & THR_FLAGS_DETACHED) != 0) {
103 		ret = EINVAL;
104 	} else if (pthread->joiner != NULL) {
105 		/* Multiple joiners are not supported. */
106 		ret = ENOTSUP;
107 	}
108 	if (ret != 0) {
109 		THR_THREAD_UNLOCK(curthread, pthread);
110 		return (ret);
111 	}
112 
113 	/* Only peek into status, do not gc the thread. */
114 	if (peek) {
115 		if (pthread->tid != TID_TERMINATED)
116 			ret = EBUSY;
117 		else if (thread_return != NULL)
118 			*thread_return = pthread->ret;
119 		THR_THREAD_UNLOCK(curthread, pthread);
120 		return (ret);
121 	}
122 
123 	/* Set the running thread to be the joiner: */
124 	pthread->joiner = curthread;
125 
126 	THR_THREAD_UNLOCK(curthread, pthread);
127 
128 	THR_CLEANUP_PUSH(curthread, backout_join, pthread);
129 	_thr_cancel_enter(curthread);
130 
131 	tid = pthread->tid;
132 	while (pthread->tid != TID_TERMINATED) {
133 		_thr_testcancel(curthread);
134 		if (abstime != NULL) {
135 			clock_gettime(CLOCK_REALTIME, &ts);
136 			TIMESPEC_SUB(&ts2, abstime, &ts);
137 			if (ts2.tv_sec < 0) {
138 				ret = ETIMEDOUT;
139 				break;
140 			}
141 			tsp = &ts2;
142 		} else
143 			tsp = NULL;
144 		ret = _thr_umtx_wait(&pthread->tid, tid, tsp);
145 		if (ret == ETIMEDOUT)
146 			break;
147 	}
148 
149 	_thr_cancel_leave(curthread, 0);
150 	THR_CLEANUP_POP(curthread, 0);
151 
152 	if (ret == ETIMEDOUT) {
153 		THR_THREAD_LOCK(curthread, pthread);
154 		pthread->joiner = NULL;
155 		THR_THREAD_UNLOCK(curthread, pthread);
156 	} else {
157 		ret = 0;
158 		tmp = pthread->ret;
159 		THR_THREAD_LOCK(curthread, pthread);
160 		pthread->flags |= THR_FLAGS_DETACHED;
161 		pthread->joiner = NULL;
162 		_thr_try_gc(curthread, pthread); /* thread lock released */
163 
164 		if (thread_return != NULL)
165 			*thread_return = tmp;
166 	}
167 	return (ret);
168 }
169