xref: /freebsd/lib/libthr/thread/thr_join.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 <sys/cdefs.h>
30 #include "namespace.h"
31 #include <errno.h>
32 #include <pthread.h>
33 #include "un-namespace.h"
34 
35 #include "thr_private.h"
36 
37 int	_pthread_peekjoin_np(pthread_t pthread, void **thread_return);
38 int	_pthread_timedjoin_np(pthread_t pthread, void **thread_return,
39 	    const struct timespec *abstime);
40 static int join_common(pthread_t, void **, const struct timespec *, bool peek);
41 
42 __weak_reference(_thr_join, pthread_join);
43 __weak_reference(_thr_join, _pthread_join);
44 __weak_reference(_pthread_timedjoin_np, pthread_timedjoin_np);
45 __weak_reference(_pthread_peekjoin_np, pthread_peekjoin_np);
46 
47 static void backout_join(void *arg)
48 {
49 	struct pthread *pthread = (struct pthread *)arg;
50 	struct pthread *curthread = _get_curthread();
51 
52 	THR_THREAD_LOCK(curthread, pthread);
53 	pthread->joiner = NULL;
54 	THR_THREAD_UNLOCK(curthread, pthread);
55 }
56 
57 int
58 _thr_join(pthread_t pthread, void **thread_return)
59 {
60 	return (join_common(pthread, thread_return, NULL, false));
61 }
62 
63 int
64 _pthread_timedjoin_np(pthread_t pthread, void **thread_return,
65 	const struct timespec *abstime)
66 {
67 	if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
68 	    abstime->tv_nsec >= 1000000000)
69 		return (EINVAL);
70 
71 	return (join_common(pthread, thread_return, abstime, false));
72 }
73 
74 int
75 _pthread_peekjoin_np(pthread_t pthread, void **thread_return)
76 {
77 	return (join_common(pthread, thread_return, NULL, true));
78 }
79 
80 /*
81  * Cancellation behavior:
82  *   if the thread is canceled, joinee is not recycled.
83  */
84 static int
85 join_common(pthread_t pthread, void **thread_return,
86     const struct timespec *abstime, bool peek)
87 {
88 	struct pthread *curthread = _get_curthread();
89 	struct timespec ts, ts2, *tsp;
90 	void *tmp;
91 	long tid;
92 	int ret;
93 
94 	if (pthread == NULL)
95 		return (EINVAL);
96 
97 	if (pthread == curthread)
98 		return (EDEADLK);
99 
100 	if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0)
101 		return (ESRCH);
102 
103 	if ((pthread->flags & THR_FLAGS_DETACHED) != 0) {
104 		ret = EINVAL;
105 	} else if (pthread->joiner != NULL) {
106 		/* Multiple joiners are not supported. */
107 		ret = ENOTSUP;
108 	}
109 	if (ret != 0) {
110 		THR_THREAD_UNLOCK(curthread, pthread);
111 		return (ret);
112 	}
113 
114 	/* Only peek into status, do not gc the thread. */
115 	if (peek) {
116 		if (pthread->tid != TID_TERMINATED)
117 			ret = EBUSY;
118 		else if (thread_return != NULL)
119 			*thread_return = pthread->ret;
120 		THR_THREAD_UNLOCK(curthread, pthread);
121 		return (ret);
122 	}
123 
124 	/* Set the running thread to be the joiner: */
125 	pthread->joiner = curthread;
126 
127 	THR_THREAD_UNLOCK(curthread, pthread);
128 
129 	THR_CLEANUP_PUSH(curthread, backout_join, pthread);
130 	_thr_cancel_enter(curthread);
131 
132 	tid = pthread->tid;
133 	while (pthread->tid != TID_TERMINATED) {
134 		_thr_testcancel(curthread);
135 		if (abstime != NULL) {
136 			clock_gettime(CLOCK_REALTIME, &ts);
137 			TIMESPEC_SUB(&ts2, abstime, &ts);
138 			if (ts2.tv_sec < 0) {
139 				ret = ETIMEDOUT;
140 				break;
141 			}
142 			tsp = &ts2;
143 		} else
144 			tsp = NULL;
145 		ret = _thr_umtx_wait(&pthread->tid, tid, tsp);
146 		if (ret == ETIMEDOUT)
147 			break;
148 	}
149 
150 	_thr_cancel_leave(curthread, 0);
151 	THR_CLEANUP_POP(curthread, 0);
152 
153 	if (ret == ETIMEDOUT) {
154 		THR_THREAD_LOCK(curthread, pthread);
155 		pthread->joiner = NULL;
156 		THR_THREAD_UNLOCK(curthread, pthread);
157 	} else {
158 		ret = 0;
159 		tmp = pthread->ret;
160 		THR_THREAD_LOCK(curthread, pthread);
161 		pthread->flags |= THR_FLAGS_DETACHED;
162 		pthread->joiner = NULL;
163 		_thr_try_gc(curthread, pthread); /* thread lock released */
164 
165 		if (thread_return != NULL)
166 			*thread_return = tmp;
167 	}
168 	return (ret);
169 }
170