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