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