2 * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
32 #ifndef _SYS_RWLOCK_H_
33 #define _SYS_RWLOCK_H_
35 #include <sys/_lock.h>
36 #include <sys/_rwlock.h>
40 #include <machine/atomic.h>
44 * The rw_lock field consists of several fields. The low bit indicates
45 * if the lock is locked with a read (shared) or write (exclusive) lock.
46 * A value of 0 indicates a write lock, and a value of 1 indicates a read
47 * lock. Bit 1 is a boolean indicating if there are any threads waiting
48 * for a read lock. Bit 2 is a boolean indicating if there are any threads
49 * waiting for a write lock. The rest of the variable's definition is
50 * dependent on the value of the first bit. For a write lock, it is a
51 * pointer to the thread holding the lock, similar to the mtx_lock field of
52 * mutexes. For read locks, it is a count of read locks that are held.
54 * When the lock is not locked by any thread, it is encoded as a read lock
57 * A note about memory barriers. Write locks need to use the same memory
58 * barriers as mutexes: _acq when acquiring a write lock and _rel when
59 * releasing a write lock. Read locks also need to use an _acq barrier when
60 * acquiring a read lock. However, since read locks do not update any
61 * locked data (modulo bugs of course), no memory barrier is needed when
62 * releasing a read lock.
65 #define RW_LOCK_READ 0x01
66 #define RW_LOCK_READ_WAITERS 0x02
67 #define RW_LOCK_WRITE_WAITERS 0x04
68 #define RW_LOCK_FLAGMASK \
69 (RW_LOCK_READ | RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
71 #define RW_OWNER(x) ((x) & ~RW_LOCK_FLAGMASK)
72 #define RW_READERS_SHIFT 3
73 #define RW_READERS(x) (RW_OWNER((x)) >> RW_READERS_SHIFT)
74 #define RW_READERS_LOCK(x) ((x) << RW_READERS_SHIFT | RW_LOCK_READ)
75 #define RW_ONE_READER (1 << RW_READERS_SHIFT)
77 #define RW_UNLOCKED RW_READERS_LOCK(0)
78 #define RW_DESTROYED (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
82 /* Very simple operations on rw_lock. */
84 /* Try to obtain a write lock once. */
85 #define _rw_write_lock(rw, tid) \
86 atomic_cmpset_acq_ptr(&(rw)->rw_lock, RW_UNLOCKED, (tid))
88 /* Release a write lock quickly if there are no waiters. */
89 #define _rw_write_unlock(rw, tid) \
90 atomic_cmpset_rel_ptr(&(rw)->rw_lock, (tid), RW_UNLOCKED)
93 * Full lock operations that are suitable to be inlined in non-debug
94 * kernels. If the lock cannot be acquired or released trivially then
95 * the work is deferred to another function.
98 /* Acquire a write lock. */
99 #define __rw_wlock(rw, tid, file, line) do { \
100 uintptr_t _tid = (uintptr_t)(tid); \
102 uint64_t waitstart = 0; \
104 if (!_rw_write_lock((rw), _tid)) { \
105 lock_profile_obtain_lock_failed(&(rw)->lock_object, \
106 &contested, &waitstart); \
107 _rw_wlock_hard((rw), _tid, (file), (line)); \
109 lock_profile_obtain_lock_success(&(rw)->lock_object, contested, \
110 waitstart, (file), (line)); \
113 /* Release a write lock. */
114 #define __rw_wunlock(rw, tid, file, line) do { \
115 uintptr_t _tid = (uintptr_t)(tid); \
117 if (!_rw_write_unlock((rw), _tid)) \
118 _rw_wunlock_hard((rw), _tid, (file), (line)); \
122 * Function prototypes. Routines that start with _ are not part of the
123 * external API and should not be called directly. Wrapper macros should
127 void rw_init(struct rwlock *rw, const char *name);
128 void rw_destroy(struct rwlock *rw);
129 void rw_sysinit(void *arg);
130 int rw_wowned(struct rwlock *rw);
131 void _rw_wlock(struct rwlock *rw, const char *file, int line);
132 void _rw_wunlock(struct rwlock *rw, const char *file, int line);
133 void _rw_rlock(struct rwlock *rw, const char *file, int line);
134 void _rw_runlock(struct rwlock *rw, const char *file, int line);
135 void _rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file,
137 void _rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file,
139 int _rw_try_upgrade(struct rwlock *rw, const char *file, int line);
140 void _rw_downgrade(struct rwlock *rw, const char *file, int line);
141 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
142 void _rw_assert(struct rwlock *rw, int what, const char *file, int line);
146 * Public interface for lock operations.
148 * XXX: Missing try locks.
152 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rwlock.h>
154 #if LOCK_DEBUG > 0 || defined(RWLOCK_NOINLINE)
155 #define rw_wlock(rw) _rw_wlock((rw), LOCK_FILE, LOCK_LINE)
156 #define rw_wunlock(rw) _rw_wunlock((rw), LOCK_FILE, LOCK_LINE)
158 #define rw_wlock(rw) \
159 __rw_wlock((rw), curthread, LOCK_FILE, LOCK_LINE)
160 #define rw_wunlock(rw) \
161 __rw_wunlock((rw), curthread, LOCK_FILE, LOCK_LINE)
163 #define rw_rlock(rw) _rw_rlock((rw), LOCK_FILE, LOCK_LINE)
164 #define rw_runlock(rw) _rw_runlock((rw), LOCK_FILE, LOCK_LINE)
165 #define rw_try_upgrade(rw) _rw_try_upgrade((rw), LOCK_FILE, LOCK_LINE)
166 #define rw_downgrade(rw) _rw_downgrade((rw), LOCK_FILE, LOCK_LINE)
167 #define rw_sleep(chan, rw, pri, wmesg, timo) \
168 _sleep((chan), &(rw)->lock_object, (pri), (wmesg), (timo))
170 #define rw_initialized(rw) lock_initalized(&(rw)->lock_object)
173 struct rwlock *ra_rw;
177 #define RW_SYSINIT(name, rw, desc) \
178 static struct rw_args name##_args = { \
182 SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \
183 rw_sysinit, &name##_args); \
184 SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \
188 * The INVARIANTS-enabled rw_assert() functionality.
190 * The constants need to be defined for INVARIANT_SUPPORT infrastructure
191 * support as _rw_assert() itself uses them and the latter implies that
192 * _rw_assert() must build.
194 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
195 #define RA_LOCKED LA_LOCKED
196 #define RA_RLOCKED LA_SLOCKED
197 #define RA_WLOCKED LA_XLOCKED
198 #define RA_UNLOCKED LA_UNLOCKED
202 #define rw_assert(rw, what) _rw_assert((rw), (what), LOCK_FILE, LOCK_LINE)
204 #define rw_assert(rw, what)
208 #endif /* !_SYS_RWLOCK_H_ */