xref: /minix3/sys/sys/rwlock.h (revision 7f2d47d84ca758d43a9bd9c1a1fefc2c1bcb3375)
1 /*	$NetBSD: rwlock.h,v 1.10 2010/02/08 09:54:27 skrll Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe and Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _SYS_RWLOCK_H_
33 #define	_SYS_RWLOCK_H_
34 
35 /*
36  * The rwlock provides exclusive access when held as a "writer",
37  * and shared access when held as a "reader".
38  *
39  * Architectures may optionally provide stubs for the following functions to
40  * implement the easy (unlocked, no waiters) cases.  If these stubs are
41  * provided, __HAVE_RW_STUBS should be defined.
42  *
43  *	rw_enter()
44  *	rw_exit()
45  *	rw_tryenter()
46  */
47 
48 #if defined(_KERNEL_OPT)
49 #include "opt_lockdebug.h"
50 #endif
51 
52 #if !defined(_KERNEL)
53 #include <sys/types.h>
54 #include <sys/inttypes.h>
55 #endif
56 
57 typedef enum krw_t {
58 	RW_READER = 0,
59 	RW_WRITER = 1
60 } krw_t;
61 
62 typedef struct krwlock krwlock_t;
63 
64 #ifdef __RWLOCK_PRIVATE
65 /*
66  * Bits in the owner field of the lock that indicate lock state.  If the
67  * WRITE_LOCKED bit is clear, then the owner field is actually a count of
68  * the number of readers.  The rw_owner field is laid out like so:
69  *
70  *	 N                    4        3        2        1        0
71  *	+---------------------------------------------------------+
72  *	| owner or read count | nodbug | wrlock | wrwant |  wait  |
73  *	+---------------------------------------------------------+
74  */
75 #define	RW_HAS_WAITERS		0x01UL	/* lock has waiters */
76 #define	RW_WRITE_WANTED		0x02UL	/* >= 1 waiter is a writer */
77 #define	RW_WRITE_LOCKED		0x04UL	/* lock is currently write locked */
78 #if defined(LOCKDEBUG)
79 #define	RW_NODEBUG		0x08UL	/* LOCKDEBUG disabled */
80 #else
81 #define	RW_NODEBUG		0x00UL	/* do nothing */
82 #endif	/* LOCKDEBUG */
83 
84 #define	RW_READ_COUNT_SHIFT	4
85 #define	RW_READ_INCR		(1UL << RW_READ_COUNT_SHIFT)
86 #define	RW_THREAD		((uintptr_t)-RW_READ_INCR)
87 #define	RW_OWNER(rw)		((rw)->rw_owner & RW_THREAD)
88 #define	RW_COUNT(rw)		((rw)->rw_owner & RW_THREAD)
89 #define	RW_FLAGS(rw)		((rw)->rw_owner & ~RW_THREAD)
90 
91 void	rw_vector_enter(krwlock_t *, const krw_t);
92 void	rw_vector_exit(krwlock_t *);
93 int	rw_vector_tryenter(krwlock_t *, const krw_t);
94 #endif	/* __RWLOCK_PRIVATE */
95 
96 #include <machine/rwlock.h>
97 
98 #ifdef _KERNEL
99 
100 void	rw_init(krwlock_t *);
101 void	rw_destroy(krwlock_t *);
102 
103 int	rw_tryenter(krwlock_t *, const krw_t);
104 int	rw_tryupgrade(krwlock_t *);
105 void	rw_downgrade(krwlock_t *);
106 
107 int	rw_read_held(krwlock_t *);
108 int	rw_write_held(krwlock_t *);
109 int	rw_lock_held(krwlock_t *);
110 
111 void	rw_enter(krwlock_t *, const krw_t);
112 void	rw_exit(krwlock_t *);
113 
114 void	rw_obj_init(void);
115 krwlock_t *rw_obj_alloc(void);
116 void	rw_obj_hold(krwlock_t *);
117 bool	rw_obj_free(krwlock_t *);
118 
119 #endif	/* _KERNEL */
120 
121 #endif /* _SYS_RWLOCK_H_ */
122