xref: /onnv-gate/usr/src/uts/common/sys/condvar_impl.h (revision 2958:98aa41c076f5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2958Sdr146992  * Common Development and Distribution License (the "License").
6*2958Sdr146992  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*2958Sdr146992 
220Sstevel@tonic-gate /*
23*2958Sdr146992  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*2958Sdr146992  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #ifndef _SYS_CONDVAR_IMPL_H
280Sstevel@tonic-gate #define	_SYS_CONDVAR_IMPL_H
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Implementation-private definitions for condition variables
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #ifndef	_ASM
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/thread.h>
390Sstevel@tonic-gate #endif	/* _ASM */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #ifdef	__cplusplus
420Sstevel@tonic-gate extern "C" {
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #ifndef	_ASM
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Condtion variables.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate 
510Sstevel@tonic-gate typedef struct _condvar_impl {
520Sstevel@tonic-gate 	ushort_t	cv_waiters;
530Sstevel@tonic-gate } condvar_impl_t;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #define	CV_HAS_WAITERS(cvp)	(((condvar_impl_t *)(cvp))->cv_waiters != 0)
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #endif	/* _ASM */
580Sstevel@tonic-gate 
59*2958Sdr146992 
60*2958Sdr146992 typedef	struct	cvwaitlock_s	{
61*2958Sdr146992 	kmutex_t	cvw_lock;
62*2958Sdr146992 	kcondvar_t	cvw_waiter;
63*2958Sdr146992 	int		cvw_refcnt;
64*2958Sdr146992 } cvwaitlock_t;
65*2958Sdr146992 
66*2958Sdr146992 
67*2958Sdr146992 #define	CVW_INIT(_c)		{				\
68*2958Sdr146992 	mutex_init(&(_c)->cvw_lock, NULL, MUTEX_DRIVER, NULL);	\
69*2958Sdr146992 	cv_init(&(_c)->cvw_waiter, NULL, CV_DRIVER, NULL);	\
70*2958Sdr146992 	(_c)->cvw_refcnt = 0;					\
71*2958Sdr146992 }
72*2958Sdr146992 
73*2958Sdr146992 #define	CVW_ENTER_READ(_c)	{				\
74*2958Sdr146992 	mutex_enter(&(_c)->cvw_lock);				\
75*2958Sdr146992 	while ((_c)->cvw_refcnt < 0)				\
76*2958Sdr146992 		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
77*2958Sdr146992 	(_c)->cvw_refcnt++;					\
78*2958Sdr146992 	mutex_exit(&(_c)->cvw_lock);				\
79*2958Sdr146992 }
80*2958Sdr146992 
81*2958Sdr146992 #define	CVW_ENTER_WRITE(_c)	{				\
82*2958Sdr146992 	mutex_enter(&(_c)->cvw_lock);				\
83*2958Sdr146992 	while ((_c)->cvw_refcnt != 0)				\
84*2958Sdr146992 		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
85*2958Sdr146992 	(_c)->cvw_refcnt = -1;					\
86*2958Sdr146992 	mutex_exit(&(_c)->cvw_lock);				\
87*2958Sdr146992 }
88*2958Sdr146992 
89*2958Sdr146992 #define	CVW_EXIT_READ(_c)	{			\
90*2958Sdr146992 	mutex_enter(&(_c)->cvw_lock);			\
91*2958Sdr146992 	ASSERT((_c)->cvw_refcnt > 0);			\
92*2958Sdr146992 	if ((--((_c)->cvw_refcnt)) == 0)		\
93*2958Sdr146992 		cv_broadcast(&(_c)->cvw_waiter);	\
94*2958Sdr146992 	mutex_exit(&(_c)->cvw_lock);			\
95*2958Sdr146992 }
96*2958Sdr146992 
97*2958Sdr146992 #define	CVW_EXIT_WRITE(_c)	{			\
98*2958Sdr146992 	mutex_enter(&(_c)->cvw_lock);			\
99*2958Sdr146992 	ASSERT((_c)->cvw_refcnt == -1);			\
100*2958Sdr146992 	(_c)->cvw_refcnt = 0;				\
101*2958Sdr146992 	cv_broadcast(&(_c)->cvw_waiter);		\
102*2958Sdr146992 	mutex_exit(&(_c)->cvw_lock);			\
103*2958Sdr146992 }
104*2958Sdr146992 
1050Sstevel@tonic-gate #ifdef	__cplusplus
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate #endif
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #endif	/* _SYS_CONDVAR_IMPL_H */
110