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