summaryrefslogtreecommitdiffstats
path: root/sys/kern/kern_sx.c
diff options
context:
space:
mode:
authorjhb <jhb@FreeBSD.org>2001-06-27 06:39:37 +0000
committerjhb <jhb@FreeBSD.org>2001-06-27 06:39:37 +0000
commit97220dbd1ea90cd0e4fbc824e176960ac2efe949 (patch)
treec0e040174a8822672536a4e08165f5b72aadcce8 /sys/kern/kern_sx.c
parente58c0d25fbd948099aab8e1cd78816f89d32a687 (diff)
downloadFreeBSD-src-97220dbd1ea90cd0e4fbc824e176960ac2efe949.zip
FreeBSD-src-97220dbd1ea90cd0e4fbc824e176960ac2efe949.tar.gz
- Add trylock variants of shared and exclusive locks.
- The sx assertions don't actually need the internal sx mutex lock, so don't bother doing so. - Add a new assertion SX_ASSERT_LOCKED() that asserts that either a shared or exclusive lock should be held. This assertion should be used instead of SX_ASSERT_SLOCKED() in almost all cases. - Adjust some KASSERT()'s to include file and line information. - Use the new witness_assert() function in the WITNESS case for sx slock asserts to verify that the current thread actually owns a slock.
Diffstat (limited to 'sys/kern/kern_sx.c')
-rw-r--r--sys/kern/kern_sx.c46
1 files changed, 42 insertions, 4 deletions
diff --git a/sys/kern/kern_sx.c b/sys/kern/kern_sx.c
index 2619f4b..124ebff 100644
--- a/sys/kern/kern_sx.c
+++ b/sys/kern/kern_sx.c
@@ -95,8 +95,8 @@ _sx_slock(struct sx *sx, const char *file, int line)
mtx_lock(&sx->sx_lock);
KASSERT(sx->sx_xholder != curproc,
- ("%s (%s): trying to get slock while xlock is held\n", __FUNCTION__,
- sx->sx_object.lo_name));
+ ("%s (%s): slock while xlock is held @ %s:%d\n", __FUNCTION__,
+ sx->sx_object.lo_name, file, line));
/*
* Loop in case we lose the race for lock acquisition.
@@ -116,6 +116,24 @@ _sx_slock(struct sx *sx, const char *file, int line)
mtx_unlock(&sx->sx_lock);
}
+int
+_sx_try_slock(struct sx *sx, const char *file, int line)
+{
+
+ mtx_lock(&sx->sx_lock);
+ if (sx->sx_cnt >= 0) {
+ sx->sx_cnt++;
+ LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 1, file, line);
+ WITNESS_LOCK(&sx->sx_object, LOP_TRYLOCK, file, line);
+ mtx_unlock(&sx->sx_lock);
+ return (1);
+ } else {
+ LOCK_LOG_TRY("SLOCK", &sx->sx_object, 0, 0, file, line);
+ mtx_unlock(&sx->sx_lock);
+ return (0);
+ }
+}
+
void
_sx_xlock(struct sx *sx, const char *file, int line)
{
@@ -152,12 +170,32 @@ _sx_xlock(struct sx *sx, const char *file, int line)
mtx_unlock(&sx->sx_lock);
}
+int
+_sx_try_xlock(struct sx *sx, const char *file, int line)
+{
+
+ mtx_lock(&sx->sx_lock);
+ if (sx->sx_cnt == 0) {
+ sx->sx_cnt--;
+ sx->sx_xholder = curproc;
+ LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 1, file, line);
+ WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE | LOP_TRYLOCK, file,
+ line);
+ mtx_unlock(&sx->sx_lock);
+ return (1);
+ } else {
+ LOCK_LOG_TRY("XLOCK", &sx->sx_object, 0, 0, file, line);
+ mtx_unlock(&sx->sx_lock);
+ return (0);
+ }
+}
+
void
_sx_sunlock(struct sx *sx, const char *file, int line)
{
mtx_lock(&sx->sx_lock);
- _SX_ASSERT_SLOCKED(sx);
+ _SX_ASSERT_SLOCKED(sx, file, line);
WITNESS_UNLOCK(&sx->sx_object, 0, file, line);
@@ -186,7 +224,7 @@ _sx_xunlock(struct sx *sx, const char *file, int line)
{
mtx_lock(&sx->sx_lock);
- _SX_ASSERT_XLOCKED(sx);
+ _SX_ASSERT_XLOCKED(sx, file, line);
MPASS(sx->sx_cnt == -1);
WITNESS_UNLOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
OpenPOWER on IntegriCloud