Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Introduce mutex_ownable() to determine if it is possible...



details:   https://anonhg.NetBSD.org/src/rev/1ef576776bdd
branches:  trunk
changeset: 353364:1ef576776bdd
user:      pgoyette <pgoyette%NetBSD.org@localhost>
date:      Mon May 01 21:35:25 2017 +0000

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

diffstat:

 sys/kern/kern_mutex.c             |  24 ++++++++++++++++++++++--
 sys/kern/subr_lockdebug.c         |  12 +++++++-----
 sys/rump/librump/rumpkern/locks.c |  14 ++++++++++++--
 sys/sys/mutex.h                   |   3 ++-
 4 files changed, 43 insertions(+), 10 deletions(-)

diffs (145 lines):

diff -r 2db13cb62b71 -r 1ef576776bdd sys/kern/kern_mutex.c
--- a/sys/kern/kern_mutex.c     Mon May 01 19:09:22 2017 +0000
+++ b/sys/kern/kern_mutex.c     Mon May 01 21:35:25 2017 +0000
@@ -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 @@
 #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_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.
diff -r 2db13cb62b71 -r 1ef576776bdd sys/kern/subr_lockdebug.c
--- a/sys/kern/subr_lockdebug.c Mon May 01 19:09:22 2017 +0000
+++ b/sys/kern/subr_lockdebug.c Mon May 01 21:35:25 2017 +0000
@@ -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_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 @@
                        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",
diff -r 2db13cb62b71 -r 1ef576776bdd sys/rump/librump/rumpkern/locks.c
--- a/sys/rump/librump/rumpkern/locks.c Mon May 01 19:09:22 2017 +0000
+++ b/sys/rump/librump/rumpkern/locks.c Mon May 01 21:35:25 2017 +0000
@@ -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 @@
 __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)
 {
 
diff -r 2db13cb62b71 -r 1ef576776bdd sys/sys/mutex.h
--- a/sys/sys/mutex.h   Mon May 01 19:09:22 2017 +0000
+++ b/sys/sys/mutex.h   Mon May 01 21:35:25 2017 +0000
@@ -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 @@
 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);



Home | Main Index | Thread Index | Old Index