From 49b94bfc54b617782de9f235dd59702b64f0022b Mon Sep 17 00:00:00 2001
From: John Baldwin <jhb@FreeBSD.org>
Date: Sat, 3 Jun 2006 21:11:33 +0000
Subject: [PATCH] Bah, fix fat finger in last.  Invert the ~ on MTX_FLAGMASK as
 it's non-intuitive for the ~ to be built into the mask.  All the users now
 explicitly ~ the mask.  In addition, add MTX_UNOWNED to the mask even though
 it technically isn't a flag.  This should unbreak mtx_owner().

Quickly spotted by:	kris
---
 sys/kern/kern_mutex.c | 4 ++--
 sys/sys/mutex.h       | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/sys/kern/kern_mutex.c b/sys/kern/kern_mutex.c
index 65b8300921e6..634fa9d82627 100644
--- a/sys/kern/kern_mutex.c
+++ b/sys/kern/kern_mutex.c
@@ -85,7 +85,7 @@ __FBSDID("$FreeBSD$");
  */
 #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
 
-#define	mtx_owner(m)	((struct thread *)((m)->mtx_lock & (MTX_FLAGMASK|MTX_UNOWNED)))
+#define	mtx_owner(m)	((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK))
 
 #ifdef DDB
 static void	db_show_mtx(struct lock_object *lock);
@@ -527,7 +527,7 @@ _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file,
 		 * If the current owner of the lock is executing on another
 		 * CPU, spin instead of blocking.
 		 */
-		owner = (struct thread *)(v & MTX_FLAGMASK);
+		owner = (struct thread *)(v & ~MTX_FLAGMASK);
 #ifdef ADAPTIVE_GIANT
 		if (TD_IS_RUNNING(owner)) {
 #else
diff --git a/sys/sys/mutex.h b/sys/sys/mutex.h
index 7984a257e707..a28ea3123b0d 100644
--- a/sys/sys/mutex.h
+++ b/sys/sys/mutex.h
@@ -71,7 +71,7 @@
 #define	MTX_RECURSED	0x00000001	/* lock recursed (for MTX_DEF only) */
 #define	MTX_CONTESTED	0x00000002	/* lock contested (for MTX_DEF only) */
 #define MTX_UNOWNED	0x00000004	/* Cookie for free mutex */
-#define	MTX_FLAGMASK	~(MTX_RECURSED | MTX_CONTESTED)
+#define	MTX_FLAGMASK	(MTX_RECURSED | MTX_CONTESTED | MTX_UNOWNED)
 
 #endif	/* _KERNEL */
 
@@ -322,7 +322,7 @@ extern struct mtx_pool *mtxpool_sleep;
 
 #define	mtx_initialized(m)	lock_initalized(&(m)->mtx_object)
 
-#define mtx_owned(m)	(((m)->mtx_lock & MTX_FLAGMASK) == (uintptr_t)curthread)
+#define mtx_owned(m)	(((m)->mtx_lock & ~MTX_FLAGMASK) == (uintptr_t)curthread)
 
 #define mtx_recursed(m)	((m)->mtx_recurse != 0)