From 5652e7b497256647cdcd4d5825cdb23b1b797dd0 Mon Sep 17 00:00:00 2001
From: Brian Behlendorf <behlendorf1@llnl.gov>
Date: Tue, 1 Dec 2009 15:47:27 -0800
Subject: [PATCH] When using x86 specific rwsem correctly intepret
 rwsem->count.

---
 include/sys/rwlock.h | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/include/sys/rwlock.h b/include/sys/rwlock.h
index 75356b95d8..0043f39664 100644
--- a/include/sys/rwlock.h
+++ b/include/sys/rwlock.h
@@ -47,9 +47,10 @@ typedef struct {
 } krwlock_t;
 
 /*
- * For the generic and x86 implementations of rw-semaphores the following
- * is true.  If your semaphore implementation internally represents the
- * semaphore state differently special case handling will be required.
+ * For the generic implementations of rw-semaphores the following is
+ * true.  If your semaphore implementation internally represents the
+ * semaphore state differently.  Then special case handling will be
+ * required so RW_COUNT() provides these semantics:
  * - if activity/count is 0 then there are no active readers or writers
  * - if activity/count is +ve then that is the number of active readers
  * - if activity/count is -1 then there is one active writer
@@ -63,9 +64,14 @@ typedef struct {
 extern void __up_read_locked(struct rw_semaphore *);
 extern int __down_write_trylock_locked(struct rw_semaphore *);
 #else
-# define RW_COUNT(rwp)                  (SEM(rwp)->count & RWSEM_ACTIVE_MASK)
-# define rw_exit_locked(rwp)            up_read(rwp)
-# define rw_tryenter_locked(rwp)        down_write_trylock(rwp)
+# ifdef _I386_RWSEM_H
+#  define RW_COUNT(rwp)                 ((SEM(rwp)->count < 0) ? (-1) : \
+					 (SEM(rwp)->count & RWSEM_ACTIVE_MASK))
+# else
+#  define RW_COUNT(rwp)                 (SEM(rwp)->count & RWSEM_ACTIVE_MASK)
+# endif
+# define rw_exit_locked(rwp)           up_read(rwp)
+# define rw_tryenter_locked(rwp)       down_write_trylock(rwp)
 #endif
 
 static inline kthread_t *