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_tryjoin_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 bool try); 42 43 __weak_reference(_thr_join, pthread_join); 44 __weak_reference(_thr_join, _pthread_join); 45 __weak_reference(_pthread_timedjoin_np, pthread_timedjoin_np); 46 __weak_reference(_pthread_peekjoin_np, pthread_peekjoin_np); 47 __weak_reference(_pthread_tryjoin_np, pthread_tryjoin_np); 48 49 static void 50 backout_join(struct pthread *pthread, struct pthread *curthread) 51 { 52 THR_THREAD_LOCK(curthread, pthread); 53 pthread->joiner = NULL; 54 THR_THREAD_UNLOCK(curthread, pthread); 55 } 56 57 static void 58 backout_join_pop(void *arg) 59 { 60 struct pthread *pthread = (struct pthread *)arg; 61 struct pthread *curthread = _get_curthread(); 62 63 backout_join(pthread, curthread); 64 } 65 66 int 67 _thr_join(pthread_t pthread, void **thread_return) 68 { 69 return (join_common(pthread, thread_return, NULL, false, false)); 70 } 71 72 int 73 _pthread_timedjoin_np(pthread_t pthread, void **thread_return, 74 const struct timespec *abstime) 75 { 76 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 || 77 abstime->tv_nsec >= 1000000000) 78 return (EINVAL); 79 80 return (join_common(pthread, thread_return, abstime, false, false)); 81 } 82 83 int 84 _pthread_peekjoin_np(pthread_t pthread, void **thread_return) 85 { 86 return (join_common(pthread, thread_return, NULL, true, false)); 87 } 88 89 int 90 _pthread_tryjoin_np(pthread_t pthread, void **thread_return) 91 { 92 return (join_common(pthread, thread_return, NULL, false, true)); 93 } 94 95 static void 96 join_common_joined(struct pthread *pthread, struct pthread *curthread, 97 void **thread_return) 98 { 99 void *tmp; 100 101 tmp = pthread->ret; 102 pthread->flags |= THR_FLAGS_DETACHED; 103 pthread->joiner = NULL; 104 _thr_try_gc(curthread, pthread); /* thread lock released */ 105 106 if (thread_return != NULL) 107 *thread_return = tmp; 108 } 109 110 /* 111 * Cancellation behavior: 112 * if the thread is canceled, joinee is not recycled. 113 */ 114 static int 115 join_common(pthread_t pthread, void **thread_return, 116 const struct timespec *abstime, bool peek, bool try) 117 { 118 struct pthread *curthread = _get_curthread(); 119 struct timespec ts, ts2, *tsp; 120 long tid; 121 int ret; 122 123 if (pthread == NULL) 124 return (EINVAL); 125 126 if (pthread == curthread) 127 return (EDEADLK); 128 129 if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0) 130 return (ESRCH); 131 132 if ((pthread->flags & THR_FLAGS_DETACHED) != 0) { 133 ret = EINVAL; 134 } else if (pthread->joiner != NULL) { 135 /* Multiple joiners are not supported. */ 136 ret = ENOTSUP; 137 } 138 if (ret != 0) { 139 THR_THREAD_UNLOCK(curthread, pthread); 140 return (ret); 141 } 142 143 /* Only peek into status, do not gc the thread. */ 144 if (peek) { 145 if (pthread->tid != TID_TERMINATED) 146 ret = EBUSY; 147 else if (thread_return != NULL) 148 *thread_return = pthread->ret; 149 THR_THREAD_UNLOCK(curthread, pthread); 150 return (ret); 151 } 152 153 /* Only try to join. */ 154 if (try) { 155 if (pthread->tid != TID_TERMINATED) { 156 THR_THREAD_UNLOCK(curthread, pthread); 157 return (EBUSY); 158 } 159 join_common_joined(pthread, curthread, thread_return); 160 return (0); 161 } 162 163 /* Set the running thread to be the joiner: */ 164 pthread->joiner = curthread; 165 166 THR_THREAD_UNLOCK(curthread, pthread); 167 168 THR_CLEANUP_PUSH(curthread, backout_join_pop, pthread); 169 _thr_cancel_enter(curthread); 170 171 tid = pthread->tid; 172 while (pthread->tid != TID_TERMINATED) { 173 _thr_testcancel(curthread); 174 if (abstime != NULL) { 175 clock_gettime(CLOCK_REALTIME, &ts); 176 TIMESPEC_SUB(&ts2, abstime, &ts); 177 if (ts2.tv_sec < 0) { 178 ret = ETIMEDOUT; 179 break; 180 } 181 tsp = &ts2; 182 } else 183 tsp = NULL; 184 ret = _thr_umtx_wait(&pthread->tid, tid, tsp); 185 if (ret == ETIMEDOUT) 186 break; 187 } 188 189 _thr_cancel_leave(curthread, 0); 190 THR_CLEANUP_POP(curthread, 0); 191 192 if (ret == ETIMEDOUT || ret == EBUSY) { 193 backout_join(pthread, curthread); 194 } else { 195 ret = 0; 196 THR_THREAD_LOCK(curthread, pthread); 197 join_common_joined(pthread, curthread, thread_return); 198 } 199 return (ret); 200 } 201