2 * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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>
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
);
52 _pthread_join(pthread_t pthread
, void **thread_return
)
54 return (join_common(pthread
, thread_return
, NULL
));
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)
65 return (join_common(pthread
, thread_return
, abstime
));
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
;
82 if (pthread
== curthread
)
85 THREAD_LIST_LOCK(curthread
);
86 if ((ret
= _thr_find_thread(curthread
, pthread
, 1)) != 0) {
88 } else if ((pthread
->tlflags
& TLFLAGS_DETACHED
) != 0) {
90 } else if (pthread
->joiner
!= NULL
) {
91 /* Multiple joiners are not supported. */
95 THREAD_LIST_UNLOCK(curthread
);
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
) {
108 if (abstime
!= NULL
) {
109 clock_gettime(CLOCK_REALTIME
, &ts
);
110 TIMESPEC_SUB(&ts2
, abstime
, &ts
);
111 if (ts2
.tv_sec
< 0) {
118 ret
= _thr_umtx_wait(&pthread
->state
, state
, tsp
,
120 if (ret
== ETIMEDOUT
)
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
);
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
;
146 __strong_reference(_pthread_join
, pthread_join
);
147 __strong_reference(_pthread_timedjoin_np
, pthread_timedjoin_np
);