10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52958Sdr146992 * Common Development and Distribution License (the "License"). 62958Sdr146992 * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 212958Sdr146992 220Sstevel@tonic-gate /* 23*7513SDarren.Reed@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 242958Sdr146992 * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #ifndef _SYS_CONDVAR_IMPL_H 280Sstevel@tonic-gate #define _SYS_CONDVAR_IMPL_H 290Sstevel@tonic-gate 300Sstevel@tonic-gate /* 310Sstevel@tonic-gate * Implementation-private definitions for condition variables 320Sstevel@tonic-gate */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate #ifndef _ASM 350Sstevel@tonic-gate #include <sys/types.h> 360Sstevel@tonic-gate #include <sys/thread.h> 370Sstevel@tonic-gate #endif /* _ASM */ 380Sstevel@tonic-gate 390Sstevel@tonic-gate #ifdef __cplusplus 400Sstevel@tonic-gate extern "C" { 410Sstevel@tonic-gate #endif 420Sstevel@tonic-gate 430Sstevel@tonic-gate #ifndef _ASM 440Sstevel@tonic-gate 450Sstevel@tonic-gate /* 460Sstevel@tonic-gate * Condtion variables. 470Sstevel@tonic-gate */ 480Sstevel@tonic-gate 490Sstevel@tonic-gate typedef struct _condvar_impl { 500Sstevel@tonic-gate ushort_t cv_waiters; 510Sstevel@tonic-gate } condvar_impl_t; 520Sstevel@tonic-gate 530Sstevel@tonic-gate #define CV_HAS_WAITERS(cvp) (((condvar_impl_t *)(cvp))->cv_waiters != 0) 540Sstevel@tonic-gate 550Sstevel@tonic-gate #endif /* _ASM */ 560Sstevel@tonic-gate 572958Sdr146992 582958Sdr146992 typedef struct cvwaitlock_s { 592958Sdr146992 kmutex_t cvw_lock; 602958Sdr146992 kcondvar_t cvw_waiter; 612958Sdr146992 int cvw_refcnt; 622958Sdr146992 } cvwaitlock_t; 632958Sdr146992 642958Sdr146992 652958Sdr146992 #define CVW_INIT(_c) { \ 662958Sdr146992 mutex_init(&(_c)->cvw_lock, NULL, MUTEX_DRIVER, NULL); \ 672958Sdr146992 cv_init(&(_c)->cvw_waiter, NULL, CV_DRIVER, NULL); \ 682958Sdr146992 (_c)->cvw_refcnt = 0; \ 692958Sdr146992 } 702958Sdr146992 712958Sdr146992 #define CVW_ENTER_READ(_c) { \ 722958Sdr146992 mutex_enter(&(_c)->cvw_lock); \ 732958Sdr146992 while ((_c)->cvw_refcnt < 0) \ 742958Sdr146992 cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock); \ 752958Sdr146992 (_c)->cvw_refcnt++; \ 762958Sdr146992 mutex_exit(&(_c)->cvw_lock); \ 772958Sdr146992 } 782958Sdr146992 792958Sdr146992 #define CVW_ENTER_WRITE(_c) { \ 802958Sdr146992 mutex_enter(&(_c)->cvw_lock); \ 812958Sdr146992 while ((_c)->cvw_refcnt != 0) \ 822958Sdr146992 cv_wait(&((_c)->cvw_waiter), &(_c)->cvw_lock); \ 832958Sdr146992 (_c)->cvw_refcnt = -1; \ 842958Sdr146992 mutex_exit(&(_c)->cvw_lock); \ 852958Sdr146992 } 862958Sdr146992 872958Sdr146992 #define CVW_EXIT_READ(_c) { \ 882958Sdr146992 mutex_enter(&(_c)->cvw_lock); \ 892958Sdr146992 ASSERT((_c)->cvw_refcnt > 0); \ 902958Sdr146992 if ((--((_c)->cvw_refcnt)) == 0) \ 912958Sdr146992 cv_broadcast(&(_c)->cvw_waiter); \ 922958Sdr146992 mutex_exit(&(_c)->cvw_lock); \ 932958Sdr146992 } 942958Sdr146992 952958Sdr146992 #define CVW_EXIT_WRITE(_c) { \ 962958Sdr146992 mutex_enter(&(_c)->cvw_lock); \ 972958Sdr146992 ASSERT((_c)->cvw_refcnt == -1); \ 982958Sdr146992 (_c)->cvw_refcnt = 0; \ 992958Sdr146992 cv_broadcast(&(_c)->cvw_waiter); \ 1002958Sdr146992 mutex_exit(&(_c)->cvw_lock); \ 1012958Sdr146992 } 1022958Sdr146992 103*7513SDarren.Reed@Sun.COM #define CVW_WRITE_TO_READ(_c) { \ 104*7513SDarren.Reed@Sun.COM mutex_enter(&(_c)->cvw_lock); \ 105*7513SDarren.Reed@Sun.COM ASSERT((_c)->cvw_refcnt == -1); \ 106*7513SDarren.Reed@Sun.COM (_c)->cvw_refcnt = 1; \ 107*7513SDarren.Reed@Sun.COM cv_broadcast(&(_c)->cvw_waiter); \ 108*7513SDarren.Reed@Sun.COM mutex_exit(&(_c)->cvw_lock); \ 109*7513SDarren.Reed@Sun.COM } 110*7513SDarren.Reed@Sun.COM 1113448Sdh155122 #define CVW_DESTROY(_c) { \ 1123448Sdh155122 mutex_destroy(&(_c)->cvw_lock); \ 1133448Sdh155122 cv_destroy(&(_c)->cvw_waiter); \ 1143448Sdh155122 } 1153448Sdh155122 1160Sstevel@tonic-gate #ifdef __cplusplus 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate #endif 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate #endif /* _SYS_CONDVAR_IMPL_H */ 121