17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5381a2a9aSdr146992  * Common Development and Distribution License (the "License").
6381a2a9aSdr146992  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21381a2a9aSdr146992 
227c478bd9Sstevel@tonic-gate /*
23*f4b3ec61Sdh155122  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24381a2a9aSdr146992  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef _SYS_CONDVAR_IMPL_H
287c478bd9Sstevel@tonic-gate #define	_SYS_CONDVAR_IMPL_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Implementation-private definitions for condition variables
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #ifndef	_ASM
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/thread.h>
397c478bd9Sstevel@tonic-gate #endif	/* _ASM */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
427c478bd9Sstevel@tonic-gate extern "C" {
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #ifndef	_ASM
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Condtion variables.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate typedef struct _condvar_impl {
527c478bd9Sstevel@tonic-gate 	ushort_t	cv_waiters;
537c478bd9Sstevel@tonic-gate } condvar_impl_t;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	CV_HAS_WAITERS(cvp)	(((condvar_impl_t *)(cvp))->cv_waiters != 0)
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #endif	/* _ASM */
587c478bd9Sstevel@tonic-gate 
59381a2a9aSdr146992 
60381a2a9aSdr146992 typedef	struct	cvwaitlock_s	{
61381a2a9aSdr146992 	kmutex_t	cvw_lock;
62381a2a9aSdr146992 	kcondvar_t	cvw_waiter;
63381a2a9aSdr146992 	int		cvw_refcnt;
64381a2a9aSdr146992 } cvwaitlock_t;
65381a2a9aSdr146992 
66381a2a9aSdr146992 
67381a2a9aSdr146992 #define	CVW_INIT(_c)		{				\
68381a2a9aSdr146992 	mutex_init(&(_c)->cvw_lock, NULL, MUTEX_DRIVER, NULL);	\
69381a2a9aSdr146992 	cv_init(&(_c)->cvw_waiter, NULL, CV_DRIVER, NULL);	\
70381a2a9aSdr146992 	(_c)->cvw_refcnt = 0;					\
71381a2a9aSdr146992 }
72381a2a9aSdr146992 
73381a2a9aSdr146992 #define	CVW_ENTER_READ(_c)	{				\
74381a2a9aSdr146992 	mutex_enter(&(_c)->cvw_lock);				\
75381a2a9aSdr146992 	while ((_c)->cvw_refcnt < 0)				\
76381a2a9aSdr146992 		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
77381a2a9aSdr146992 	(_c)->cvw_refcnt++;					\
78381a2a9aSdr146992 	mutex_exit(&(_c)->cvw_lock);				\
79381a2a9aSdr146992 }
80381a2a9aSdr146992 
81381a2a9aSdr146992 #define	CVW_ENTER_WRITE(_c)	{				\
82381a2a9aSdr146992 	mutex_enter(&(_c)->cvw_lock);				\
83381a2a9aSdr146992 	while ((_c)->cvw_refcnt != 0)				\
84381a2a9aSdr146992 		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
85381a2a9aSdr146992 	(_c)->cvw_refcnt = -1;					\
86381a2a9aSdr146992 	mutex_exit(&(_c)->cvw_lock);				\
87381a2a9aSdr146992 }
88381a2a9aSdr146992 
89381a2a9aSdr146992 #define	CVW_EXIT_READ(_c)	{			\
90381a2a9aSdr146992 	mutex_enter(&(_c)->cvw_lock);			\
91381a2a9aSdr146992 	ASSERT((_c)->cvw_refcnt > 0);			\
92381a2a9aSdr146992 	if ((--((_c)->cvw_refcnt)) == 0)		\
93381a2a9aSdr146992 		cv_broadcast(&(_c)->cvw_waiter);	\
94381a2a9aSdr146992 	mutex_exit(&(_c)->cvw_lock);			\
95381a2a9aSdr146992 }
96381a2a9aSdr146992 
97381a2a9aSdr146992 #define	CVW_EXIT_WRITE(_c)	{			\
98381a2a9aSdr146992 	mutex_enter(&(_c)->cvw_lock);			\
99381a2a9aSdr146992 	ASSERT((_c)->cvw_refcnt == -1);			\
100381a2a9aSdr146992 	(_c)->cvw_refcnt = 0;				\
101381a2a9aSdr146992 	cv_broadcast(&(_c)->cvw_waiter);		\
102381a2a9aSdr146992 	mutex_exit(&(_c)->cvw_lock);			\
103381a2a9aSdr146992 }
104381a2a9aSdr146992 
105*f4b3ec61Sdh155122 #define	CVW_DESTROY(_c)	{				\
106*f4b3ec61Sdh155122 	mutex_destroy(&(_c)->cvw_lock);			\
107*f4b3ec61Sdh155122 	cv_destroy(&(_c)->cvw_waiter);			\
108*f4b3ec61Sdh155122 }
109*f4b3ec61Sdh155122 
1107c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate #endif
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #endif	/* _SYS_CONDVAR_IMPL_H */
115