1 /* 2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by John Birrell. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35 #include <errno.h> 36 #include <pthread.h> 37 38 #include "thr_private.h" 39 40 static int join_common(pthread_t, void **, const struct timespec *); 41 42 __weak_reference(_pthread_join, pthread_join); 43 __weak_reference(_pthread_timedjoin_np, pthread_timedjoin_np); 44 45 static void backout_join(void *arg) 46 { 47 struct pthread *curthread = _get_curthread(); 48 struct pthread *pthread = (struct pthread *)arg; 49 50 THREAD_LIST_LOCK(curthread); 51 pthread->joiner = NULL; 52 THREAD_LIST_UNLOCK(curthread); 53 } 54 55 int 56 _pthread_join(pthread_t pthread, void **thread_return) 57 { 58 return (join_common(pthread, thread_return, NULL)); 59 } 60 61 int 62 _pthread_timedjoin_np(pthread_t pthread, void **thread_return, 63 const struct timespec *abstime) 64 { 65 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 || 66 abstime->tv_nsec >= 1000000000) 67 return (EINVAL); 68 69 return (join_common(pthread, thread_return, abstime)); 70 } 71 72 static int 73 join_common(pthread_t pthread, void **thread_return, 74 const struct timespec *abstime) 75 { 76 struct pthread *curthread = _get_curthread(); 77 struct timespec ts, ts2, *tsp; 78 void *tmp; 79 long tid; 80 int oldcancel; 81 int ret = 0; 82 83 if (pthread == NULL) 84 return (EINVAL); 85 86 if (pthread == curthread) 87 return (EDEADLK); 88 89 THREAD_LIST_LOCK(curthread); 90 if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0) { 91 ret = ESRCH; 92 } else if ((pthread->tlflags & TLFLAGS_DETACHED) != 0) { 93 ret = ESRCH; 94 } else if (pthread->joiner != NULL) { 95 /* Multiple joiners are not supported. */ 96 ret = ENOTSUP; 97 } 98 if (ret) { 99 THREAD_LIST_UNLOCK(curthread); 100 return (ret); 101 } 102 /* Set the running thread to be the joiner: */ 103 pthread->joiner = curthread; 104 105 THREAD_LIST_UNLOCK(curthread); 106 107 THR_CLEANUP_PUSH(curthread, backout_join, pthread); 108 oldcancel = _thr_cancel_enter(curthread); 109 110 tid = pthread->tid; 111 while (pthread->tid != TID_TERMINATED) { 112 if (abstime != NULL) { 113 clock_gettime(CLOCK_REALTIME, &ts); 114 TIMESPEC_SUB(&ts2, abstime, &ts); 115 if (ts2.tv_sec < 0) { 116 ret = ETIMEDOUT; 117 break; 118 } 119 tsp = &ts2; 120 } else 121 tsp = NULL; 122 ret = _thr_umtx_wait(&pthread->tid, tid, tsp); 123 if (ret == ETIMEDOUT) 124 break; 125 } 126 127 _thr_cancel_leave(curthread, oldcancel); 128 THR_CLEANUP_POP(curthread, 0); 129 130 if (ret == ETIMEDOUT) { 131 THREAD_LIST_LOCK(curthread); 132 pthread->joiner = NULL; 133 THREAD_LIST_UNLOCK(curthread); 134 } else { 135 tmp = pthread->ret; 136 THREAD_LIST_LOCK(curthread); 137 pthread->tlflags |= TLFLAGS_DETACHED; 138 pthread->joiner = NULL; 139 THR_GCLIST_ADD(pthread); 140 THREAD_LIST_UNLOCK(curthread); 141 142 if (thread_return != NULL) 143 *thread_return = tmp; 144 } 145 return (ret); 146 } 147