xref: /titanic_51/usr/src/uts/common/sys/condvar_impl.h (revision 72680cf5a1002d05d9daaa30635598ea7db543d1)
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*72680cf5SDarren Reed  * Copyright 2009 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 /*
317c478bd9Sstevel@tonic-gate  * Implementation-private definitions for condition variables
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifndef	_ASM
357c478bd9Sstevel@tonic-gate #include <sys/types.h>
367c478bd9Sstevel@tonic-gate #include <sys/thread.h>
377c478bd9Sstevel@tonic-gate #endif	/* _ASM */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
407c478bd9Sstevel@tonic-gate extern "C" {
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #ifndef	_ASM
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Condtion variables.
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate typedef struct _condvar_impl {
507c478bd9Sstevel@tonic-gate 	ushort_t	cv_waiters;
517c478bd9Sstevel@tonic-gate } condvar_impl_t;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	CV_HAS_WAITERS(cvp)	(((condvar_impl_t *)(cvp))->cv_waiters != 0)
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #endif	/* _ASM */
567c478bd9Sstevel@tonic-gate 
57381a2a9aSdr146992 
58*72680cf5SDarren Reed /*
59*72680cf5SDarren Reed  * The cvwaitlock_t structure and associated macros provide an implementation
60*72680cf5SDarren Reed  * of a locking mechanism that allows recursion on the reader lock without
61*72680cf5SDarren Reed  * danger of a pending write lock elsewhere being able to cause a deadlock.
62*72680cf5SDarren Reed  * The provision for supporting recursion is necessary for use with the
63*72680cf5SDarren Reed  * netinfo (neti) kernel module when processing network data.
64*72680cf5SDarren Reed  *
65*72680cf5SDarren Reed  * Support for recursion (giving precedence to readers and allowing them
66*72680cf5SDarren Reed  * to enter even when a write is blocked) can result in write starvation.
67*72680cf5SDarren Reed  * There is no priority inheritence for this locking interface.
68*72680cf5SDarren Reed  */
69381a2a9aSdr146992 typedef	struct	cvwaitlock_s	{
70381a2a9aSdr146992 	kmutex_t	cvw_lock;
71381a2a9aSdr146992 	kcondvar_t	cvw_waiter;
72381a2a9aSdr146992 	int		cvw_refcnt;
73381a2a9aSdr146992 } cvwaitlock_t;
74381a2a9aSdr146992 
75381a2a9aSdr146992 
76381a2a9aSdr146992 #define	CVW_INIT(_c)		{				\
77381a2a9aSdr146992 	mutex_init(&(_c)->cvw_lock, NULL, MUTEX_DRIVER, NULL);	\
78381a2a9aSdr146992 	cv_init(&(_c)->cvw_waiter, NULL, CV_DRIVER, NULL);	\
79381a2a9aSdr146992 	(_c)->cvw_refcnt = 0;					\
80381a2a9aSdr146992 }
81381a2a9aSdr146992 
82381a2a9aSdr146992 #define	CVW_ENTER_READ(_c)	{				\
83381a2a9aSdr146992 	mutex_enter(&(_c)->cvw_lock);				\
84381a2a9aSdr146992 	while ((_c)->cvw_refcnt < 0)				\
85381a2a9aSdr146992 		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
86381a2a9aSdr146992 	(_c)->cvw_refcnt++;					\
87381a2a9aSdr146992 	mutex_exit(&(_c)->cvw_lock);				\
88381a2a9aSdr146992 }
89381a2a9aSdr146992 
90381a2a9aSdr146992 #define	CVW_ENTER_WRITE(_c)	{				\
91381a2a9aSdr146992 	mutex_enter(&(_c)->cvw_lock);				\
92381a2a9aSdr146992 	while ((_c)->cvw_refcnt != 0)				\
93381a2a9aSdr146992 		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
94381a2a9aSdr146992 	(_c)->cvw_refcnt = -1;					\
95381a2a9aSdr146992 	mutex_exit(&(_c)->cvw_lock);				\
96381a2a9aSdr146992 }
97381a2a9aSdr146992 
98381a2a9aSdr146992 #define	CVW_EXIT_READ(_c)	{			\
99381a2a9aSdr146992 	mutex_enter(&(_c)->cvw_lock);			\
100381a2a9aSdr146992 	ASSERT((_c)->cvw_refcnt > 0);			\
101381a2a9aSdr146992 	if ((--((_c)->cvw_refcnt)) == 0)		\
102381a2a9aSdr146992 		cv_broadcast(&(_c)->cvw_waiter);	\
103381a2a9aSdr146992 	mutex_exit(&(_c)->cvw_lock);			\
104381a2a9aSdr146992 }
105381a2a9aSdr146992 
106381a2a9aSdr146992 #define	CVW_EXIT_WRITE(_c)	{			\
107381a2a9aSdr146992 	mutex_enter(&(_c)->cvw_lock);			\
108381a2a9aSdr146992 	ASSERT((_c)->cvw_refcnt == -1);			\
109381a2a9aSdr146992 	(_c)->cvw_refcnt = 0;				\
110381a2a9aSdr146992 	cv_broadcast(&(_c)->cvw_waiter);		\
111381a2a9aSdr146992 	mutex_exit(&(_c)->cvw_lock);			\
112381a2a9aSdr146992 }
113381a2a9aSdr146992 
1147ddc9b1aSDarren Reed #define	CVW_WRITE_TO_READ(_c)	{			\
1157ddc9b1aSDarren Reed 	mutex_enter(&(_c)->cvw_lock);			\
1167ddc9b1aSDarren Reed 	ASSERT((_c)->cvw_refcnt == -1);			\
1177ddc9b1aSDarren Reed 	(_c)->cvw_refcnt = 1;				\
1187ddc9b1aSDarren Reed 	cv_broadcast(&(_c)->cvw_waiter);		\
1197ddc9b1aSDarren Reed 	mutex_exit(&(_c)->cvw_lock);			\
1207ddc9b1aSDarren Reed }
1217ddc9b1aSDarren Reed 
122f4b3ec61Sdh155122 #define	CVW_DESTROY(_c)	{				\
123f4b3ec61Sdh155122 	mutex_destroy(&(_c)->cvw_lock);			\
124f4b3ec61Sdh155122 	cv_destroy(&(_c)->cvw_waiter);			\
125f4b3ec61Sdh155122 }
126f4b3ec61Sdh155122 
1277c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate #endif
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #endif	/* _SYS_CONDVAR_IMPL_H */
132