2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "namespace.h"
35 #include "un-namespace.h"
37 #include "thr_private.h"
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 *);
43 __weak_reference(_pthread_join, pthread_join);
44 __weak_reference(_pthread_timedjoin_np, pthread_timedjoin_np);
46 static void backout_join(void *arg)
48 struct pthread *pthread = (struct pthread *)arg;
49 struct pthread *curthread = _get_curthread();
51 THR_THREAD_LOCK(curthread, pthread);
52 pthread->joiner = NULL;
53 THR_THREAD_UNLOCK(curthread, pthread);
57 _pthread_join(pthread_t pthread, void **thread_return)
59 return (join_common(pthread, thread_return, NULL));
63 _pthread_timedjoin_np(pthread_t pthread, void **thread_return,
64 const struct timespec *abstime)
66 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
67 abstime->tv_nsec >= 1000000000)
70 return (join_common(pthread, thread_return, abstime));
74 * Cancellation behavior:
75 * if the thread is canceled, joinee is not recycled.
78 join_common(pthread_t pthread, void **thread_return,
79 const struct timespec *abstime)
81 struct pthread *curthread = _get_curthread();
82 struct timespec ts, ts2, *tsp;
90 if (pthread == curthread)
93 if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0)
96 if ((pthread->flags & THR_FLAGS_DETACHED) != 0) {
98 } else if (pthread->joiner != NULL) {
99 /* Multiple joiners are not supported. */
103 THR_THREAD_UNLOCK(curthread, pthread);
106 /* Set the running thread to be the joiner: */
107 pthread->joiner = curthread;
109 THR_THREAD_UNLOCK(curthread, pthread);
111 THR_CLEANUP_PUSH(curthread, backout_join, pthread);
112 _thr_cancel_enter(curthread);
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) {
127 ret = _thr_umtx_wait(&pthread->tid, tid, tsp);
128 if (ret == ETIMEDOUT)
132 _thr_cancel_leave(curthread, 0);
133 THR_CLEANUP_POP(curthread, 0);
135 if (ret == ETIMEDOUT) {
136 THR_THREAD_LOCK(curthread, pthread);
137 pthread->joiner = NULL;
138 THR_THREAD_UNLOCK(curthread, pthread);
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 */
147 if (thread_return != NULL)
148 *thread_return = tmp;