xref: /titanic_51/usr/src/uts/common/sys/rwstlock.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #ifndef _SYS_RWSTLOCK_H
28*7c478bd9Sstevel@tonic-gate #define	_SYS_RWSTLOCK_H
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate /*
33*7c478bd9Sstevel@tonic-gate  * Alternate rwlock that is interruptible and can be released by a thread
34*7c478bd9Sstevel@tonic-gate  * other than the one that acquired the lock.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus
38*7c478bd9Sstevel@tonic-gate extern "C" {
39*7c478bd9Sstevel@tonic-gate #endif
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/ksynch.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/rwlock.h>
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate typedef struct rwstlock {
46*7c478bd9Sstevel@tonic-gate 	intptr_t	rwst_count;
47*7c478bd9Sstevel@tonic-gate 	kcondvar_t	rwst_rcv;
48*7c478bd9Sstevel@tonic-gate 	kcondvar_t	rwst_wcv;
49*7c478bd9Sstevel@tonic-gate 	kmutex_t	rwst_lock;
50*7c478bd9Sstevel@tonic-gate } rwstlock_t;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  * The interfaces below are private to Sun Microsystems,
54*7c478bd9Sstevel@tonic-gate  * and these might change without notice.
55*7c478bd9Sstevel@tonic-gate  */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #define	RWST_TRYENTER	0x01
58*7c478bd9Sstevel@tonic-gate #define	RWST_SIG	0x02
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate #define	RWST_HELD(l)		((l)->rwst_count != 0)
61*7c478bd9Sstevel@tonic-gate #define	RWST_READ_HELD(l)	((l)->rwst_count > 0)
62*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_HELD(l)	((l)->rwst_count < 0)
63*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_OWNER(l)	\
64*7c478bd9Sstevel@tonic-gate 	((l)->rwst_count == (LONG_MIN | (intptr_t)curthread))
65*7c478bd9Sstevel@tonic-gate #define	RWST_OWNER(l)		(RWST_WRITE_HELD(l) ? \
66*7c478bd9Sstevel@tonic-gate 	((struct _kthread *)((l)->rwst_count & ~LONG_MIN)) : NULL)
67*7c478bd9Sstevel@tonic-gate #define	RWST_READ_WANTED(l)	CV_HAS_WAITERS(&(l)->rwst_rcv)
68*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_WANTED(l)	CV_HAS_WAITERS(&(l)->rwst_wcv)
69*7c478bd9Sstevel@tonic-gate #define	RWST_WAIT(cv, lock, f)	\
70*7c478bd9Sstevel@tonic-gate 	((f) & RWST_SIG ? cv_wait_sig(cv, lock) : (cv_wait(cv, lock), 1))
71*7c478bd9Sstevel@tonic-gate #define	RWST_READ_WAIT(l, f)	RWST_WAIT(&(l)->rwst_rcv, &(l)->rwst_lock, f)
72*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_WAIT(l, f)	RWST_WAIT(&(l)->rwst_wcv, &(l)->rwst_lock, f)
73*7c478bd9Sstevel@tonic-gate #define	RWST_READ_WAKE_ALL(l)	cv_broadcast(&(l)->rwst_rcv)
74*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_WAKE_ONE(l)	cv_signal(&(l)->rwst_wcv)
75*7c478bd9Sstevel@tonic-gate #define	RWST_READ_ENTER(l)	(l)->rwst_count++
76*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_ENTER(l)	(l)->rwst_count = LONG_MIN | (intptr_t)curthread
77*7c478bd9Sstevel@tonic-gate #define	RWST_READ_EXIT(l)	(l)->rwst_count--
78*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_EXIT(l)	(l)->rwst_count = 0
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate extern void rwst_enter(rwstlock_t *, krw_t);
81*7c478bd9Sstevel@tonic-gate extern int rwst_enter_sig(rwstlock_t *, krw_t);
82*7c478bd9Sstevel@tonic-gate extern void rwst_exit(rwstlock_t *);
83*7c478bd9Sstevel@tonic-gate extern void rwst_init(rwstlock_t *, char *, krw_type_t, void *);
84*7c478bd9Sstevel@tonic-gate extern void rwst_destroy(rwstlock_t *);
85*7c478bd9Sstevel@tonic-gate extern int rwst_lock_held(rwstlock_t *, krw_t);
86*7c478bd9Sstevel@tonic-gate extern int rwst_tryenter(rwstlock_t *, krw_t);
87*7c478bd9Sstevel@tonic-gate extern struct _kthread *rwst_owner(rwstlock_t *);
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate #endif
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate #endif /* _SYS_RWSTLOCK_H */
94