condvar_impl.h revision 2958:98aa41c076f5
1266072Sdes/*
2266072Sdes * CDDL HEADER START
3266072Sdes *
4266072Sdes * The contents of this file are subject to the terms of the
5266072Sdes * Common Development and Distribution License (the "License").
6266072Sdes * You may not use this file except in compliance with the License.
7266072Sdes *
8266072Sdes * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9266072Sdes * or http://www.opensolaris.org/os/licensing.
10266072Sdes * See the License for the specific language governing permissions
11266072Sdes * and limitations under the License.
12266072Sdes *
13266072Sdes * When distributing Covered Code, include this CDDL HEADER in each
14266072Sdes * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15266072Sdes * If applicable, add the following below this CDDL HEADER, with the
16266072Sdes * fields enclosed by brackets "[]" replaced with your own identifying
17266072Sdes * information: Portions Copyright [yyyy] [name of copyright owner]
18266072Sdes *
19266072Sdes * CDDL HEADER END
20266072Sdes */
21266072Sdes
22266072Sdes/*
23266072Sdes * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24266072Sdes * Use is subject to license terms.
25266072Sdes */
26266072Sdes
27266072Sdes#ifndef _SYS_CONDVAR_IMPL_H
28266072Sdes#define	_SYS_CONDVAR_IMPL_H
29266072Sdes
30266072Sdes#pragma ident	"%Z%%M%	%I%	%E% SMI"
31266072Sdes
32266072Sdes/*
33266072Sdes * Implementation-private definitions for condition variables
34266072Sdes */
35266072Sdes
36266072Sdes#ifndef	_ASM
37266072Sdes#include <sys/types.h>
38266072Sdes#include <sys/thread.h>
39266072Sdes#endif	/* _ASM */
40266072Sdes
41266072Sdes#ifdef	__cplusplus
42266072Sdesextern "C" {
43266072Sdes#endif
44266072Sdes
45266072Sdes#ifndef	_ASM
46266072Sdes
47266072Sdes/*
48266072Sdes * Condtion variables.
49266072Sdes */
50266072Sdes
51266072Sdestypedef struct _condvar_impl {
52266072Sdes	ushort_t	cv_waiters;
53266072Sdes} condvar_impl_t;
54266072Sdes
55266072Sdes#define	CV_HAS_WAITERS(cvp)	(((condvar_impl_t *)(cvp))->cv_waiters != 0)
56266072Sdes
57266072Sdes#endif	/* _ASM */
58266072Sdes
59266072Sdes
60266072Sdestypedef	struct	cvwaitlock_s	{
61266072Sdes	kmutex_t	cvw_lock;
62266072Sdes	kcondvar_t	cvw_waiter;
63266072Sdes	int		cvw_refcnt;
64266072Sdes} cvwaitlock_t;
65266072Sdes
66266072Sdes
67266072Sdes#define	CVW_INIT(_c)		{				\
68266072Sdes	mutex_init(&(_c)->cvw_lock, NULL, MUTEX_DRIVER, NULL);	\
69266072Sdes	cv_init(&(_c)->cvw_waiter, NULL, CV_DRIVER, NULL);	\
70266072Sdes	(_c)->cvw_refcnt = 0;					\
71266072Sdes}
72266072Sdes
73266072Sdes#define	CVW_ENTER_READ(_c)	{				\
74266072Sdes	mutex_enter(&(_c)->cvw_lock);				\
75266072Sdes	while ((_c)->cvw_refcnt < 0)				\
76266072Sdes		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
77266072Sdes	(_c)->cvw_refcnt++;					\
78266072Sdes	mutex_exit(&(_c)->cvw_lock);				\
79266072Sdes}
80266072Sdes
81266072Sdes#define	CVW_ENTER_WRITE(_c)	{				\
82266072Sdes	mutex_enter(&(_c)->cvw_lock);				\
83266072Sdes	while ((_c)->cvw_refcnt != 0)				\
84266072Sdes		cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock);	\
85266072Sdes	(_c)->cvw_refcnt = -1;					\
86266072Sdes	mutex_exit(&(_c)->cvw_lock);				\
87266072Sdes}
88266072Sdes
89266072Sdes#define	CVW_EXIT_READ(_c)	{			\
90266072Sdes	mutex_enter(&(_c)->cvw_lock);			\
91266072Sdes	ASSERT((_c)->cvw_refcnt > 0);			\
92266072Sdes	if ((--((_c)->cvw_refcnt)) == 0)		\
93266072Sdes		cv_broadcast(&(_c)->cvw_waiter);	\
94266072Sdes	mutex_exit(&(_c)->cvw_lock);			\
95266072Sdes}
96266072Sdes
97266072Sdes#define	CVW_EXIT_WRITE(_c)	{			\
98266072Sdes	mutex_enter(&(_c)->cvw_lock);			\
99266072Sdes	ASSERT((_c)->cvw_refcnt == -1);			\
100266072Sdes	(_c)->cvw_refcnt = 0;				\
101266072Sdes	cv_broadcast(&(_c)->cvw_waiter);		\
102266072Sdes	mutex_exit(&(_c)->cvw_lock);			\
103266072Sdes}
104266072Sdes
105266072Sdes#ifdef	__cplusplus
106266072Sdes}
107266072Sdes#endif
108266072Sdes
109266072Sdes#endif	/* _SYS_CONDVAR_IMPL_H */
110266072Sdes