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 154484 2006-01-17 16:55:17Z 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
132 mtx_unlock(sx->sx_lock);
133}
134
135int
136_sx_try_slock(struct sx *sx, const char *file, int line)
137{
138
139 mtx_lock(sx->sx_lock);
140 if (sx->sx_cnt >= 0) {
141 sx->sx_cnt++;
142 LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 1, file, line);
143 WITNESS_LOCK(&sx->sx_object, LOP_TRYLOCK, file, line);
144 mtx_unlock(sx->sx_lock);
145 return (1);
146 } else {
147 LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 0, file, line);
148 mtx_unlock(sx->sx_lock);
149 return (0);
150 }
151}
--- 27 unchanged lines hidden (view full) ---
179 MPASS(sx->sx_cnt == 0);
180
181 /* Acquire an exclusive lock. */
182 sx->sx_cnt--;
183 sx->sx_xholder = curthread;
184
185 LOCK_LOG_LOCK("XLOCK", &sx->sx_object, 0, 0, file, line);
186 WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
187
188 mtx_unlock(sx->sx_lock);
189}
190
191int
192_sx_try_xlock(struct sx *sx, const char *file, int line)
193{
194
195 mtx_lock(sx->sx_lock);
196 if (sx->sx_cnt == 0) {
197 sx->sx_cnt--;
198 sx->sx_xholder = curthread;
199 LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 1, file, line);
200 WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK, file,
201 line);
202 mtx_unlock(sx->sx_lock);
203 return (1);
204 } else {
205 LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 0, file, line);
206 mtx_unlock(sx->sx_lock);
207 return (0);
208 }
209}
210
211void
212_sx_sunlock(struct sx *sx, const char *file, int line)
213{
214
215 _sx_assert(sx, SX_SLOCKED, file, line);
216 mtx_lock(sx->sx_lock);
217
218 WITNESS_UNLOCK(&sx->sx_object, 0, file, line);
219
220 /* Release. */
221 sx->sx_cnt--;
222
223 /*
224 * If we just released the last shared lock, wake any waiters up, giving
225 * exclusive lockers precedence. In order to make sure that exclusive
--- 14 unchanged lines hidden (view full) ---
240void
241_sx_xunlock(struct sx *sx, const char *file, int line)
242{
243
244 _sx_assert(sx, SX_XLOCKED, file, line);
245 mtx_lock(sx->sx_lock);
246 MPASS(sx->sx_cnt == -1);
247
248 WITNESS_UNLOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
249
250 /* Release. */
251 sx->sx_cnt++;
252 sx->sx_xholder = NULL;
253
254 /*
255 * Wake up waiters if there are any. Give precedence to slock waiters.
--- 137 unchanged lines hidden ---
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 154484 2006-01-17 16:55:17Z 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
132 mtx_unlock(sx->sx_lock);
133}
134
135int
136_sx_try_slock(struct sx *sx, const char *file, int line)
137{
138
139 mtx_lock(sx->sx_lock);
140 if (sx->sx_cnt >= 0) {
141 sx->sx_cnt++;
142 LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 1, file, line);
143 WITNESS_LOCK(&sx->sx_object, LOP_TRYLOCK, file, line);
144 mtx_unlock(sx->sx_lock);
145 return (1);
146 } else {
147 LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 0, file, line);
148 mtx_unlock(sx->sx_lock);
149 return (0);
150 }
151}
--- 27 unchanged lines hidden (view full) ---
179 MPASS(sx->sx_cnt == 0);
180
181 /* Acquire an exclusive lock. */
182 sx->sx_cnt--;
183 sx->sx_xholder = curthread;
184
185 LOCK_LOG_LOCK("XLOCK", &sx->sx_object, 0, 0, file, line);
186 WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
187
188 mtx_unlock(sx->sx_lock);
189}
190
191int
192_sx_try_xlock(struct sx *sx, const char *file, int line)
193{
194
195 mtx_lock(sx->sx_lock);
196 if (sx->sx_cnt == 0) {
197 sx->sx_cnt--;
198 sx->sx_xholder = curthread;
199 LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 1, file, line);
200 WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK, file,
201 line);
202 mtx_unlock(sx->sx_lock);
203 return (1);
204 } else {
205 LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 0, file, line);
206 mtx_unlock(sx->sx_lock);
207 return (0);
208 }
209}
210
211void
212_sx_sunlock(struct sx *sx, const char *file, int line)
213{
214
215 _sx_assert(sx, SX_SLOCKED, file, line);
216 mtx_lock(sx->sx_lock);
217
218 WITNESS_UNLOCK(&sx->sx_object, 0, file, line);
219
220 /* Release. */
221 sx->sx_cnt--;
222
223 /*
224 * If we just released the last shared lock, wake any waiters up, giving
225 * exclusive lockers precedence. In order to make sure that exclusive
--- 14 unchanged lines hidden (view full) ---
240void
241_sx_xunlock(struct sx *sx, const char *file, int line)
242{
243
244 _sx_assert(sx, SX_XLOCKED, file, line);
245 mtx_lock(sx->sx_lock);
246 MPASS(sx->sx_cnt == -1);
247
248 WITNESS_UNLOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
249
250 /* Release. */
251 sx->sx_cnt++;
252 sx->sx_xholder = NULL;
253
254 /*
255 * Wake up waiters if there are any. Give precedence to slock waiters.
--- 137 unchanged lines hidden ---