xref: /dragonfly/lib/libthread_xu/thread/thr_join.c (revision 940be950819fa932cd401a01f1182bf686a2e61e)
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, 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "namespace.h"
27 #include <machine/tls.h>
28 #include <errno.h>
29 #include <pthread.h>
30 #include "un-namespace.h"
31 
32 #include "thr_private.h"
33 
34 #define cpu_ccfence()       __asm __volatile("" : : : "memory")
35 
36 int _pthread_timedjoin_np(pthread_t pthread, void **thread_return,
37           const struct timespec *abstime);
38 
39 static int join_common(pthread_t, void **, const struct timespec *);
40 
backout_join(void * arg)41 static void backout_join(void *arg)
42 {
43           pthread_t curthread = tls_get_curthread();
44           pthread_t pthread = (pthread_t)arg;
45 
46           THREAD_LIST_LOCK(curthread);
47           pthread->joiner = NULL;
48           THREAD_LIST_UNLOCK(curthread);
49 }
50 
51 int
_pthread_join(pthread_t pthread,void ** thread_return)52 _pthread_join(pthread_t pthread, void **thread_return)
53 {
54           return (join_common(pthread, thread_return, NULL));
55 }
56 
57 int
_pthread_timedjoin_np(pthread_t pthread,void ** thread_return,const struct timespec * abstime)58 _pthread_timedjoin_np(pthread_t pthread, void **thread_return,
59           const struct timespec *abstime)
60 {
61           if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
62               abstime->tv_nsec >= 1000000000)
63                     return (EINVAL);
64 
65           return (join_common(pthread, thread_return, abstime));
66 }
67 
68 static int
join_common(pthread_t pthread,void ** thread_return,const struct timespec * abstime)69 join_common(pthread_t pthread, void **thread_return,
70           const struct timespec *abstime)
71 {
72           pthread_t curthread = tls_get_curthread();
73           struct timespec ts, ts2, *tsp;
74           void *tmp;
75           long state;
76           int oldcancel;
77           int ret = 0;
78 
79           if (pthread == NULL)
80                     return (EINVAL);
81 
82           if (pthread == curthread)
83                     return (EDEADLK);
84 
85           THREAD_LIST_LOCK(curthread);
86           if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0) {
87                     ret = ESRCH;
88           } else if ((pthread->tlflags & TLFLAGS_DETACHED) != 0) {
89                     ret = EINVAL;
90           } else if (pthread->joiner != NULL) {
91                     /* Multiple joiners are not supported. */
92                     ret = ENOTSUP;
93           }
94           if (ret) {
95                     THREAD_LIST_UNLOCK(curthread);
96                     return (ret);
97           }
98           /* Set the running thread to be the joiner: */
99           pthread->joiner = curthread;
100 
101           THREAD_LIST_UNLOCK(curthread);
102 
103           THR_CLEANUP_PUSH(curthread, backout_join, pthread);
104           oldcancel = _thr_cancel_enter(curthread);
105 
106           while ((state = pthread->state) != PS_DEAD) {
107                     cpu_ccfence();
108                     if (abstime != NULL) {
109                               clock_gettime(CLOCK_REALTIME, &ts);
110                               timespecsub(abstime, &ts, &ts2);
111                               if (ts2.tv_sec < 0) {
112                                         ret = ETIMEDOUT;
113                                         break;
114                               }
115                               tsp = &ts2;
116                     } else
117                               tsp = NULL;
118                     ret = _thr_umtx_wait(&pthread->state, state, tsp,
119                                              CLOCK_REALTIME);
120                     if (ret == ETIMEDOUT)
121                               break;
122           }
123 
124           _thr_cancel_leave(curthread, oldcancel);
125           THR_CLEANUP_POP(curthread, 0);
126 
127           if (ret == ETIMEDOUT) {
128                     THREAD_LIST_LOCK(curthread);
129                     pthread->joiner = NULL;
130                     THREAD_LIST_UNLOCK(curthread);
131           } else {
132                     ret = 0;
133                     tmp = pthread->ret;
134                     THREAD_LIST_LOCK(curthread);
135                     pthread->tlflags |= TLFLAGS_DETACHED;
136                     pthread->joiner = NULL;
137                     THR_GCLIST_ADD(pthread);
138                     THREAD_LIST_UNLOCK(curthread);
139 
140                     if (thread_return != NULL)
141                               *thread_return = tmp;
142           }
143           return (ret);
144 }
145 
146 __strong_reference(_pthread_join, pthread_join);
147 __strong_reference(_pthread_timedjoin_np, pthread_timedjoin_np);
148