]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/sys/rwlock.h
MFC r363988:
[FreeBSD/stable/9.git] / sys / sys / rwlock.h
1 /*-
2  * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #ifndef _SYS_RWLOCK_H_
30 #define _SYS_RWLOCK_H_
31
32 #include <sys/_lock.h>
33 #include <sys/_rwlock.h>
34 #include <sys/lock_profile.h>
35 #include <sys/lockstat.h>
36
37 #ifdef _KERNEL
38 #include <sys/pcpu.h>
39 #include <machine/atomic.h>
40 #endif
41
42 /*
43  * The rw_lock field consists of several fields.  The low bit indicates
44  * if the lock is locked with a read (shared) or write (exclusive) lock.
45  * A value of 0 indicates a write lock, and a value of 1 indicates a read
46  * lock.  Bit 1 is a boolean indicating if there are any threads waiting
47  * for a read lock.  Bit 2 is a boolean indicating if there are any threads
48  * waiting for a write lock.  The rest of the variable's definition is
49  * dependent on the value of the first bit.  For a write lock, it is a
50  * pointer to the thread holding the lock, similar to the mtx_lock field of
51  * mutexes.  For read locks, it is a count of read locks that are held.
52  *
53  * When the lock is not locked by any thread, it is encoded as a read lock
54  * with zero waiters.
55  */
56
57 #define RW_LOCK_READ            0x01
58 #define RW_LOCK_READ_WAITERS    0x02
59 #define RW_LOCK_WRITE_WAITERS   0x04
60 #define RW_LOCK_WRITE_SPINNER   0x08
61 #define RW_LOCK_FLAGMASK                                                \
62         (RW_LOCK_READ | RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS |  \
63         RW_LOCK_WRITE_SPINNER)
64 #define RW_LOCK_WAITERS         (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
65
66 #define RW_OWNER(x)             ((x) & ~RW_LOCK_FLAGMASK)
67 #define RW_READERS_SHIFT        4
68 #define RW_READERS(x)           (RW_OWNER((x)) >> RW_READERS_SHIFT)
69 #define RW_READERS_LOCK(x)      ((x) << RW_READERS_SHIFT | RW_LOCK_READ)
70 #define RW_ONE_READER           (1 << RW_READERS_SHIFT)
71
72 #define RW_UNLOCKED             RW_READERS_LOCK(0)
73 #define RW_DESTROYED            (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
74
75 #ifdef _KERNEL
76
77 #define rw_recurse      lock_object.lo_data
78
79 /* Very simple operations on rw_lock. */
80
81 /* Try to obtain a write lock once. */
82 #define _rw_write_lock(rw, tid)                                         \
83         atomic_cmpset_acq_ptr(&(rw)->rw_lock, RW_UNLOCKED, (tid))
84
85 /* Release a write lock quickly if there are no waiters. */
86 #define _rw_write_unlock(rw, tid)                                       \
87         atomic_cmpset_rel_ptr(&(rw)->rw_lock, (tid), RW_UNLOCKED)
88
89 /*
90  * Full lock operations that are suitable to be inlined in non-debug
91  * kernels.  If the lock cannot be acquired or released trivially then
92  * the work is deferred to another function.
93  */
94
95 /* Acquire a write lock. */
96 #define __rw_wlock(rw, tid, file, line) do {                            \
97         uintptr_t _tid = (uintptr_t)(tid);                              \
98                                                                         \
99         if (!_rw_write_lock((rw), _tid))                                \
100                 _rw_wlock_hard((rw), _tid, (file), (line));             \
101         else                                                            \
102                 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_WLOCK_ACQUIRE, \
103                     rw, 0, 0, (file), (line));                          \
104 } while (0)
105
106 /* Release a write lock. */
107 #define __rw_wunlock(rw, tid, file, line) do {                          \
108         uintptr_t _tid = (uintptr_t)(tid);                              \
109                                                                         \
110         if ((rw)->rw_recurse)                                           \
111                 (rw)->rw_recurse--;                                     \
112         else if (!_rw_write_unlock((rw), _tid))                         \
113                 _rw_wunlock_hard((rw), _tid, (file), (line));           \
114 } while (0)
115
116 /*
117  * Function prototypes.  Routines that start with _ are not part of the
118  * external API and should not be called directly.  Wrapper macros should
119  * be used instead.
120  */
121
122 #define rw_init(rw, name)       rw_init_flags((rw), (name), 0)
123 void    rw_init_flags(struct rwlock *rw, const char *name, int opts);
124 void    rw_destroy(struct rwlock *rw);
125 void    rw_sysinit(void *arg);
126 void    rw_sysinit_flags(void *arg);
127 int     rw_wowned(struct rwlock *rw);
128 void    _rw_wlock(struct rwlock *rw, const char *file, int line);
129 int     _rw_try_wlock(struct rwlock *rw, const char *file, int line);
130 void    _rw_wunlock(struct rwlock *rw, const char *file, int line);
131 void    _rw_rlock(struct rwlock *rw, const char *file, int line);
132 int     _rw_try_rlock(struct rwlock *rw, const char *file, int line);
133 void    _rw_runlock(struct rwlock *rw, const char *file, int line);
134 void    _rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file,
135             int line);
136 void    _rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file,
137             int line);
138 int     _rw_try_upgrade(struct rwlock *rw, const char *file, int line);
139 void    _rw_downgrade(struct rwlock *rw, const char *file, int line);
140 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
141 void    _rw_assert(struct rwlock *rw, int what, const char *file, int line);
142 #endif
143
144 /*
145  * Public interface for lock operations.
146  */
147
148 #ifndef LOCK_DEBUG
149 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rwlock.h>
150 #endif
151 #if LOCK_DEBUG > 0 || defined(RWLOCK_NOINLINE)
152 #define rw_wlock(rw)            _rw_wlock((rw), LOCK_FILE, LOCK_LINE)
153 #define rw_wunlock(rw)          _rw_wunlock((rw), LOCK_FILE, LOCK_LINE)
154 #else
155 #define rw_wlock(rw)                                                    \
156         __rw_wlock((rw), curthread, LOCK_FILE, LOCK_LINE)
157 #define rw_wunlock(rw)                                                  \
158         __rw_wunlock((rw), curthread, LOCK_FILE, LOCK_LINE)
159 #endif
160 #define rw_rlock(rw)            _rw_rlock((rw), LOCK_FILE, LOCK_LINE)
161 #define rw_runlock(rw)          _rw_runlock((rw), LOCK_FILE, LOCK_LINE)
162 #define rw_try_rlock(rw)        _rw_try_rlock((rw), LOCK_FILE, LOCK_LINE)
163 #define rw_try_upgrade(rw)      _rw_try_upgrade((rw), LOCK_FILE, LOCK_LINE)
164 #define rw_try_wlock(rw)        _rw_try_wlock((rw), LOCK_FILE, LOCK_LINE)
165 #define rw_downgrade(rw)        _rw_downgrade((rw), LOCK_FILE, LOCK_LINE)
166 #define rw_unlock(rw)   do {                                            \
167         if (rw_wowned(rw))                                              \
168                 rw_wunlock(rw);                                         \
169         else                                                            \
170                 rw_runlock(rw);                                         \
171 } while (0)
172 #define rw_sleep(chan, rw, pri, wmesg, timo)                            \
173         _sleep((chan), &(rw)->lock_object, (pri), (wmesg), (timo))
174
175 #define rw_initialized(rw)      lock_initalized(&(rw)->lock_object)
176
177 struct rw_args {
178         struct rwlock   *ra_rw;
179         const char      *ra_desc;
180 };
181
182 struct rw_args_flags {
183         struct rwlock   *ra_rw;
184         const char      *ra_desc;
185         int             ra_flags;
186 };
187
188 #define RW_SYSINIT(name, rw, desc)                                      \
189         static struct rw_args name##_args = {                           \
190                 (rw),                                                   \
191                 (desc),                                                 \
192         };                                                              \
193         SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,        \
194             rw_sysinit, &name##_args);                                  \
195         SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,    \
196             rw_destroy, (rw))
197
198
199 #define RW_SYSINIT_FLAGS(name, rw, desc, flags)                         \
200         static struct rw_args_flags name##_args = {                     \
201                 (rw),                                                   \
202                 (desc),                                                 \
203                 (flags),                                                \
204         };                                                              \
205         SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,        \
206             rw_sysinit_flags, &name##_args);                            \
207         SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,    \
208             rw_destroy, (rw))
209
210 /*
211  * Options passed to rw_init_flags().
212  */
213 #define RW_DUPOK        0x01
214 #define RW_NOPROFILE    0x02
215 #define RW_NOWITNESS    0x04
216 #define RW_QUIET        0x08
217 #define RW_RECURSE      0x10
218
219 /*
220  * The INVARIANTS-enabled rw_assert() functionality.
221  *
222  * The constants need to be defined for INVARIANT_SUPPORT infrastructure
223  * support as _rw_assert() itself uses them and the latter implies that
224  * _rw_assert() must build.
225  */
226 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
227 #define RA_LOCKED               LA_LOCKED
228 #define RA_RLOCKED              LA_SLOCKED
229 #define RA_WLOCKED              LA_XLOCKED
230 #define RA_UNLOCKED             LA_UNLOCKED
231 #define RA_RECURSED             LA_RECURSED
232 #define RA_NOTRECURSED          LA_NOTRECURSED
233 #endif
234
235 #ifdef INVARIANTS
236 #define rw_assert(rw, what)     _rw_assert((rw), (what), LOCK_FILE, LOCK_LINE)
237 #else
238 #define rw_assert(rw, what)
239 #endif
240
241 #endif /* _KERNEL */
242 #endif /* !_SYS_RWLOCK_H_ */