thr_cond.c revision 164905
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: head/lib/libthr/thread/thr_cond.c 164905 2006-12-05 07:23:58Z davidxu $
27 */
28
29#include "namespace.h"
30#include <stdlib.h>
31#include <errno.h>
32#include <string.h>
33#include <pthread.h>
34#include <limits.h>
35#include "un-namespace.h"
36
37#include "thr_private.h"
38
39/*
40 * Prototypes
41 */
42int	__pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
43int	__pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
44		       const struct timespec * abstime);
45static int cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);
46static int cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
47		    const struct timespec *abstime, int cancel);
48static int cond_signal_common(pthread_cond_t *cond, int broadcast);
49
50/*
51 * Double underscore versions are cancellation points.  Single underscore
52 * versions are not and are provided for libc internal usage (which
53 * shouldn't introduce cancellation points).
54 */
55__weak_reference(__pthread_cond_wait, pthread_cond_wait);
56__weak_reference(__pthread_cond_timedwait, pthread_cond_timedwait);
57
58__weak_reference(_pthread_cond_init, pthread_cond_init);
59__weak_reference(_pthread_cond_destroy, pthread_cond_destroy);
60__weak_reference(_pthread_cond_signal, pthread_cond_signal);
61__weak_reference(_pthread_cond_broadcast, pthread_cond_broadcast);
62
63static int
64cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
65{
66	pthread_cond_t	pcond;
67	int             rval = 0;
68
69	if ((pcond = (pthread_cond_t)
70	    calloc(1, sizeof(struct pthread_cond))) == NULL) {
71		rval = ENOMEM;
72	} else {
73		/*
74		 * Initialise the condition variable structure:
75		 */
76		if (cond_attr == NULL || *cond_attr == NULL) {
77			pcond->c_pshared = 0;
78			pcond->c_clockid = CLOCK_REALTIME;
79		} else {
80			pcond->c_pshared = (*cond_attr)->c_pshared;
81			pcond->c_clockid = (*cond_attr)->c_clockid;
82		}
83		_thr_umutex_init(&pcond->c_lock);
84		*cond = pcond;
85	}
86	/* Return the completion status: */
87	return (rval);
88}
89
90static int
91init_static(struct pthread *thread, pthread_cond_t *cond)
92{
93	int ret;
94
95	THR_LOCK_ACQUIRE(thread, &_cond_static_lock);
96
97	if (*cond == NULL)
98		ret = cond_init(cond, NULL);
99	else
100		ret = 0;
101
102	THR_LOCK_RELEASE(thread, &_cond_static_lock);
103
104	return (ret);
105}
106
107int
108_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr)
109{
110
111	*cond = NULL;
112	return (cond_init(cond, cond_attr));
113}
114
115int
116_pthread_cond_destroy(pthread_cond_t *cond)
117{
118	struct pthread		*curthread = _get_curthread();
119	struct pthread_cond	*cv;
120	int			rval = 0;
121
122	if (*cond == NULL)
123		rval = EINVAL;
124	else {
125		cv = *cond;
126		THR_UMUTEX_LOCK(curthread, &cv->c_lock);
127#if 0
128		/* Lock the condition variable structure: */
129		if (cv->c_kerncv.c_has_waiters) {
130			THR_UMUTEX_UNLOCK(curthread, &cv->c_lock);
131			return (EBUSY);
132		}
133#endif
134		/*
135		 * NULL the caller's pointer now that the condition
136		 * variable has been destroyed:
137		 */
138		*cond = NULL;
139		THR_UMUTEX_UNLOCK(curthread, &cv->c_lock);
140
141		/*
142		 * Free the memory allocated for the condition
143		 * variable structure:
144		 */
145		free(cv);
146
147	}
148	/* Return the completion status: */
149	return (rval);
150}
151
152struct cond_cancel_info
153{
154	pthread_mutex_t	*mutex;
155	pthread_cond_t	*cond;
156	int		count;
157};
158
159static void
160cond_cancel_handler(void *arg)
161{
162	struct pthread *curthread = _get_curthread();
163	struct cond_cancel_info *info = (struct cond_cancel_info *)arg;
164	pthread_cond_t  cv;
165
166	cv = *(info->cond);
167	if ((cv->c_lock.m_owner & ~UMUTEX_CONTESTED) == TID(curthread))
168		THR_UMUTEX_UNLOCK(curthread, &cv->c_lock);
169	_mutex_cv_lock(info->mutex, info->count);
170}
171
172static int
173cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
174	const struct timespec *abstime, int cancel)
175{
176	struct pthread	*curthread = _get_curthread();
177	struct timespec ts, ts2, *tsp;
178	struct cond_cancel_info info;
179	pthread_cond_t  cv;
180	int		ret = 0;
181
182	/*
183	 * If the condition variable is statically initialized,
184	 * perform the dynamic initialization:
185	 */
186	if (__predict_false(*cond == NULL &&
187	    (ret = init_static(curthread, cond)) != 0))
188		return (ret);
189
190	cv = *cond;
191	THR_UMUTEX_LOCK(curthread, &cv->c_lock);
192	ret = _mutex_cv_unlock(mutex, &info.count);
193	if (ret) {
194		THR_UMUTEX_UNLOCK(curthread, &cv->c_lock);
195		return (ret);
196	}
197
198	info.mutex = mutex;
199	info.cond  = cond;
200
201	if (abstime != NULL) {
202		clock_gettime(cv->c_clockid, &ts);
203		TIMESPEC_SUB(&ts2, abstime, &ts);
204		tsp = &ts2;
205	} else
206		tsp = NULL;
207
208	if (cancel) {
209		THR_CLEANUP_PUSH(curthread, cond_cancel_handler, &info);
210		_thr_cancel_enter_defer(curthread);
211		ret = _thr_ucond_wait(&cv->c_kerncv, &cv->c_lock, tsp, 1);
212		_thr_cancel_leave_defer(curthread, ret);
213		THR_CLEANUP_POP(curthread, 0);
214	} else {
215		ret = _thr_ucond_wait(&cv->c_kerncv, &cv->c_lock, tsp, 0);
216	}
217	if (ret == EINTR)
218		ret = 0;
219	_mutex_cv_lock(mutex, info.count);
220	return (ret);
221}
222
223int
224_pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
225{
226
227	return (cond_wait_common(cond, mutex, NULL, 0));
228}
229
230int
231__pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
232{
233
234	return (cond_wait_common(cond, mutex, NULL, 1));
235}
236
237int
238_pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex,
239		       const struct timespec * abstime)
240{
241
242	if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
243	    abstime->tv_nsec >= 1000000000)
244		return (EINVAL);
245
246	return (cond_wait_common(cond, mutex, abstime, 0));
247}
248
249int
250__pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
251		       const struct timespec *abstime)
252{
253
254	if (abstime == NULL || abstime->tv_sec < 0 || abstime->tv_nsec < 0 ||
255	    abstime->tv_nsec >= 1000000000)
256		return (EINVAL);
257
258	return (cond_wait_common(cond, mutex, abstime, 1));
259}
260
261static int
262cond_signal_common(pthread_cond_t *cond, int broadcast)
263{
264	struct pthread	*curthread = _get_curthread();
265	pthread_cond_t	cv;
266	int		ret = 0;
267
268	/*
269	 * If the condition variable is statically initialized, perform dynamic
270	 * initialization.
271	 */
272	if (__predict_false(*cond == NULL &&
273	    (ret = init_static(curthread, cond)) != 0))
274		return (ret);
275
276	cv = *cond;
277	THR_UMUTEX_LOCK(curthread, &cv->c_lock);
278	if (cv->c_kerncv.c_has_waiters) {
279		if (!broadcast)
280			ret = _thr_ucond_signal(&cv->c_kerncv);
281		else
282			ret = _thr_ucond_broadcast(&cv->c_kerncv);
283	}
284	THR_UMUTEX_UNLOCK(curthread, &cv->c_lock);
285	return (ret);
286}
287
288int
289_pthread_cond_signal(pthread_cond_t * cond)
290{
291
292	return (cond_signal_common(cond, 0));
293}
294
295int
296_pthread_cond_broadcast(pthread_cond_t * cond)
297{
298
299	return (cond_signal_common(cond, 1));
300}
301