xref: /illumos-gate/usr/src/uts/common/sys/rwstlock.h (revision fafb665d)
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 /*
31*7c478bd9Sstevel@tonic-gate  * Alternate rwlock that is interruptible and can be released by a thread
32*7c478bd9Sstevel@tonic-gate  * other than the one that acquired the lock.
33*7c478bd9Sstevel@tonic-gate  */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus
36*7c478bd9Sstevel@tonic-gate extern "C" {
37*7c478bd9Sstevel@tonic-gate #endif
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/ksynch.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/rwlock.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate typedef struct rwstlock {
44*7c478bd9Sstevel@tonic-gate 	intptr_t	rwst_count;
45*7c478bd9Sstevel@tonic-gate 	kcondvar_t	rwst_rcv;
46*7c478bd9Sstevel@tonic-gate 	kcondvar_t	rwst_wcv;
47*7c478bd9Sstevel@tonic-gate 	kmutex_t	rwst_lock;
48*7c478bd9Sstevel@tonic-gate } rwstlock_t;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate  * The interfaces below are private to Sun Microsystems,
52*7c478bd9Sstevel@tonic-gate  * and these might change without notice.
53*7c478bd9Sstevel@tonic-gate  */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate #define	RWST_TRYENTER	0x01
56*7c478bd9Sstevel@tonic-gate #define	RWST_SIG	0x02
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate #define	RWST_HELD(l)		((l)->rwst_count != 0)
59*7c478bd9Sstevel@tonic-gate #define	RWST_READ_HELD(l)	((l)->rwst_count > 0)
60*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_HELD(l)	((l)->rwst_count < 0)
61*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_OWNER(l)	\
62*7c478bd9Sstevel@tonic-gate 	((l)->rwst_count == (LONG_MIN | (intptr_t)curthread))
63*7c478bd9Sstevel@tonic-gate #define	RWST_OWNER(l)		(RWST_WRITE_HELD(l) ? \
64*7c478bd9Sstevel@tonic-gate 	((struct _kthread *)((l)->rwst_count & ~LONG_MIN)) : NULL)
65*7c478bd9Sstevel@tonic-gate #define	RWST_READ_WANTED(l)	CV_HAS_WAITERS(&(l)->rwst_rcv)
66*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_WANTED(l)	CV_HAS_WAITERS(&(l)->rwst_wcv)
67*7c478bd9Sstevel@tonic-gate #define	RWST_WAIT(cv, lock, f)	\
68*7c478bd9Sstevel@tonic-gate 	((f) & RWST_SIG ? cv_wait_sig(cv, lock) : (cv_wait(cv, lock), 1))
69*7c478bd9Sstevel@tonic-gate #define	RWST_READ_WAIT(l, f)	RWST_WAIT(&(l)->rwst_rcv, &(l)->rwst_lock, f)
70*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_WAIT(l, f)	RWST_WAIT(&(l)->rwst_wcv, &(l)->rwst_lock, f)
71*7c478bd9Sstevel@tonic-gate #define	RWST_READ_WAKE_ALL(l)	cv_broadcast(&(l)->rwst_rcv)
72*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_WAKE_ONE(l)	cv_signal(&(l)->rwst_wcv)
73*7c478bd9Sstevel@tonic-gate #define	RWST_READ_ENTER(l)	(l)->rwst_count++
74*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_ENTER(l)	(l)->rwst_count = LONG_MIN | (intptr_t)curthread
75*7c478bd9Sstevel@tonic-gate #define	RWST_READ_EXIT(l)	(l)->rwst_count--
76*7c478bd9Sstevel@tonic-gate #define	RWST_WRITE_EXIT(l)	(l)->rwst_count = 0
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate extern void rwst_enter(rwstlock_t *, krw_t);
79*7c478bd9Sstevel@tonic-gate extern int rwst_enter_sig(rwstlock_t *, krw_t);
80*7c478bd9Sstevel@tonic-gate extern void rwst_exit(rwstlock_t *);
81*7c478bd9Sstevel@tonic-gate extern void rwst_init(rwstlock_t *, char *, krw_type_t, void *);
82*7c478bd9Sstevel@tonic-gate extern void rwst_destroy(rwstlock_t *);
83*7c478bd9Sstevel@tonic-gate extern int rwst_lock_held(rwstlock_t *, krw_t);
84*7c478bd9Sstevel@tonic-gate extern int rwst_tryenter(rwstlock_t *, krw_t);
85*7c478bd9Sstevel@tonic-gate extern struct _kthread *rwst_owner(rwstlock_t *);
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate #ifdef __cplusplus
88*7c478bd9Sstevel@tonic-gate }
89*7c478bd9Sstevel@tonic-gate #endif
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate #endif /* _SYS_RWSTLOCK_H */
92