xref: /titanic_44/usr/src/uts/common/sys/condvar_impl.h (revision 90f050286227cf4c4f8aa425555d04723d331d48)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SYS_CONDVAR_IMPL_H
28 #define	_SYS_CONDVAR_IMPL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * Implementation-private definitions for condition variables
34  */
35 
36 #ifndef	_ASM
37 #include <sys/types.h>
38 #include <sys/thread.h>
39 #endif	/* _ASM */
40 
41 #ifdef	__cplusplus
42 extern "C" {
43 #endif
44 
45 #ifndef	_ASM
46 
47 /*
48  * Condtion variables.
49  */
50 
51 typedef struct _condvar_impl {
52 	ushort_t	cv_waiters;
53 } condvar_impl_t;
54 
55 #define	CV_HAS_WAITERS(cvp)	(((condvar_impl_t *)(cvp))->cv_waiters != 0)
56 
57 #endif	/* _ASM */
58 
59 
60 typedef	struct	cvwaitlock_s	{
61 	kmutex_t	cvw_lock;
62 	kcondvar_t	cvw_waiter;
63 	int		cvw_refcnt;
64 } cvwaitlock_t;
65 
66 
67 #define	CVW_INIT(_c)		{				\
68 	mutex_init(&(_c)->cvw_lock, NULL, MUTEX_DRIVER, NULL);	\
69 	cv_init(&(_c)->cvw_waiter, NULL, CV_DRIVER, NULL);	\
70 	(_c)->cvw_refcnt = 0;					\
71 }
72 
73 #define	CVW_ENTER_READ(_c)	{				\
74 	mutex_enter(&(_c)->cvw_lock);				\
75 	while ((_c)->cvw_refcnt < 0)				\
76 		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
77 	(_c)->cvw_refcnt++;					\
78 	mutex_exit(&(_c)->cvw_lock);				\
79 }
80 
81 #define	CVW_ENTER_WRITE(_c)	{				\
82 	mutex_enter(&(_c)->cvw_lock);				\
83 	while ((_c)->cvw_refcnt != 0)				\
84 		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
85 	(_c)->cvw_refcnt = -1;					\
86 	mutex_exit(&(_c)->cvw_lock);				\
87 }
88 
89 #define	CVW_EXIT_READ(_c)	{			\
90 	mutex_enter(&(_c)->cvw_lock);			\
91 	ASSERT((_c)->cvw_refcnt > 0);			\
92 	if ((--((_c)->cvw_refcnt)) == 0)		\
93 		cv_broadcast(&(_c)->cvw_waiter);	\
94 	mutex_exit(&(_c)->cvw_lock);			\
95 }
96 
97 #define	CVW_EXIT_WRITE(_c)	{			\
98 	mutex_enter(&(_c)->cvw_lock);			\
99 	ASSERT((_c)->cvw_refcnt == -1);			\
100 	(_c)->cvw_refcnt = 0;				\
101 	cv_broadcast(&(_c)->cvw_waiter);		\
102 	mutex_exit(&(_c)->cvw_lock);			\
103 }
104 
105 #ifdef	__cplusplus
106 }
107 #endif
108 
109 #endif	/* _SYS_CONDVAR_IMPL_H */
110