xref: /illumos-gate/usr/src/lib/libc/port/sys/lwp_cond.c (revision 07a48826732249fcd3aa8dd53c8389595e9f1fbc)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 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 #include "lint.h"
30 #include <sys/types.h>
31 
32 #include <time.h>
33 #include <errno.h>
34 #include <synch.h>
35 #include <sys/synch32.h>
36 #include <pthread.h>
37 
38 extern int ___lwp_cond_wait(lwp_cond_t *, lwp_mutex_t *, timespec_t *, int);
39 extern int ___lwp_mutex_timedlock(lwp_mutex_t *, timespec_t *);
40 
41 int
42 _lwp_cond_wait(cond_t *cv, mutex_t *mp)
43 {
44 	int error;
45 
46 	error = ___lwp_cond_wait(cv, mp, NULL, 0);
47 	if (mp->mutex_type & (PTHREAD_PRIO_INHERIT|PTHREAD_PRIO_PROTECT))
48 		(void) ___lwp_mutex_timedlock(mp, NULL);
49 	else
50 		(void) _lwp_mutex_lock(mp);
51 	return (error);
52 }
53 
54 int
55 _lwp_cond_reltimedwait(cond_t *cv, mutex_t *mp, timespec_t *relts)
56 {
57 	int error;
58 
59 	if (relts != NULL &&
60 	    (relts->tv_sec < 0 || (ulong_t)relts->tv_nsec >= NANOSEC))
61 		return (EINVAL);
62 	error = ___lwp_cond_wait(cv, mp, relts, 0);
63 	if (mp->mutex_type & (PTHREAD_PRIO_INHERIT|PTHREAD_PRIO_PROTECT))
64 		(void) ___lwp_mutex_timedlock(mp, NULL);
65 	else
66 		(void) _lwp_mutex_lock(mp);
67 	return (error);
68 }
69 
70 int
71 _lwp_cond_timedwait(cond_t *cv, mutex_t *mp, timespec_t *absts)
72 {
73 	extern void abstime_to_reltime(clockid_t,
74 	    const timespec_t *, timespec_t *);
75 	timespec_t tslocal;
76 
77 	abstime_to_reltime(CLOCK_REALTIME, absts, &tslocal);
78 	return (_lwp_cond_reltimedwait(cv, mp, &tslocal));
79 }
80