Module Name:    src
Committed By:   pgoyette
Date:           Mon May  1 21:35:26 UTC 2017

Modified Files:
        src/sys/kern: kern_mutex.c subr_lockdebug.c
        src/sys/rump/librump/rumpkern: locks.c
        src/sys/sys: mutex.h

Log Message:
Introduce mutex_ownable() to determine if it is possible for the current
process to acquire a mutex.


To generate a diff of this commit:
cvs rdiff -u -r1.64 -r1.65 src/sys/kern/kern_mutex.c
cvs rdiff -u -r1.55 -r1.56 src/sys/kern/subr_lockdebug.c
cvs rdiff -u -r1.73 -r1.74 src/sys/rump/librump/rumpkern/locks.c
cvs rdiff -u -r1.20 -r1.21 src/sys/sys/mutex.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/kern_mutex.c
diff -u src/sys/kern/kern_mutex.c:1.64 src/sys/kern/kern_mutex.c:1.65
--- src/sys/kern/kern_mutex.c:1.64	Thu Jan 26 04:11:56 2017
+++ src/sys/kern/kern_mutex.c	Mon May  1 21:35:25 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_mutex.c,v 1.64 2017/01/26 04:11:56 christos Exp $	*/
+/*	$NetBSD: kern_mutex.c,v 1.65 2017/05/01 21:35:25 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -40,7 +40,7 @@
 #define	__MUTEX_PRIVATE
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.64 2017/01/26 04:11:56 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.65 2017/05/01 21:35:25 pgoyette Exp $");
 
 #include <sys/param.h>
 #include <sys/atomic.h>
@@ -75,6 +75,9 @@ __KERNEL_RCSID(0, "$NetBSD: kern_mutex.c
 #define	MUTEX_WANTLOCK(mtx)					\
     LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx),		\
         (uintptr_t)__builtin_return_address(0), 0)
+#define	MUTEX_TESTLOCK(mtx)					\
+    LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx),		\
+        (uintptr_t)__builtin_return_address(0), -1)
 #define	MUTEX_LOCKED(mtx)					\
     LOCKDEBUG_LOCKED(MUTEX_DEBUG_P(mtx), (mtx), NULL,		\
         (uintptr_t)__builtin_return_address(0), 0)
@@ -831,6 +834,23 @@ mutex_owner(kmutex_t *mtx)
 }
 
 /*
+ * mutex_ownable:
+ *
+ *	When compiled with DEBUG and LOCKDEBUG defined, ensure that
+ *	the mutex is available.  We cannot use !mutex_owned() since
+ *	that won't work correctly for spin mutexes.
+ */
+int
+mutex_ownable(kmutex_t *mtx)
+{
+
+#ifdef LOCKDEBUG
+	MUTEX_TESTLOCK(mtx);
+#endif
+	return 1;
+}
+
+/*
  * mutex_tryenter:
  *
  *	Try to acquire the mutex; return non-zero if we did.

Index: src/sys/kern/subr_lockdebug.c
diff -u src/sys/kern/subr_lockdebug.c:1.55 src/sys/kern/subr_lockdebug.c:1.56
--- src/sys/kern/subr_lockdebug.c:1.55	Thu Jan 26 04:11:56 2017
+++ src/sys/kern/subr_lockdebug.c	Mon May  1 21:35:26 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_lockdebug.c,v 1.55 2017/01/26 04:11:56 christos Exp $	*/
+/*	$NetBSD: subr_lockdebug.c,v 1.56 2017/05/01 21:35:26 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.55 2017/01/26 04:11:56 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_lockdebug.c,v 1.56 2017/05/01 21:35:26 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -417,7 +417,9 @@ lockdebug_more(int s)
 /*
  * lockdebug_wantlock:
  *
- *	Process the preamble to a lock acquire.
+ *	Process the preamble to a lock acquire.  The "shared"
+ *	parameter controls which ld_{ex,sh}want counter is
+ *	updated; a negative value of shared updates neither.
  */
 void
 lockdebug_wantlock(const char *func, size_t line,
@@ -454,9 +456,9 @@ lockdebug_wantlock(const char *func, siz
 			return;
 		}
 	}
-	if (shared)
+	if (shared > 0)
 		ld->ld_shwant++;
-	else
+	else if (shared == 0)
 		ld->ld_exwant++;
 	if (recurse) {
 		lockdebug_abort1(func, line, ld, s, "locking against myself",

Index: src/sys/rump/librump/rumpkern/locks.c
diff -u src/sys/rump/librump/rumpkern/locks.c:1.73 src/sys/rump/librump/rumpkern/locks.c:1.74
--- src/sys/rump/librump/rumpkern/locks.c:1.73	Fri Jan 27 09:50:47 2017
+++ src/sys/rump/librump/rumpkern/locks.c	Mon May  1 21:35:26 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locks.c,v 1.73 2017/01/27 09:50:47 ozaki-r Exp $	*/
+/*	$NetBSD: locks.c,v 1.74 2017/05/01 21:35:26 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.73 2017/01/27 09:50:47 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locks.c,v 1.74 2017/05/01 21:35:26 pgoyette Exp $");
 
 #include <sys/param.h>
 #include <sys/kmem.h>
@@ -183,6 +183,16 @@ mutex_exit(kmutex_t *mtx)
 __strong_alias(mutex_spin_exit,mutex_exit);
 
 int
+mutex_ownable(kmutex_t *mtx)
+{
+
+#ifdef LOCKDEBUG
+	WANTLOCK(mtx, -1);
+#endif
+	return 1;
+}
+
+int
 mutex_owned(kmutex_t *mtx)
 {
 

Index: src/sys/sys/mutex.h
diff -u src/sys/sys/mutex.h:1.20 src/sys/sys/mutex.h:1.21
--- src/sys/sys/mutex.h:1.20	Mon Feb  8 09:54:27 2010
+++ src/sys/sys/mutex.h	Mon May  1 21:35:26 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mutex.h,v 1.20 2010/02/08 09:54:27 skrll Exp $	*/
+/*	$NetBSD: mutex.h,v 1.21 2017/05/01 21:35:26 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
@@ -204,6 +204,7 @@ void	mutex_spin_exit(kmutex_t *);
 int	mutex_tryenter(kmutex_t *);
 
 int	mutex_owned(kmutex_t *);
+int	mutex_ownable(kmutex_t *);
 lwp_t	*mutex_owner(kmutex_t *);
 
 void	mutex_obj_init(void);

Reply via email to