xref: /illumos-gate/usr/src/uts/common/sys/condvar_impl.h (revision b31b5de1357c915fe7dab4d9646d9d84f9fe69bc)
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 #ifndef _SYS_CONDVAR_IMPL_H
28 #define	_SYS_CONDVAR_IMPL_H
29 
30 /*
31  * Implementation-private definitions for condition variables
32  */
33 
34 #ifndef	_ASM
35 #include <sys/types.h>
36 #include <sys/thread.h>
37 #endif	/* _ASM */
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 #ifndef	_ASM
44 
45 /*
46  * Condtion variables.
47  */
48 
49 typedef struct _condvar_impl {
50 	ushort_t	cv_waiters;
51 } condvar_impl_t;
52 
53 #define	CV_HAS_WAITERS(cvp)	(((condvar_impl_t *)(cvp))->cv_waiters != 0)
54 
55 #endif	/* _ASM */
56 
57 
58 typedef	struct	cvwaitlock_s	{
59 	kmutex_t	cvw_lock;
60 	kcondvar_t	cvw_waiter;
61 	int		cvw_refcnt;
62 } cvwaitlock_t;
63 
64 
65 #define	CVW_INIT(_c)		{				\
66 	mutex_init(&(_c)->cvw_lock, NULL, MUTEX_DRIVER, NULL);	\
67 	cv_init(&(_c)->cvw_waiter, NULL, CV_DRIVER, NULL);	\
68 	(_c)->cvw_refcnt = 0;					\
69 }
70 
71 #define	CVW_ENTER_READ(_c)	{				\
72 	mutex_enter(&(_c)->cvw_lock);				\
73 	while ((_c)->cvw_refcnt < 0)				\
74 		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
75 	(_c)->cvw_refcnt++;					\
76 	mutex_exit(&(_c)->cvw_lock);				\
77 }
78 
79 #define	CVW_ENTER_WRITE(_c)	{				\
80 	mutex_enter(&(_c)->cvw_lock);				\
81 	while ((_c)->cvw_refcnt != 0)				\
82 		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
83 	(_c)->cvw_refcnt = -1;					\
84 	mutex_exit(&(_c)->cvw_lock);				\
85 }
86 
87 #define	CVW_EXIT_READ(_c)	{			\
88 	mutex_enter(&(_c)->cvw_lock);			\
89 	ASSERT((_c)->cvw_refcnt > 0);			\
90 	if ((--((_c)->cvw_refcnt)) == 0)		\
91 		cv_broadcast(&(_c)->cvw_waiter);	\
92 	mutex_exit(&(_c)->cvw_lock);			\
93 }
94 
95 #define	CVW_EXIT_WRITE(_c)	{			\
96 	mutex_enter(&(_c)->cvw_lock);			\
97 	ASSERT((_c)->cvw_refcnt == -1);			\
98 	(_c)->cvw_refcnt = 0;				\
99 	cv_broadcast(&(_c)->cvw_waiter);		\
100 	mutex_exit(&(_c)->cvw_lock);			\
101 }
102 
103 #define	CVW_WRITE_TO_READ(_c)	{			\
104 	mutex_enter(&(_c)->cvw_lock);			\
105 	ASSERT((_c)->cvw_refcnt == -1);			\
106 	(_c)->cvw_refcnt = 1;				\
107 	cv_broadcast(&(_c)->cvw_waiter);		\
108 	mutex_exit(&(_c)->cvw_lock);			\
109 }
110 
111 #define	CVW_DESTROY(_c)	{				\
112 	mutex_destroy(&(_c)->cvw_lock);			\
113 	cv_destroy(&(_c)->cvw_waiter);			\
114 }
115 
116 #ifdef	__cplusplus
117 }
118 #endif
119 
120 #endif	/* _SYS_CONDVAR_IMPL_H */
121