thr_join.c revision 150901
1/*
2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>.
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 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by John Birrell.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: head/lib/libthr/thread/thr_join.c 150901 2005-10-04 06:15:25Z davidxu $
33 */
34
35#include <errno.h>
36#include <pthread.h>
37
38#include "thr_private.h"
39
40static int join_common(pthread_t, void **, const struct timespec *);
41
42__weak_reference(_pthread_join, pthread_join);
43__weak_reference(_pthread_timedjoin_np, pthread_timedjoin_np);
44
45static void backout_join(void *arg)
46{
47	struct pthread *curthread = _get_curthread();
48	struct pthread *pthread = (struct pthread *)arg;
49
50	THREAD_LIST_LOCK(curthread);
51	pthread->joiner = NULL;
52	THREAD_LIST_UNLOCK(curthread);
53}
54
55int
56_pthread_join(pthread_t pthread, void **thread_return)
57{
58	return (join_common(pthread, thread_return, NULL));
59}
60
61int
62_pthread_timedjoin_np(pthread_t pthread, void **thread_return,
63	const struct timespec *abstime)
64{
65	if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
66	    abstime->tv_nsec >= 1000000000)
67		return (EINVAL);
68
69	return (join_common(pthread, thread_return, abstime));
70}
71
72static int
73join_common(pthread_t pthread, void **thread_return,
74	const struct timespec *abstime)
75{
76	struct pthread *curthread = _get_curthread();
77	struct timespec ts, ts2, *tsp;
78	void *tmp;
79	long state;
80	int oldcancel;
81	int ret = 0;
82
83	if (pthread == NULL)
84		return (EINVAL);
85
86	if (pthread == curthread)
87		return (EDEADLK);
88
89	THREAD_LIST_LOCK(curthread);
90	if ((ret = _thr_find_thread(curthread, pthread, 1)) != 0) {
91		ret = ESRCH;
92	} else if ((pthread->tlflags & TLFLAGS_DETACHED) != 0) {
93		ret = ESRCH;
94	} else if (pthread->joiner != NULL) {
95		/* Multiple joiners are not supported. */
96		ret = ENOTSUP;
97	}
98	if (ret) {
99		THREAD_LIST_UNLOCK(curthread);
100		return (ret);
101	}
102	/* Set the running thread to be the joiner: */
103	pthread->joiner = curthread;
104
105	THREAD_LIST_UNLOCK(curthread);
106
107	THR_CLEANUP_PUSH(curthread, backout_join, pthread);
108	oldcancel = _thr_cancel_enter(curthread);
109
110	while ((state = pthread->state) != PS_DEAD) {
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->state, state, 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		tmp = pthread->ret;
135		THREAD_LIST_LOCK(curthread);
136		pthread->tlflags |= TLFLAGS_DETACHED;
137		pthread->joiner = NULL;
138		THR_GCLIST_ADD(pthread);
139		THREAD_LIST_UNLOCK(curthread);
140
141		if (thread_return != NULL)
142			*thread_return = tmp;
143	}
144	return (ret);
145}
146