rtld - Add fork hooks for libthread_xu to install
[dragonfly.git] / lib / libthread_xu / thread / thr_join.c
blobd82d4ded3ee1b401f2bc637819ef24916c592355
1 /*
2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
3 * All rights reserved.
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.
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.
26 #include "namespace.h"
27 #include <machine/tls.h>
28 #include <errno.h>
29 #include <pthread.h>
30 #include "un-namespace.h"
32 #include "thr_private.h"
34 #define cpu_ccfence() __asm __volatile("" : : : "memory")
36 int _pthread_timedjoin_np(pthread_t pthread, void **thread_return,
37 const struct timespec *abstime);
39 static int join_common(pthread_t, void **, const struct timespec *);
41 static void backout_join(void *arg)
43 struct pthread *curthread = tls_get_curthread();
44 struct pthread *pthread = (struct pthread *)arg;
46 THREAD_LIST_LOCK(curthread);
47 pthread->joiner = NULL;
48 THREAD_LIST_UNLOCK(curthread);
51 int
52 _pthread_join(pthread_t pthread, void **thread_return)
54 return (join_common(pthread, thread_return, NULL));
57 int
58 _pthread_timedjoin_np(pthread_t pthread, void **thread_return,
59 const struct timespec *abstime)
61 if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
62 abstime->tv_nsec >= 1000000000)
63 return (EINVAL);
65 return (join_common(pthread, thread_return, abstime));
68 static int
69 join_common(pthread_t pthread, void **thread_return,
70 const struct timespec *abstime)
72 struct pthread *curthread = tls_get_curthread();
73 struct timespec ts, ts2, *tsp;
74 void *tmp;
75 long state;
76 int oldcancel;
77 int ret = 0;
79 if (pthread == NULL)
80 return (EINVAL);
82 if (pthread == curthread)
83 return (EDEADLK);
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;
94 if (ret) {
95 THREAD_LIST_UNLOCK(curthread);
96 return (ret);
98 /* Set the running thread to be the joiner: */
99 pthread->joiner = curthread;
101 THREAD_LIST_UNLOCK(curthread);
103 THR_CLEANUP_PUSH(curthread, backout_join, pthread);
104 oldcancel = _thr_cancel_enter(curthread);
106 while ((state = pthread->state) != PS_DEAD) {
107 cpu_ccfence();
108 if (abstime != NULL) {
109 clock_gettime(CLOCK_REALTIME, &ts);
110 TIMESPEC_SUB(&ts2, abstime, &ts);
111 if (ts2.tv_sec < 0) {
112 ret = ETIMEDOUT;
113 break;
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;
124 _thr_cancel_leave(curthread, oldcancel);
125 THR_CLEANUP_POP(curthread, 0);
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);
140 if (thread_return != NULL)
141 *thread_return = tmp;
143 return (ret);
146 __strong_reference(_pthread_join, pthread_join);
147 __strong_reference(_pthread_timedjoin_np, pthread_timedjoin_np);