1 /* 2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org> 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 unmodified, this list of conditions, and the following 10 * disclaimer. 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. 14 * 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. 25 * 26 * $FreeBSD$ 27 * 28 */ 29 30 #include <errno.h> 31 #include <pthread.h> 32 33 #include "thr_private.h" 34 35 static int join_common(pthread_t, void **, const struct timespec *); 36 37 __weak_reference(_pthread_join, pthread_join); 38 __weak_reference(_pthread_timedjoin_np, pthread_timedjoin_np); 39 40 static void backout_join(void *arg) 41 { 42 struct pthread *curthread = _get_curthread(); 43 struct pthread *pthread = (struct pthread *)arg; 44 45 THREAD_LIST_LOCK(curthread); 46 pthread->joiner = NULL; 47 THREAD_LIST_UNLOCK(curthread); 48 } 49 50 int 51 _pthread_join(pthread_t pthread, void **thread_return) 52 { 53 return (join_common(pthread, thread_return, NULL)); 54 } 55 56 int 57 _pthread_timedjoin_np(pthread_t pthread, void **thread_return, 58 const struct timespec *abstime) 59 { 60 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 || 61 abstime->tv_nsec >= 1000000000) 62 return (EINVAL); 63 64 return (join_common(pthread, thread_return, abstime)); 65 } 66 67 static int 68 join_common(pthread_t pthread, void **thread_return, 69 const struct timespec *abstime) 70 { 71 struct pthread *curthread = _get_curthread(); 72 struct timespec ts, ts2, *tsp; 73 void *tmp; 74 long tid; 75 int oldcancel; 76 int ret = 0; 77 78 if (pthread == NULL) 79 return (EINVAL); 80 81 if (pthread == curthread) 82 return (EDEADLK); 83 84 THREAD_LIST_LOCK(curthread); 85 if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0) { 86 ret = ESRCH; 87 } else if ((pthread->tlflags & TLFLAGS_DETACHED) != 0) { 88 ret = ESRCH; 89 } else if (pthread->joiner != NULL) { 90 /* Multiple joiners are not supported. */ 91 ret = ENOTSUP; 92 } 93 if (ret) { 94 THREAD_LIST_UNLOCK(curthread); 95 return (ret); 96 } 97 /* Set the running thread to be the joiner: */ 98 pthread->joiner = curthread; 99 100 THREAD_LIST_UNLOCK(curthread); 101 102 THR_CLEANUP_PUSH(curthread, backout_join, pthread); 103 oldcancel = _thr_cancel_enter(curthread); 104 105 tid = pthread->tid; 106 while (pthread->tid != TID_TERMINATED) { 107 if (abstime != NULL) { 108 clock_gettime(CLOCK_REALTIME, &ts); 109 TIMESPEC_SUB(&ts2, abstime, &ts); 110 if (ts2.tv_sec < 0) { 111 ret = ETIMEDOUT; 112 break; 113 } 114 tsp = &ts2; 115 } else 116 tsp = NULL; 117 ret = _thr_umtx_wait(&pthread->tid, tid, tsp); 118 if (ret == ETIMEDOUT) 119 break; 120 } 121 122 _thr_cancel_leave(curthread, oldcancel); 123 THR_CLEANUP_POP(curthread, 0); 124 125 if (ret == ETIMEDOUT) { 126 THREAD_LIST_LOCK(curthread); 127 pthread->joiner = NULL; 128 THREAD_LIST_UNLOCK(curthread); 129 } else { 130 ret = 0; 131 tmp = pthread->ret; 132 THREAD_LIST_LOCK(curthread); 133 pthread->tlflags |= TLFLAGS_DETACHED; 134 pthread->joiner = NULL; 135 THR_GCLIST_ADD(pthread); 136 THREAD_LIST_UNLOCK(curthread); 137 138 if (thread_return != NULL) 139 *thread_return = tmp; 140 } 141 return (ret); 142 } 143