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 33 #include "namespace.h" 34 #include <machine/tls.h> 35 36 #include <errno.h> 37 #include <pthread.h> 38 #include "un-namespace.h" 39 40 #include "thr_private.h" 41 42 int _pthread_timedjoin_np(pthread_t pthread, void **thread_return, 43 const struct timespec *abstime); 44 45 static int join_common(pthread_t, void **, const struct timespec *); 46 47 static void backout_join(void *arg) 48 { 49 struct pthread *curthread = tls_get_curthread(); 50 struct pthread *pthread = (struct pthread *)arg; 51 52 THREAD_LIST_LOCK(curthread); 53 pthread->joiner = NULL; 54 THREAD_LIST_UNLOCK(curthread); 55 } 56 57 int 58 _pthread_join(pthread_t pthread, void **thread_return) 59 { 60 return (join_common(pthread, thread_return, NULL)); 61 } 62 63 int 64 _pthread_timedjoin_np(pthread_t pthread, void **thread_return, 65 const struct timespec *abstime) 66 { 67 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 || 68 abstime->tv_nsec >= 1000000000) 69 return (EINVAL); 70 71 return (join_common(pthread, thread_return, abstime)); 72 } 73 74 static int 75 join_common(pthread_t pthread, void **thread_return, 76 const struct timespec *abstime) 77 { 78 struct pthread *curthread = tls_get_curthread(); 79 struct timespec ts, ts2, *tsp; 80 void *tmp; 81 long state; 82 int oldcancel; 83 int ret = 0; 84 85 if (pthread == NULL) 86 return (EINVAL); 87 88 if (pthread == curthread) 89 return (EDEADLK); 90 91 THREAD_LIST_LOCK(curthread); 92 if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0) { 93 ret = ESRCH; 94 } else if ((pthread->tlflags & TLFLAGS_DETACHED) != 0) { 95 ret = EINVAL; 96 } else if (pthread->joiner != NULL) { 97 /* Multiple joiners are not supported. */ 98 ret = ENOTSUP; 99 } 100 if (ret) { 101 THREAD_LIST_UNLOCK(curthread); 102 return (ret); 103 } 104 /* Set the running thread to be the joiner: */ 105 pthread->joiner = curthread; 106 107 THREAD_LIST_UNLOCK(curthread); 108 109 THR_CLEANUP_PUSH(curthread, backout_join, pthread); 110 oldcancel = _thr_cancel_enter(curthread); 111 112 while ((state = pthread->state) != PS_DEAD) { 113 if (abstime != NULL) { 114 clock_gettime(CLOCK_REALTIME, &ts); 115 TIMESPEC_SUB(&ts2, abstime, &ts); 116 if (ts2.tv_sec < 0) { 117 ret = ETIMEDOUT; 118 break; 119 } 120 tsp = &ts2; 121 } else 122 tsp = NULL; 123 ret = _thr_umtx_wait(&pthread->state, state, tsp, 124 CLOCK_REALTIME); 125 if (ret == ETIMEDOUT) 126 break; 127 } 128 129 _thr_cancel_leave(curthread, oldcancel); 130 THR_CLEANUP_POP(curthread, 0); 131 132 if (ret == ETIMEDOUT) { 133 THREAD_LIST_LOCK(curthread); 134 pthread->joiner = NULL; 135 THREAD_LIST_UNLOCK(curthread); 136 } else { 137 ret = 0; 138 tmp = pthread->ret; 139 THREAD_LIST_LOCK(curthread); 140 pthread->tlflags |= TLFLAGS_DETACHED; 141 pthread->joiner = NULL; 142 THR_GCLIST_ADD(pthread); 143 THREAD_LIST_UNLOCK(curthread); 144 145 if (thread_return != NULL) 146 *thread_return = tmp; 147 } 148 return (ret); 149 } 150 151 __strong_reference(_pthread_join, pthread_join); 152 __strong_reference(_pthread_timedjoin_np, pthread_timedjoin_np); 153 154