2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "namespace.h"
33 #include "un-namespace.h"
35 #include "thr_private.h"
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 *);
41 __weak_reference(_pthread_join, pthread_join);
42 __weak_reference(_pthread_timedjoin_np, pthread_timedjoin_np);
44 static void backout_join(void *arg)
46 struct pthread *pthread = (struct pthread *)arg;
47 struct pthread *curthread = _get_curthread();
49 THR_THREAD_LOCK(curthread, pthread);
50 pthread->joiner = NULL;
51 THR_THREAD_UNLOCK(curthread, pthread);
55 _pthread_join(pthread_t pthread, void **thread_return)
57 return (join_common(pthread, thread_return, NULL));
61 _pthread_timedjoin_np(pthread_t pthread, void **thread_return,
62 const struct timespec *abstime)
64 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
65 abstime->tv_nsec >= 1000000000)
68 return (join_common(pthread, thread_return, abstime));
72 * Cancellation behavior:
73 * if the thread is canceled, joinee is not recycled.
76 join_common(pthread_t pthread, void **thread_return,
77 const struct timespec *abstime)
79 struct pthread *curthread = _get_curthread();
80 struct timespec ts, ts2, *tsp;
88 if (pthread == curthread)
91 if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0)
94 if ((pthread->flags & THR_FLAGS_DETACHED) != 0) {
96 } else if (pthread->joiner != NULL) {
97 /* Multiple joiners are not supported. */
101 THR_THREAD_UNLOCK(curthread, pthread);
104 /* Set the running thread to be the joiner: */
105 pthread->joiner = curthread;
107 THR_THREAD_UNLOCK(curthread, pthread);
109 THR_CLEANUP_PUSH(curthread, backout_join, pthread);
110 _thr_cancel_enter(curthread);
113 while (pthread->tid != TID_TERMINATED) {
114 _thr_testcancel(curthread);
115 if (abstime != NULL) {
116 clock_gettime(CLOCK_REALTIME, &ts);
117 TIMESPEC_SUB(&ts2, abstime, &ts);
118 if (ts2.tv_sec < 0) {
125 ret = _thr_umtx_wait(&pthread->tid, tid, tsp);
126 if (ret == ETIMEDOUT)
130 _thr_cancel_leave(curthread, 0);
131 THR_CLEANUP_POP(curthread, 0);
133 if (ret == ETIMEDOUT) {
134 THR_THREAD_LOCK(curthread, pthread);
135 pthread->joiner = NULL;
136 THR_THREAD_UNLOCK(curthread, pthread);
140 THR_THREAD_LOCK(curthread, pthread);
141 pthread->flags |= THR_FLAGS_DETACHED;
142 pthread->joiner = NULL;
143 _thr_try_gc(curthread, pthread); /* thread lock released */
145 if (thread_return != NULL)
146 *thread_return = tmp;