xref: /illumos-gate/usr/src/lib/libc/port/sys/lwp_cond.c (revision 4de2612967d06c4fdbf524a62556a1e8118a006f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #pragma weak _lwp_cond_wait = __lwp_cond_wait
30 #pragma weak _lwp_cond_reltimedwait = __lwp_cond_reltimedwait
31 #pragma weak _lwp_cond_timedwait = __lwp_cond_timedwait
32 
33 #include "synonyms.h"
34 #include <sys/types.h>
35 
36 #include <time.h>
37 #include <errno.h>
38 #include <synch.h>
39 #include <sys/synch32.h>
40 #include <pthread.h>
41 
42 extern int ___lwp_cond_wait(lwp_cond_t *, lwp_mutex_t *, timespec_t *, int);
43 extern int ___lwp_mutex_timedlock(lwp_mutex_t *, timespec_t *);
44 
45 int
46 _lwp_cond_wait(cond_t *cv, mutex_t *mp)
47 {
48 	int error;
49 
50 	error = ___lwp_cond_wait(cv, mp, NULL, 0);
51 	if (mp->mutex_type & (PTHREAD_PRIO_INHERIT|PTHREAD_PRIO_PROTECT))
52 		(void) ___lwp_mutex_timedlock(mp, NULL);
53 	else
54 		(void) _lwp_mutex_lock(mp);
55 	return (error);
56 }
57 
58 int
59 _lwp_cond_reltimedwait(cond_t *cv, mutex_t *mp, timespec_t *relts)
60 {
61 	int error;
62 
63 	if (relts != NULL &&
64 	    (relts->tv_sec < 0 || (ulong_t)relts->tv_nsec >= NANOSEC))
65 		return (EINVAL);
66 	error = ___lwp_cond_wait(cv, mp, relts, 0);
67 	if (mp->mutex_type & (PTHREAD_PRIO_INHERIT|PTHREAD_PRIO_PROTECT))
68 		(void) ___lwp_mutex_timedlock(mp, NULL);
69 	else
70 		(void) _lwp_mutex_lock(mp);
71 	return (error);
72 }
73 
74 int
75 _lwp_cond_timedwait(cond_t *cv, mutex_t *mp, timespec_t *absts)
76 {
77 	extern void abstime_to_reltime(clockid_t,
78 		const timespec_t *, timespec_t *);
79 	timespec_t tslocal;
80 
81 	abstime_to_reltime(CLOCK_REALTIME, absts, &tslocal);
82 	return (_lwp_cond_reltimedwait(cv, mp, &tslocal));
83 }
84