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