xref: /freebsd/lib/libthr/thread/thr_join.c (revision 123af6ec70016f5556da5972d4d63c7d175c06d3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 
32 #include "namespace.h"
33 #include <errno.h>
34 #include <pthread.h>
35 #include "un-namespace.h"
36 
37 #include "thr_private.h"
38 
39 int	_pthread_timedjoin_np(pthread_t pthread, void **thread_return,
40 	const struct timespec *abstime);
41 static int join_common(pthread_t, void **, const struct timespec *);
42 
43 __weak_reference(_pthread_join, pthread_join);
44 __weak_reference(_pthread_timedjoin_np, pthread_timedjoin_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 _pthread_join(pthread_t pthread, void **thread_return)
58 {
59 	return (join_common(pthread, thread_return, NULL));
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));
71 }
72 
73 /*
74  * Cancellation behavior:
75  *   if the thread is canceled, joinee is not recycled.
76  */
77 static int
78 join_common(pthread_t pthread, void **thread_return,
79 	const struct timespec *abstime)
80 {
81 	struct pthread *curthread = _get_curthread();
82 	struct timespec ts, ts2, *tsp;
83 	void *tmp;
84 	long tid;
85 	int ret = 0;
86 
87 	if (pthread == NULL)
88 		return (EINVAL);
89 
90 	if (pthread == curthread)
91 		return (EDEADLK);
92 
93 	if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0)
94 		return (ESRCH);
95 
96 	if ((pthread->flags & THR_FLAGS_DETACHED) != 0) {
97 		ret = EINVAL;
98 	} else if (pthread->joiner != NULL) {
99 		/* Multiple joiners are not supported. */
100 		ret = ENOTSUP;
101 	}
102 	if (ret) {
103 		THR_THREAD_UNLOCK(curthread, pthread);
104 		return (ret);
105 	}
106 	/* Set the running thread to be the joiner: */
107 	pthread->joiner = curthread;
108 
109 	THR_THREAD_UNLOCK(curthread, pthread);
110 
111 	THR_CLEANUP_PUSH(curthread, backout_join, pthread);
112 	_thr_cancel_enter(curthread);
113 
114 	tid = pthread->tid;
115 	while (pthread->tid != TID_TERMINATED) {
116 		_thr_testcancel(curthread);
117 		if (abstime != NULL) {
118 			clock_gettime(CLOCK_REALTIME, &ts);
119 			TIMESPEC_SUB(&ts2, abstime, &ts);
120 			if (ts2.tv_sec < 0) {
121 				ret = ETIMEDOUT;
122 				break;
123 			}
124 			tsp = &ts2;
125 		} else
126 			tsp = NULL;
127 		ret = _thr_umtx_wait(&pthread->tid, tid, tsp);
128 		if (ret == ETIMEDOUT)
129 			break;
130 	}
131 
132 	_thr_cancel_leave(curthread, 0);
133 	THR_CLEANUP_POP(curthread, 0);
134 
135 	if (ret == ETIMEDOUT) {
136 		THR_THREAD_LOCK(curthread, pthread);
137 		pthread->joiner = NULL;
138 		THR_THREAD_UNLOCK(curthread, pthread);
139 	} else {
140 		ret = 0;
141 		tmp = pthread->ret;
142 		THR_THREAD_LOCK(curthread, pthread);
143 		pthread->flags |= THR_FLAGS_DETACHED;
144 		pthread->joiner = NULL;
145 		_thr_try_gc(curthread, pthread); /* thread lock released */
146 
147 		if (thread_return != NULL)
148 			*thread_return = tmp;
149 	}
150 	return (ret);
151 }
152