171b3fa15SDavid Xu /*
2d3b15642Szrj  * Copyright (c) 2005, David Xu <davidxu@freebsd.org>
371b3fa15SDavid Xu  * All rights reserved.
471b3fa15SDavid Xu  *
571b3fa15SDavid Xu  * Redistribution and use in source and binary forms, with or without
671b3fa15SDavid Xu  * modification, are permitted provided that the following conditions
771b3fa15SDavid Xu  * are met:
871b3fa15SDavid Xu  * 1. Redistributions of source code must retain the above copyright
971b3fa15SDavid Xu  *    notice, this list of conditions and the following disclaimer.
1071b3fa15SDavid Xu  * 2. Redistributions in binary form must reproduce the above copyright
1171b3fa15SDavid Xu  *    notice, this list of conditions and the following disclaimer in the
1271b3fa15SDavid Xu  *    documentation and/or other materials provided with the distribution.
1371b3fa15SDavid Xu  *
14d3b15642Szrj  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15d3b15642Szrj  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16d3b15642Szrj  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17d3b15642Szrj  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18d3b15642Szrj  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19d3b15642Szrj  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20d3b15642Szrj  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21d3b15642Szrj  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22d3b15642Szrj  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23d3b15642Szrj  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2471b3fa15SDavid Xu  */
259e2ee207SJoerg Sonnenberger 
26fc71f871SDavid Xu #include "namespace.h"
279e2ee207SJoerg Sonnenberger #include <machine/tls.h>
2871b3fa15SDavid Xu #include <errno.h>
2971b3fa15SDavid Xu #include <pthread.h>
30fc71f871SDavid Xu #include "un-namespace.h"
31fc71f871SDavid Xu 
3271b3fa15SDavid Xu #include "thr_private.h"
3371b3fa15SDavid Xu 
34e19be507SMatthew Dillon #define cpu_ccfence()       __asm __volatile("" : : : "memory")
35e19be507SMatthew Dillon 
36d1cf13b9SSascha Wildner int _pthread_timedjoin_np(pthread_t pthread, void **thread_return,
37d1cf13b9SSascha Wildner 	const struct timespec *abstime);
38d1cf13b9SSascha Wildner 
39f78203a2SDavid Xu static int join_common(pthread_t, void **, const struct timespec *);
40f78203a2SDavid Xu 
backout_join(void * arg)4171b3fa15SDavid Xu static void backout_join(void *arg)
4271b3fa15SDavid Xu {
43*940be950Szrj 	pthread_t curthread = tls_get_curthread();
44*940be950Szrj 	pthread_t pthread = (pthread_t)arg;
4571b3fa15SDavid Xu 
4671b3fa15SDavid Xu 	THREAD_LIST_LOCK(curthread);
4771b3fa15SDavid Xu 	pthread->joiner = NULL;
4871b3fa15SDavid Xu 	THREAD_LIST_UNLOCK(curthread);
4971b3fa15SDavid Xu }
5071b3fa15SDavid Xu 
5171b3fa15SDavid Xu int
_pthread_join(pthread_t pthread,void ** thread_return)5271b3fa15SDavid Xu _pthread_join(pthread_t pthread, void **thread_return)
5371b3fa15SDavid Xu {
54f78203a2SDavid Xu 	return (join_common(pthread, thread_return, NULL));
55f78203a2SDavid Xu }
56f78203a2SDavid Xu 
57f78203a2SDavid Xu int
_pthread_timedjoin_np(pthread_t pthread,void ** thread_return,const struct timespec * abstime)58f78203a2SDavid Xu _pthread_timedjoin_np(pthread_t pthread, void **thread_return,
59f78203a2SDavid Xu 	const struct timespec *abstime)
60f78203a2SDavid Xu {
61f78203a2SDavid Xu 	if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
62f78203a2SDavid Xu 	    abstime->tv_nsec >= 1000000000)
63f78203a2SDavid Xu 		return (EINVAL);
64f78203a2SDavid Xu 
65f78203a2SDavid Xu 	return (join_common(pthread, thread_return, abstime));
66f78203a2SDavid Xu }
67f78203a2SDavid Xu 
68f78203a2SDavid Xu static int
join_common(pthread_t pthread,void ** thread_return,const struct timespec * abstime)69f78203a2SDavid Xu join_common(pthread_t pthread, void **thread_return,
70f78203a2SDavid Xu 	const struct timespec *abstime)
71f78203a2SDavid Xu {
72*940be950Szrj 	pthread_t curthread = tls_get_curthread();
73f78203a2SDavid Xu 	struct timespec ts, ts2, *tsp;
7471b3fa15SDavid Xu 	void *tmp;
7571b3fa15SDavid Xu 	long state;
7671b3fa15SDavid Xu 	int oldcancel;
7771b3fa15SDavid Xu 	int ret = 0;
7871b3fa15SDavid Xu 
7971b3fa15SDavid Xu 	if (pthread == NULL)
8071b3fa15SDavid Xu 		return (EINVAL);
8171b3fa15SDavid Xu 
8271b3fa15SDavid Xu 	if (pthread == curthread)
8371b3fa15SDavid Xu 		return (EDEADLK);
8471b3fa15SDavid Xu 
8571b3fa15SDavid Xu 	THREAD_LIST_LOCK(curthread);
8671b3fa15SDavid Xu 	if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0) {
8771b3fa15SDavid Xu 		ret = ESRCH;
8871b3fa15SDavid Xu 	} else if ((pthread->tlflags & TLFLAGS_DETACHED) != 0) {
893bfbb8ccSSascha Wildner 		ret = EINVAL;
9071b3fa15SDavid Xu 	} else if (pthread->joiner != NULL) {
9171b3fa15SDavid Xu 		/* Multiple joiners are not supported. */
9271b3fa15SDavid Xu 		ret = ENOTSUP;
9371b3fa15SDavid Xu 	}
9471b3fa15SDavid Xu 	if (ret) {
9571b3fa15SDavid Xu 		THREAD_LIST_UNLOCK(curthread);
9671b3fa15SDavid Xu 		return (ret);
9771b3fa15SDavid Xu 	}
9871b3fa15SDavid Xu 	/* Set the running thread to be the joiner: */
9971b3fa15SDavid Xu 	pthread->joiner = curthread;
10071b3fa15SDavid Xu 
10171b3fa15SDavid Xu 	THREAD_LIST_UNLOCK(curthread);
10271b3fa15SDavid Xu 
10371b3fa15SDavid Xu 	THR_CLEANUP_PUSH(curthread, backout_join, pthread);
10471b3fa15SDavid Xu 	oldcancel = _thr_cancel_enter(curthread);
10571b3fa15SDavid Xu 
10671b3fa15SDavid Xu 	while ((state = pthread->state) != PS_DEAD) {
107e19be507SMatthew Dillon 		cpu_ccfence();
108f78203a2SDavid Xu 		if (abstime != NULL) {
109f78203a2SDavid Xu 			clock_gettime(CLOCK_REALTIME, &ts);
110ce96aca2SSascha Wildner 			timespecsub(abstime, &ts, &ts2);
111f78203a2SDavid Xu 			if (ts2.tv_sec < 0) {
112f78203a2SDavid Xu 				ret = ETIMEDOUT;
113f78203a2SDavid Xu 				break;
114f78203a2SDavid Xu 			}
115f78203a2SDavid Xu 			tsp = &ts2;
116f78203a2SDavid Xu 		} else
117f78203a2SDavid Xu 			tsp = NULL;
118f78203a2SDavid Xu 		ret = _thr_umtx_wait(&pthread->state, state, tsp,
119f78203a2SDavid Xu 				     CLOCK_REALTIME);
120f78203a2SDavid Xu 		if (ret == ETIMEDOUT)
121f78203a2SDavid Xu 			break;
12271b3fa15SDavid Xu 	}
12371b3fa15SDavid Xu 
12471b3fa15SDavid Xu 	_thr_cancel_leave(curthread, oldcancel);
12571b3fa15SDavid Xu 	THR_CLEANUP_POP(curthread, 0);
12671b3fa15SDavid Xu 
127f78203a2SDavid Xu 	if (ret == ETIMEDOUT) {
128f78203a2SDavid Xu 		THREAD_LIST_LOCK(curthread);
129f78203a2SDavid Xu 		pthread->joiner = NULL;
130f78203a2SDavid Xu 		THREAD_LIST_UNLOCK(curthread);
131f78203a2SDavid Xu 	} else {
13209df5196SDavid Xu 		ret = 0;
13371b3fa15SDavid Xu 		tmp = pthread->ret;
13471b3fa15SDavid Xu 		THREAD_LIST_LOCK(curthread);
13571b3fa15SDavid Xu 		pthread->tlflags |= TLFLAGS_DETACHED;
136f78203a2SDavid Xu 		pthread->joiner = NULL;
13771b3fa15SDavid Xu 		THR_GCLIST_ADD(pthread);
13871b3fa15SDavid Xu 		THREAD_LIST_UNLOCK(curthread);
13971b3fa15SDavid Xu 
14071b3fa15SDavid Xu 		if (thread_return != NULL)
14171b3fa15SDavid Xu 			*thread_return = tmp;
142f78203a2SDavid Xu 	}
14371b3fa15SDavid Xu 	return (ret);
14471b3fa15SDavid Xu }
1455a1048c8SDavid Xu 
1465a1048c8SDavid Xu __strong_reference(_pthread_join, pthread_join);
1475a1048c8SDavid Xu __strong_reference(_pthread_timedjoin_np, pthread_timedjoin_np);
148