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