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