1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the author nor the names of any co-contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD$ 32 */ 33 34 #ifndef _SYS_RMLOCK_H_ 35 #define _SYS_RMLOCK_H_ 36 37 #include <sys/mutex.h> 38 #include <sys/sx.h> 39 #include <sys/_lock.h> 40 #include <sys/_rmlock.h> 41 42 #ifdef _KERNEL 43 44 /* 45 * Flags passed to rm_init_flags(9). 46 */ 47 #define RM_NOWITNESS 0x00000001 48 #define RM_RECURSE 0x00000002 49 #define RM_SLEEPABLE 0x00000004 50 #define RM_NEW 0x00000008 51 #define RM_DUPOK 0x00000010 52 53 void rm_init(struct rmlock *rm, const char *name); 54 void rm_init_flags(struct rmlock *rm, const char *name, int opts); 55 void rm_destroy(struct rmlock *rm); 56 int rm_wowned(const struct rmlock *rm); 57 void rm_sysinit(void *arg); 58 59 void _rm_wlock_debug(struct rmlock *rm, const char *file, int line); 60 void _rm_wunlock_debug(struct rmlock *rm, const char *file, int line); 61 int _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker, 62 int trylock, const char *file, int line); 63 void _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker, 64 const char *file, int line); 65 66 void _rm_wlock(struct rmlock *rm); 67 void _rm_wunlock(struct rmlock *rm); 68 int _rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker, 69 int trylock); 70 void _rm_runlock(struct rmlock *rm, struct rm_priotracker *tracker); 71 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 72 void _rm_assert(const struct rmlock *rm, int what, const char *file, 73 int line); 74 #endif 75 76 /* 77 * Public interface for lock operations. 78 */ 79 #ifndef LOCK_DEBUG 80 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rmlock.h> 81 #endif 82 83 #if LOCK_DEBUG > 0 84 #define rm_wlock(rm) _rm_wlock_debug((rm), LOCK_FILE, LOCK_LINE) 85 #define rm_wunlock(rm) _rm_wunlock_debug((rm), LOCK_FILE, LOCK_LINE) 86 #define rm_rlock(rm,tracker) \ 87 ((void)_rm_rlock_debug((rm),(tracker), 0, LOCK_FILE, LOCK_LINE )) 88 #define rm_try_rlock(rm,tracker) \ 89 _rm_rlock_debug((rm),(tracker), 1, LOCK_FILE, LOCK_LINE ) 90 #define rm_runlock(rm,tracker) \ 91 _rm_runlock_debug((rm), (tracker), LOCK_FILE, LOCK_LINE ) 92 #else 93 #define rm_wlock(rm) _rm_wlock((rm)) 94 #define rm_wunlock(rm) _rm_wunlock((rm)) 95 #define rm_rlock(rm,tracker) ((void)_rm_rlock((rm),(tracker), 0)) 96 #define rm_try_rlock(rm,tracker) _rm_rlock((rm),(tracker), 1) 97 #define rm_runlock(rm,tracker) _rm_runlock((rm), (tracker)) 98 #endif 99 #define rm_sleep(chan, rm, pri, wmesg, timo) \ 100 _sleep((chan), &(rm)->lock_object, (pri), (wmesg), \ 101 tick_sbt * (timo), 0, C_HARDCLOCK) 102 103 struct rm_args { 104 struct rmlock *ra_rm; 105 const char *ra_desc; 106 int ra_flags; 107 }; 108 109 #define RM_SYSINIT_FLAGS(name, rm, desc, flags) \ 110 static struct rm_args name##_args = { \ 111 (rm), \ 112 (desc), \ 113 (flags), \ 114 }; \ 115 SYSINIT(name##_rm_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 116 rm_sysinit, &name##_args); \ 117 SYSUNINIT(name##_rm_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE, \ 118 rm_destroy, (rm)) 119 120 #define RM_SYSINIT(name, rm, desc) RM_SYSINIT_FLAGS(name, rm, desc, 0) 121 122 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) 123 #define RA_LOCKED LA_LOCKED 124 #define RA_RLOCKED LA_SLOCKED 125 #define RA_WLOCKED LA_XLOCKED 126 #define RA_UNLOCKED LA_UNLOCKED 127 #define RA_RECURSED LA_RECURSED 128 #define RA_NOTRECURSED LA_NOTRECURSED 129 #endif 130 131 #ifdef INVARIANTS 132 #define rm_assert(rm, what) _rm_assert((rm), (what), LOCK_FILE, LOCK_LINE) 133 #else 134 #define rm_assert(rm, what) 135 #endif 136 137 void rms_init(struct rmslock *rms, const char *name); 138 void rms_destroy(struct rmslock *rms); 139 void rms_rlock(struct rmslock *rms); 140 int rms_try_rlock(struct rmslock *rms); 141 void rms_runlock(struct rmslock *rms); 142 void rms_wlock(struct rmslock *rms); 143 void rms_wunlock(struct rmslock *rms); 144 void rms_unlock(struct rmslock *rms); 145 146 static inline int 147 rms_wowned(struct rmslock *rms) 148 { 149 150 return (rms->owner == curthread); 151 } 152 153 #ifdef INVARIANTS 154 #define rms_assert_rlock_ok(x) \ 155 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 156 #else 157 #define rms_assert_rlock_ok(x) 158 #endif 159 160 #ifdef INVARIANTS 161 /* 162 * For assertion purposes. 163 * 164 * Main limitation is that we at best can tell there are readers, but not 165 * whether curthread is one of them. 166 */ 167 static inline int 168 rms_rowned(struct rmslock *rms) 169 { 170 171 return (rms->debug_readers > 0); 172 } 173 174 static inline int 175 rms_owned_any(struct rmslock *rms) 176 { 177 178 if (rms_wowned(rms)) 179 return (1); 180 181 return (rms_rowned(rms)); 182 } 183 #endif 184 185 #endif /* _KERNEL */ 186 #endif /* !_SYS_RMLOCK_H_ */ 187