1/*- 2 * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice(s), this list of conditions and the following disclaimer as --- 20 unchanged lines hidden (view full) --- 29 * Shared/exclusive locks. This implementation assures deterministic lock 30 * granting behavior, so that slocks and xlocks are interleaved. 31 * 32 * Priority propagation will not generally raise the priority of lock holders, 33 * so should not be relied upon in combination with sx locks. 34 */ 35 36#include <sys/cdefs.h> |
37__FBSDID("$FreeBSD: head/sys/kern/kern_sx.c 160771 2006-07-27 21:45:55Z jhb $"); |
38 39#include "opt_ddb.h" 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/ktr.h> 44#include <sys/linker_set.h> 45#include <sys/condvar.h> --- 77 unchanged lines hidden (view full) --- 123 sx->sx_shrd_wcnt--; 124 } 125 126 /* Acquire a shared lock. */ 127 sx->sx_cnt++; 128 129 LOCK_LOG_LOCK("SLOCK", &sx->sx_object, 0, 0, file, line); 130 WITNESS_LOCK(&sx->sx_object, 0, file, line); |
131 curthread->td_locks++; |
132 133 mtx_unlock(sx->sx_lock); 134} 135 136int 137_sx_try_slock(struct sx *sx, const char *file, int line) 138{ 139 140 mtx_lock(sx->sx_lock); 141 if (sx->sx_cnt >= 0) { 142 sx->sx_cnt++; 143 LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 1, file, line); 144 WITNESS_LOCK(&sx->sx_object, LOP_TRYLOCK, file, line); |
145 curthread->td_locks++; |
146 mtx_unlock(sx->sx_lock); 147 return (1); 148 } else { 149 LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 0, file, line); 150 mtx_unlock(sx->sx_lock); 151 return (0); 152 } 153} --- 27 unchanged lines hidden (view full) --- 181 MPASS(sx->sx_cnt == 0); 182 183 /* Acquire an exclusive lock. */ 184 sx->sx_cnt--; 185 sx->sx_xholder = curthread; 186 187 LOCK_LOG_LOCK("XLOCK", &sx->sx_object, 0, 0, file, line); 188 WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line); |
189 curthread->td_locks++; |
190 191 mtx_unlock(sx->sx_lock); 192} 193 194int 195_sx_try_xlock(struct sx *sx, const char *file, int line) 196{ 197 198 mtx_lock(sx->sx_lock); 199 if (sx->sx_cnt == 0) { 200 sx->sx_cnt--; 201 sx->sx_xholder = curthread; 202 LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 1, file, line); 203 WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK, file, 204 line); |
205 curthread->td_locks++; |
206 mtx_unlock(sx->sx_lock); 207 return (1); 208 } else { 209 LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 0, file, line); 210 mtx_unlock(sx->sx_lock); 211 return (0); 212 } 213} 214 215void 216_sx_sunlock(struct sx *sx, const char *file, int line) 217{ 218 219 _sx_assert(sx, SX_SLOCKED, file, line); 220 mtx_lock(sx->sx_lock); 221 |
222 curthread->td_locks--; |
223 WITNESS_UNLOCK(&sx->sx_object, 0, file, line); 224 225 /* Release. */ 226 sx->sx_cnt--; 227 228 /* 229 * If we just released the last shared lock, wake any waiters up, giving 230 * exclusive lockers precedence. In order to make sure that exclusive --- 14 unchanged lines hidden (view full) --- 245void 246_sx_xunlock(struct sx *sx, const char *file, int line) 247{ 248 249 _sx_assert(sx, SX_XLOCKED, file, line); 250 mtx_lock(sx->sx_lock); 251 MPASS(sx->sx_cnt == -1); 252 |
253 curthread->td_locks--; |
254 WITNESS_UNLOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line); 255 256 /* Release. */ 257 sx->sx_cnt++; 258 sx->sx_xholder = NULL; 259 260 /* 261 * Wake up waiters if there are any. Give precedence to slock waiters. --- 137 unchanged lines hidden --- |