1154941Sjhb/*- 2154941Sjhb * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org> 3154941Sjhb * All rights reserved. 4154941Sjhb * 5154941Sjhb * Redistribution and use in source and binary forms, with or without 6154941Sjhb * modification, are permitted provided that the following conditions 7154941Sjhb * are met: 8154941Sjhb * 1. Redistributions of source code must retain the above copyright 9154941Sjhb * notice, this list of conditions and the following disclaimer. 10154941Sjhb * 2. Redistributions in binary form must reproduce the above copyright 11154941Sjhb * notice, this list of conditions and the following disclaimer in the 12154941Sjhb * documentation and/or other materials provided with the distribution. 13154941Sjhb * 14154941Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15154941Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16154941Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17154941Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18154941Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19154941Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20154941Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21154941Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22154941Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23154941Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24154941Sjhb * SUCH DAMAGE. 25154941Sjhb * 26154941Sjhb * $FreeBSD$ 27154941Sjhb */ 28154941Sjhb 29154941Sjhb#ifndef _SYS_RWLOCK_H_ 30154941Sjhb#define _SYS_RWLOCK_H_ 31154941Sjhb 32154941Sjhb#include <sys/_lock.h> 33154941Sjhb#include <sys/_rwlock.h> 34171516Sattilio#include <sys/lock_profile.h> 35192853Ssson#include <sys/lockstat.h> 36154941Sjhb 37154941Sjhb#ifdef _KERNEL 38163320Sglebius#include <sys/pcpu.h> 39154941Sjhb#include <machine/atomic.h> 40154941Sjhb#endif 41154941Sjhb 42154941Sjhb/* 43154941Sjhb * The rw_lock field consists of several fields. The low bit indicates 44154941Sjhb * if the lock is locked with a read (shared) or write (exclusive) lock. 45154941Sjhb * A value of 0 indicates a write lock, and a value of 1 indicates a read 46154941Sjhb * lock. Bit 1 is a boolean indicating if there are any threads waiting 47154941Sjhb * for a read lock. Bit 2 is a boolean indicating if there are any threads 48154941Sjhb * waiting for a write lock. The rest of the variable's definition is 49154941Sjhb * dependent on the value of the first bit. For a write lock, it is a 50154941Sjhb * pointer to the thread holding the lock, similar to the mtx_lock field of 51154941Sjhb * mutexes. For read locks, it is a count of read locks that are held. 52154941Sjhb * 53154941Sjhb * When the lock is not locked by any thread, it is encoded as a read lock 54154941Sjhb * with zero waiters. 55154941Sjhb */ 56154941Sjhb 57154941Sjhb#define RW_LOCK_READ 0x01 58154941Sjhb#define RW_LOCK_READ_WAITERS 0x02 59154941Sjhb#define RW_LOCK_WRITE_WAITERS 0x04 60176017Sjeff#define RW_LOCK_WRITE_SPINNER 0x08 61154941Sjhb#define RW_LOCK_FLAGMASK \ 62171052Sattilio (RW_LOCK_READ | RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS | \ 63176017Sjeff RW_LOCK_WRITE_SPINNER) 64176017Sjeff#define RW_LOCK_WAITERS (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS) 65154941Sjhb 66154941Sjhb#define RW_OWNER(x) ((x) & ~RW_LOCK_FLAGMASK) 67171052Sattilio#define RW_READERS_SHIFT 4 68154941Sjhb#define RW_READERS(x) (RW_OWNER((x)) >> RW_READERS_SHIFT) 69154941Sjhb#define RW_READERS_LOCK(x) ((x) << RW_READERS_SHIFT | RW_LOCK_READ) 70154941Sjhb#define RW_ONE_READER (1 << RW_READERS_SHIFT) 71154941Sjhb 72154941Sjhb#define RW_UNLOCKED RW_READERS_LOCK(0) 73169394Sjhb#define RW_DESTROYED (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS) 74154941Sjhb 75154941Sjhb#ifdef _KERNEL 76154941Sjhb 77179025Sattilio#define rw_recurse lock_object.lo_data 78179025Sattilio 79154941Sjhb/* Very simple operations on rw_lock. */ 80154941Sjhb 81154941Sjhb/* Try to obtain a write lock once. */ 82154941Sjhb#define _rw_write_lock(rw, tid) \ 83154941Sjhb atomic_cmpset_acq_ptr(&(rw)->rw_lock, RW_UNLOCKED, (tid)) 84154941Sjhb 85154941Sjhb/* Release a write lock quickly if there are no waiters. */ 86154941Sjhb#define _rw_write_unlock(rw, tid) \ 87154941Sjhb atomic_cmpset_rel_ptr(&(rw)->rw_lock, (tid), RW_UNLOCKED) 88154941Sjhb 89154941Sjhb/* 90154941Sjhb * Full lock operations that are suitable to be inlined in non-debug 91154941Sjhb * kernels. If the lock cannot be acquired or released trivially then 92154941Sjhb * the work is deferred to another function. 93154941Sjhb */ 94154941Sjhb 95154941Sjhb/* Acquire a write lock. */ 96171516Sattilio#define __rw_wlock(rw, tid, file, line) do { \ 97154941Sjhb uintptr_t _tid = (uintptr_t)(tid); \ 98167012Skmacy \ 99171516Sattilio if (!_rw_write_lock((rw), _tid)) \ 100154941Sjhb _rw_wlock_hard((rw), _tid, (file), (line)); \ 101192853Ssson else \ 102192853Ssson LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_WLOCK_ACQUIRE, \ 103192853Ssson rw, 0, 0, (file), (line)); \ 104154941Sjhb} while (0) 105154941Sjhb 106154941Sjhb/* Release a write lock. */ 107154941Sjhb#define __rw_wunlock(rw, tid, file, line) do { \ 108154941Sjhb uintptr_t _tid = (uintptr_t)(tid); \ 109154941Sjhb \ 110176017Sjeff if ((rw)->rw_recurse) \ 111176017Sjeff (rw)->rw_recurse--; \ 112176017Sjeff else if (!_rw_write_unlock((rw), _tid)) \ 113154941Sjhb _rw_wunlock_hard((rw), _tid, (file), (line)); \ 114154941Sjhb} while (0) 115154941Sjhb 116154941Sjhb/* 117154941Sjhb * Function prototypes. Routines that start with _ are not part of the 118154941Sjhb * external API and should not be called directly. Wrapper macros should 119154941Sjhb * be used instead. 120154941Sjhb */ 121154941Sjhb 122171052Sattilio#define rw_init(rw, name) rw_init_flags((rw), (name), 0) 123171052Sattiliovoid rw_init_flags(struct rwlock *rw, const char *name, int opts); 124154941Sjhbvoid rw_destroy(struct rwlock *rw); 125154941Sjhbvoid rw_sysinit(void *arg); 126185778Skmacyvoid rw_sysinit_flags(void *arg); 127167024Srwatsonint rw_wowned(struct rwlock *rw); 128154941Sjhbvoid _rw_wlock(struct rwlock *rw, const char *file, int line); 129177843Sattilioint _rw_try_wlock(struct rwlock *rw, const char *file, int line); 130154941Sjhbvoid _rw_wunlock(struct rwlock *rw, const char *file, int line); 131154941Sjhbvoid _rw_rlock(struct rwlock *rw, const char *file, int line); 132177843Sattilioint _rw_try_rlock(struct rwlock *rw, const char *file, int line); 133154941Sjhbvoid _rw_runlock(struct rwlock *rw, const char *file, int line); 134154941Sjhbvoid _rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, 135154941Sjhb int line); 136154941Sjhbvoid _rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, 137154941Sjhb int line); 138157882Sjhbint _rw_try_upgrade(struct rwlock *rw, const char *file, int line); 139157882Sjhbvoid _rw_downgrade(struct rwlock *rw, const char *file, int line); 140154941Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 141154941Sjhbvoid _rw_assert(struct rwlock *rw, int what, const char *file, int line); 142154941Sjhb#endif 143154941Sjhb 144154941Sjhb/* 145154941Sjhb * Public interface for lock operations. 146154941Sjhb */ 147154941Sjhb 148154941Sjhb#ifndef LOCK_DEBUG 149154941Sjhb#error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rwlock.h> 150154941Sjhb#endif 151154941Sjhb#if LOCK_DEBUG > 0 || defined(RWLOCK_NOINLINE) 152154941Sjhb#define rw_wlock(rw) _rw_wlock((rw), LOCK_FILE, LOCK_LINE) 153154941Sjhb#define rw_wunlock(rw) _rw_wunlock((rw), LOCK_FILE, LOCK_LINE) 154154941Sjhb#else 155154941Sjhb#define rw_wlock(rw) \ 156154941Sjhb __rw_wlock((rw), curthread, LOCK_FILE, LOCK_LINE) 157154941Sjhb#define rw_wunlock(rw) \ 158154941Sjhb __rw_wunlock((rw), curthread, LOCK_FILE, LOCK_LINE) 159154941Sjhb#endif 160154941Sjhb#define rw_rlock(rw) _rw_rlock((rw), LOCK_FILE, LOCK_LINE) 161154941Sjhb#define rw_runlock(rw) _rw_runlock((rw), LOCK_FILE, LOCK_LINE) 162177843Sattilio#define rw_try_rlock(rw) _rw_try_rlock((rw), LOCK_FILE, LOCK_LINE) 163157882Sjhb#define rw_try_upgrade(rw) _rw_try_upgrade((rw), LOCK_FILE, LOCK_LINE) 164177843Sattilio#define rw_try_wlock(rw) _rw_try_wlock((rw), LOCK_FILE, LOCK_LINE) 165157882Sjhb#define rw_downgrade(rw) _rw_downgrade((rw), LOCK_FILE, LOCK_LINE) 166177260Spjd#define rw_unlock(rw) do { \ 167177260Spjd if (rw_wowned(rw)) \ 168177260Spjd rw_wunlock(rw); \ 169177260Spjd else \ 170177260Spjd rw_runlock(rw); \ 171177260Spjd} while (0) 172167387Sjhb#define rw_sleep(chan, rw, pri, wmesg, timo) \ 173167787Sjhb _sleep((chan), &(rw)->lock_object, (pri), (wmesg), (timo)) 174154941Sjhb 175167787Sjhb#define rw_initialized(rw) lock_initalized(&(rw)->lock_object) 176154941Sjhb 177154941Sjhbstruct rw_args { 178154941Sjhb struct rwlock *ra_rw; 179154941Sjhb const char *ra_desc; 180154941Sjhb}; 181154941Sjhb 182185778Skmacystruct rw_args_flags { 183185778Skmacy struct rwlock *ra_rw; 184185778Skmacy const char *ra_desc; 185185778Skmacy int ra_flags; 186185778Skmacy}; 187185778Skmacy 188154941Sjhb#define RW_SYSINIT(name, rw, desc) \ 189154941Sjhb static struct rw_args name##_args = { \ 190154941Sjhb (rw), \ 191154941Sjhb (desc), \ 192154941Sjhb }; \ 193154941Sjhb SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 194154941Sjhb rw_sysinit, &name##_args); \ 195159897Sjhb SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 196154941Sjhb rw_destroy, (rw)) 197154941Sjhb 198185778Skmacy 199185778Skmacy#define RW_SYSINIT_FLAGS(name, rw, desc, flags) \ 200185778Skmacy static struct rw_args_flags name##_args = { \ 201185778Skmacy (rw), \ 202185778Skmacy (desc), \ 203185778Skmacy (flags), \ 204185778Skmacy }; \ 205185778Skmacy SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 206185778Skmacy rw_sysinit_flags, &name##_args); \ 207185778Skmacy SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 208185778Skmacy rw_destroy, (rw)) 209185778Skmacy 210154941Sjhb/* 211171052Sattilio * Options passed to rw_init_flags(). 212171052Sattilio */ 213171052Sattilio#define RW_DUPOK 0x01 214171052Sattilio#define RW_NOPROFILE 0x02 215171052Sattilio#define RW_NOWITNESS 0x04 216171052Sattilio#define RW_QUIET 0x08 217171052Sattilio#define RW_RECURSE 0x10 218171052Sattilio 219171052Sattilio/* 220154941Sjhb * The INVARIANTS-enabled rw_assert() functionality. 221154941Sjhb * 222154941Sjhb * The constants need to be defined for INVARIANT_SUPPORT infrastructure 223154941Sjhb * support as _rw_assert() itself uses them and the latter implies that 224154941Sjhb * _rw_assert() must build. 225154941Sjhb */ 226154941Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 227154941Sjhb#define RA_LOCKED LA_LOCKED 228154941Sjhb#define RA_RLOCKED LA_SLOCKED 229154941Sjhb#define RA_WLOCKED LA_XLOCKED 230154941Sjhb#define RA_UNLOCKED LA_UNLOCKED 231171052Sattilio#define RA_RECURSED LA_RECURSED 232171052Sattilio#define RA_NOTRECURSED LA_NOTRECURSED 233154941Sjhb#endif 234154941Sjhb 235154941Sjhb#ifdef INVARIANTS 236155042Sglebius#define rw_assert(rw, what) _rw_assert((rw), (what), LOCK_FILE, LOCK_LINE) 237154941Sjhb#else 238154941Sjhb#define rw_assert(rw, what) 239154941Sjhb#endif 240154941Sjhb 241154941Sjhb#endif /* _KERNEL */ 242154941Sjhb#endif /* !_SYS_RWLOCK_H_ */ 243