source: rtems-libbsd/freebsd/sys/sys/rwlock.h @ 62c8ca0

55-freebsd-126-freebsd-12
Last change on this file since 62c8ca0 was 62c8ca0, checked in by Sebastian Huber <sebastian.huber@…>, on 05/18/17 at 07:35:46

Fix INVARIANTS support

  • Property mode set to 100644
File size: 11.1 KB
Line 
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#ifdef __rtems__
43#define RWLOCK_NOINLINE 1
44#endif /* __rtems__ */
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
60#define RW_LOCK_READ            0x01
61#define RW_LOCK_READ_WAITERS    0x02
62#define RW_LOCK_WRITE_WAITERS   0x04
63#define RW_LOCK_WRITE_SPINNER   0x08
64#define RW_LOCK_WRITER_RECURSED 0x10
65#define RW_LOCK_FLAGMASK                                                \
66        (RW_LOCK_READ | RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS |  \
67        RW_LOCK_WRITE_SPINNER | RW_LOCK_WRITER_RECURSED)
68#define RW_LOCK_WAITERS         (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
69
70#define RW_OWNER(x)             ((x) & ~RW_LOCK_FLAGMASK)
71#define RW_READERS_SHIFT        5
72#define RW_READERS(x)           (RW_OWNER((x)) >> RW_READERS_SHIFT)
73#define RW_READERS_LOCK(x)      ((x) << RW_READERS_SHIFT | RW_LOCK_READ)
74#define RW_ONE_READER           (1 << RW_READERS_SHIFT)
75
76#define RW_UNLOCKED             RW_READERS_LOCK(0)
77#define RW_DESTROYED            (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
78
79#ifdef _KERNEL
80
81#define rw_recurse      lock_object.lo_data
82
83#define RW_READ_VALUE(x)        ((x)->rw_lock)
84
85/* Very simple operations on rw_lock. */
86
87/* Try to obtain a write lock once. */
88#define _rw_write_lock(rw, tid)                                         \
89        atomic_cmpset_acq_ptr(&(rw)->rw_lock, RW_UNLOCKED, (tid))
90
91#define _rw_write_lock_fetch(rw, vp, tid)                               \
92        atomic_fcmpset_acq_ptr(&(rw)->rw_lock, vp, (tid))
93
94/* Release a write lock quickly if there are no waiters. */
95#define _rw_write_unlock(rw, tid)                                       \
96        atomic_cmpset_rel_ptr(&(rw)->rw_lock, (tid), RW_UNLOCKED)
97
98/*
99 * Full lock operations that are suitable to be inlined in non-debug
100 * kernels.  If the lock cannot be acquired or released trivially then
101 * the work is deferred to another function.
102 */
103
104#ifndef __rtems__
105/* Acquire a write lock. */
106#define __rw_wlock(rw, tid, file, line) do {                            \
107        uintptr_t _tid = (uintptr_t)(tid);                              \
108        uintptr_t _v = RW_UNLOCKED;                                     \
109                                                                        \
110        if (__predict_false(LOCKSTAT_PROFILE_ENABLED(rw__acquire) ||    \
111            !_rw_write_lock_fetch((rw), &_v, _tid)))                    \
112                _rw_wlock_hard((rw), _v, _tid, (file), (line));         \
113} while (0)
114
115/* Release a write lock. */
116#define __rw_wunlock(rw, tid, file, line) do {                          \
117        uintptr_t _tid = (uintptr_t)(tid);                              \
118                                                                        \
119        if (__predict_false(LOCKSTAT_PROFILE_ENABLED(rw__release) ||    \
120            !_rw_write_unlock((rw), _tid)))                             \
121                _rw_wunlock_hard((rw), _tid, (file), (line));           \
122} while (0)
123#endif /* __rtems__ */
124
125/*
126 * Function prototypes.  Routines that start with _ are not part of the
127 * external API and should not be called directly.  Wrapper macros should
128 * be used instead.
129 */
130#ifndef __rtems__
131void    _rw_init_flags(volatile uintptr_t *c, const char *name, int opts);
132void    _rw_destroy(volatile uintptr_t *c);
133void    rw_sysinit(void *arg);
134void    rw_sysinit_flags(void *arg);
135int     _rw_wowned(const volatile uintptr_t *c);
136void    _rw_wlock_cookie(volatile uintptr_t *c, const char *file, int line);
137int     __rw_try_wlock(volatile uintptr_t *c, const char *file, int line);
138void    _rw_wunlock_cookie(volatile uintptr_t *c, const char *file, int line);
139void    __rw_rlock(volatile uintptr_t *c, const char *file, int line);
140int     __rw_try_rlock(volatile uintptr_t *c, const char *file, int line);
141void    _rw_runlock_cookie(volatile uintptr_t *c, const char *file, int line);
142void    __rw_wlock_hard(volatile uintptr_t *c, uintptr_t v, uintptr_t tid,
143            const char *file, int line);
144void    __rw_wunlock_hard(volatile uintptr_t *c, uintptr_t tid,
145            const char *file, int line);
146int     __rw_try_upgrade(volatile uintptr_t *c, const char *file, int line);
147void    __rw_downgrade(volatile uintptr_t *c, const char *file, int line);
148#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
149void    __rw_assert(const volatile uintptr_t *c, int what, const char *file,
150            int line);
151#endif
152#else /* __rtems__ */
153#define rw_init(rw, n) rw_init_flags(rw, n, 0)
154void    rw_init_flags(struct rwlock *rw, const char *name, int opts);
155void    rw_destroy(struct rwlock *rw);
156void    rw_sysinit(void *arg);
157void    rw_sysinit_flags(void *arg);
158int     rw_wowned(struct rwlock *rw);
159void    _rw_wlock(struct rwlock *rw, const char *file, int line);
160int     _rw_try_wlock(struct rwlock *rw, const char *file, int line);
161void    _rw_wunlock(struct rwlock *rw, const char *file, int line);
162void    _rw_rlock(struct rwlock *rw, const char *file, int line);
163int     _rw_try_rlock(struct rwlock *rw, const char *file, int line);
164void    _rw_runlock(struct rwlock *rw, const char *file, int line);
165int     _rw_try_upgrade(struct rwlock *rw, const char *file, int line);
166void    _rw_downgrade(struct rwlock *rw, const char *file, int line);
167#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
168void    _rw_assert(const struct rwlock *rw, int what, const char *file,
169            int line);
170#endif
171#endif /* __rtems__ */
172
173#ifndef __rtems__
174/*
175 * Top-level macros to provide lock cookie once the actual rwlock is passed.
176 * They will also prevent passing a malformed object to the rwlock KPI by
177 * failing compilation as the rw_lock reserved member will not be found.
178 */
179#define rw_init(rw, n)                                                  \
180        _rw_init_flags(&(rw)->rw_lock, n, 0)
181#define rw_init_flags(rw, n, o)                                         \
182        _rw_init_flags(&(rw)->rw_lock, n, o)
183#define rw_destroy(rw)                                                  \
184        _rw_destroy(&(rw)->rw_lock)
185#define rw_wowned(rw)                                                   \
186        _rw_wowned(&(rw)->rw_lock)
187#define _rw_wlock(rw, f, l)                                             \
188        _rw_wlock_cookie(&(rw)->rw_lock, f, l)
189#define _rw_try_wlock(rw, f, l)                                         \
190        __rw_try_wlock(&(rw)->rw_lock, f, l)
191#define _rw_wunlock(rw, f, l)                                           \
192        _rw_wunlock_cookie(&(rw)->rw_lock, f, l)
193#define _rw_rlock(rw, f, l)                                             \
194        __rw_rlock(&(rw)->rw_lock, f, l)
195#define _rw_try_rlock(rw, f, l)                                         \
196        __rw_try_rlock(&(rw)->rw_lock, f, l)
197#define _rw_runlock(rw, f, l)                                           \
198        _rw_runlock_cookie(&(rw)->rw_lock, f, l)
199#define _rw_wlock_hard(rw, v, t, f, l)                                  \
200        __rw_wlock_hard(&(rw)->rw_lock, v, t, f, l)
201#define _rw_wunlock_hard(rw, t, f, l)                                   \
202        __rw_wunlock_hard(&(rw)->rw_lock, t, f, l)
203#define _rw_try_upgrade(rw, f, l)                                       \
204        __rw_try_upgrade(&(rw)->rw_lock, f, l)
205#define _rw_downgrade(rw, f, l)                                         \
206        __rw_downgrade(&(rw)->rw_lock, f, l)
207#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
208#define _rw_assert(rw, w, f, l)                                         \
209        __rw_assert(&(rw)->rw_lock, w, f, l)
210#endif
211#endif /* __rtems__ */
212
213
214/*
215 * Public interface for lock operations.
216 */
217
218#ifndef LOCK_DEBUG
219#error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rwlock.h>
220#endif
221#if LOCK_DEBUG > 0 || defined(RWLOCK_NOINLINE)
222#define rw_wlock(rw)            _rw_wlock((rw), LOCK_FILE, LOCK_LINE)
223#define rw_wunlock(rw)          _rw_wunlock((rw), LOCK_FILE, LOCK_LINE)
224#else
225#define rw_wlock(rw)                                                    \
226        __rw_wlock((rw), curthread, LOCK_FILE, LOCK_LINE)
227#define rw_wunlock(rw)                                                  \
228        __rw_wunlock((rw), curthread, LOCK_FILE, LOCK_LINE)
229#endif
230#define rw_rlock(rw)            _rw_rlock((rw), LOCK_FILE, LOCK_LINE)
231#define rw_runlock(rw)          _rw_runlock((rw), LOCK_FILE, LOCK_LINE)
232#define rw_try_rlock(rw)        _rw_try_rlock((rw), LOCK_FILE, LOCK_LINE)
233#define rw_try_upgrade(rw)      _rw_try_upgrade((rw), LOCK_FILE, LOCK_LINE)
234#define rw_try_wlock(rw)        _rw_try_wlock((rw), LOCK_FILE, LOCK_LINE)
235#define rw_downgrade(rw)        _rw_downgrade((rw), LOCK_FILE, LOCK_LINE)
236#define rw_unlock(rw)   do {                                            \
237        if (rw_wowned(rw))                                              \
238                rw_wunlock(rw);                                         \
239        else                                                            \
240                rw_runlock(rw);                                         \
241} while (0)
242#define rw_sleep(chan, rw, pri, wmesg, timo)                            \
243        _sleep((chan), &(rw)->lock_object, (pri), (wmesg),              \
244            tick_sbt * (timo), 0, C_HARDCLOCK)
245
246#define rw_initialized(rw)      lock_initialized(&(rw)->lock_object)
247
248struct rw_args {
249        void            *ra_rw;
250        const char      *ra_desc;
251};
252
253struct rw_args_flags {
254        void            *ra_rw;
255        const char      *ra_desc;
256        int             ra_flags;
257};
258
259#define RW_SYSINIT(name, rw, desc)                                      \
260        static struct rw_args name##_args = {                           \
261                (rw),                                                   \
262                (desc),                                                 \
263        };                                                              \
264        SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,        \
265            rw_sysinit, &name##_args);                                  \
266        SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,    \
267            _rw_destroy, __DEVOLATILE(void *, &(rw)->rw_lock))
268
269
270#define RW_SYSINIT_FLAGS(name, rw, desc, flags)                         \
271        static struct rw_args_flags name##_args = {                     \
272                (rw),                                                   \
273                (desc),                                                 \
274                (flags),                                                \
275        };                                                              \
276        SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,        \
277            rw_sysinit_flags, &name##_args);                            \
278        SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,    \
279            _rw_destroy, __DEVOLATILE(void *, &(rw)->rw_lock))
280
281/*
282 * Options passed to rw_init_flags().
283 */
284#define RW_DUPOK        0x01
285#define RW_NOPROFILE    0x02
286#define RW_NOWITNESS    0x04
287#define RW_QUIET        0x08
288#define RW_RECURSE      0x10
289#define RW_NEW          0x20
290
291/*
292 * The INVARIANTS-enabled rw_assert() functionality.
293 *
294 * The constants need to be defined for INVARIANT_SUPPORT infrastructure
295 * support as _rw_assert() itself uses them and the latter implies that
296 * _rw_assert() must build.
297 */
298#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
299#define RA_LOCKED               LA_LOCKED
300#define RA_RLOCKED              LA_SLOCKED
301#define RA_WLOCKED              LA_XLOCKED
302#define RA_UNLOCKED             LA_UNLOCKED
303#define RA_RECURSED             LA_RECURSED
304#define RA_NOTRECURSED          LA_NOTRECURSED
305#endif
306
307#ifdef INVARIANTS
308#define rw_assert(rw, what)     _rw_assert((rw), (what), LOCK_FILE, LOCK_LINE)
309#else
310#define rw_assert(rw, what)
311#endif
312
313#endif /* _KERNEL */
314#endif /* !_SYS_RWLOCK_H_ */
Note: See TracBrowser for help on using the repository browser.