xref: /illumos-gate/usr/src/uts/common/sys/rwlock_impl.h (revision dd72704bd9e794056c558153663c739e2012d721)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SYS_RWLOCK_IMPL_H
28 #define	_SYS_RWLOCK_IMPL_H
29 
30 /*
31  * Implementation-private definitions for readers/writer locks.
32  */
33 
34 #ifndef _ASM
35 
36 #include <sys/rwlock.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 typedef struct rwlock_impl {
43 	uintptr_t	rw_wwwh;	/* waiters, write wanted, hold count */
44 } rwlock_impl_t;
45 
46 #endif	/* _ASM */
47 
48 #define	RW_HAS_WAITERS		1
49 #define	RW_WRITE_WANTED		2
50 #define	RW_WRITE_LOCKED		4
51 #define	RW_READ_LOCK		8
52 #define	RW_WRITE_LOCK(thread)	((uintptr_t)(thread) | RW_WRITE_LOCKED)
53 #define	RW_HOLD_COUNT		(-RW_READ_LOCK)
54 #define	RW_HOLD_COUNT_SHIFT	3		/* log2(RW_READ_LOCK) */
55 #define	RW_READ_COUNT		RW_HOLD_COUNT
56 #define	RW_OWNER		RW_HOLD_COUNT
57 #define	RW_LOCKED		RW_HOLD_COUNT
58 #define	RW_WRITE_CLAIMED	(RW_WRITE_LOCKED | RW_WRITE_WANTED)
59 #define	RW_DOUBLE_LOCK		(RW_WRITE_LOCK(0) | RW_READ_LOCK)
60 
61 /*
62  * These macros are used by both the implementation of rw_*() routines and
63  * by the implementation of the rwlock-related DTrace subroutines.  (DTrace
64  * cannot make calls into the rw_*() routines; it must use the macros.)
65  */
66 #define	_RW_READ_HELD(rwlp, tmp)					\
67 	((((tmp) = ((rwlock_impl_t *)(rwlp))->rw_wwwh) & RW_LOCKED) &&	\
68 	!((tmp) & RW_WRITE_LOCKED))
69 
70 #define	_RW_WRITE_HELD(rwlp)						\
71 	((((rwlock_impl_t *)(rwlp))->rw_wwwh &				\
72 	(RW_OWNER | RW_WRITE_LOCKED)) == RW_WRITE_LOCK(curthread))
73 
74 #define	_RW_LOCK_HELD(rwlp)						\
75 	((((rwlock_impl_t *)(rwlp))->rw_wwwh & RW_LOCKED) ? 1 : 0)
76 
77 #define	_RW_ISWRITER(rwlp)						\
78 	((((rwlock_impl_t *)(rwlp))->rw_wwwh & RW_WRITE_CLAIMED) ? 1 : 0)
79 
80 #ifdef	__cplusplus
81 }
82 #endif
83 
84 #endif	/* _SYS_RWLOCK_IMPL_H */
85