From f2098b2382d2465c912acd1c95589a614bf51ef0 Mon Sep 17 00:00:00 2001 From: thorpej Date: Thu, 17 Aug 2000 04:15:43 +0000 Subject: [PATCH] Some more lock debugging support: - LOCK_ASSERT(), which expands to KASSERT() if LOCKDEBUG. - new simple_lock_held(), which tests if the calling CPU holds the specified simple lock. From Bill Sommerfeld, modified slightly by me. --- sys/kern/kern_lock.c | 25 ++++++++++++++++++++++++- sys/sys/lock.h | 8 +++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/sys/kern/kern_lock.c b/sys/kern/kern_lock.c index d47fe9f5c908..e4b6c1e360b8 100644 --- a/sys/kern/kern_lock.c +++ b/sys/kern/kern_lock.c @@ -1,4 +1,4 @@ -/* $NetBSD: kern_lock.c,v 1.37 2000/08/10 04:37:59 eeh Exp $ */ +/* $NetBSD: kern_lock.c,v 1.38 2000/08/17 04:15:43 thorpej Exp $ */ /*- * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc. @@ -832,6 +832,29 @@ _simple_lock(__volatile struct simplelock *alp, const char *id, int l) splx(s); } +int +_simple_lock_held(__volatile struct simplelock *alp) +{ +#if defined(MULTIPROCESSOR) + cpuid_t cpu_id = cpu_number(); + int s, locked = 0; + + s = splhigh(); + if (__cpu_simple_lock_try(&alp->lock_data) == 0) + locked = (alp->lock_holder == cpu_id); + else + __cpu_simple_unlock(&alp->lock_data); + splx(s); +#else + int s, locked; + + s = splhigh(); + locked = (alp->lock_data == __SIMPLELOCK_LOCKED); + splx(s); +#endif + return (locked); +} + int _simple_lock_try(__volatile struct simplelock *alp, const char *id, int l) { diff --git a/sys/sys/lock.h b/sys/sys/lock.h index b794f32c8a29..7b5f123b7bdc 100644 --- a/sys/sys/lock.h +++ b/sys/sys/lock.h @@ -1,4 +1,4 @@ -/* $NetBSD: lock.h,v 1.34 2000/08/07 22:10:52 thorpej Exp $ */ +/* $NetBSD: lock.h,v 1.35 2000/08/17 04:15:43 thorpej Exp $ */ /*- * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc. @@ -293,10 +293,14 @@ void spinlock_switchcheck(void); void _simple_lock(__volatile struct simplelock *, const char *, int); int _simple_lock_try(__volatile struct simplelock *, const char *, int); void _simple_unlock(__volatile struct simplelock *, const char *, int); +int _simple_lock_held(__volatile struct simplelock *); #define simple_lock(alp) _simple_lock((alp), __FILE__, __LINE__) #define simple_lock_try(alp) _simple_lock_try((alp), __FILE__, __LINE__) #define simple_unlock(alp) _simple_unlock((alp), __FILE__, __LINE__) +#define simple_lock_held(alp) _simple_lock_held((alp)) + +#define LOCK_ASSERT(x) KASSERT(x) void simple_lock_init(struct simplelock *); void simple_lock_dump(void); @@ -307,11 +311,13 @@ void simple_lock_switchcheck(void); #define simple_lock(alp) __cpu_simple_lock(&(alp)->lock_data) #define simple_lock_try(alp) __cpu_simple_lock_try(&(alp)->lock_data) #define simple_unlock(alp) __cpu_simple_unlock(&(alp)->lock_data) +#define LOCK_ASSERT(x) /* nothing */ #else #define simple_lock_init(alp) (alp)->lock_data = __SIMPLELOCK_UNLOCKED #define simple_lock(alp) /* nothing */ #define simple_lock_try(alp) (1) /* always succeeds */ #define simple_unlock(alp) /* nothing */ +#define LOCK_ASSERT(x) /* nothing */ #endif #endif /* _KERNEL */