Index: c/src/exec/score/ChangeLog
===================================================================
--- c/src/exec/score/ChangeLog	(revision 760045f0f34ce2b801066128f340b2267d91b26d)
+++ c/src/exec/score/ChangeLog	(revision fb1d8f81a0e38540fb470da89d044dd28b0134ad)
@@ -1,2 +1,15 @@
+
+2001-08-30	Joel Sherrill <joel@OARcorp.com>
+
+	*  src/coremutex.c, src/coremutexseize.c, src/coremutexsurrender.c,
+	inline/rtems/score/coremutex.inl: The per thread field resource_count
+	should only be manipulated when a mutex is priority ceiling or
+	priority inherit.  This was reported by Chris Johns <ccj@acm.org>
+	who also noticed that the use of switches for all disciplines
+	generated less efficient code than using explicit tests for the one
+	or two cases we were really interested in.  Further review of his
+	modifications made it apparent that the "isa" methods to test mutex
+	discipline were not being used so this modification was swept into
+	the code as well.
 
 2001-08-30	Joel Sherrill <joel@OARcorp.com>
Index: c/src/exec/score/inline/rtems/score/coremutex.inl
===================================================================
--- c/src/exec/score/inline/rtems/score/coremutex.inl	(revision 760045f0f34ce2b801066128f340b2267d91b26d)
+++ c/src/exec/score/inline/rtems/score/coremutex.inl	(revision fb1d8f81a0e38540fb470da89d044dd28b0134ad)
@@ -135,7 +135,8 @@
     the_mutex->holder_id  = executing->Object.id;
     the_mutex->nest_count = 1;
-    executing->resource_count++;
-    if ( the_mutex->Attributes.discipline !=
-           CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING ) {
+    if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+         _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) )
+      executing->resource_count++;
+    if ( !_CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
         _ISR_Enable( level );
         return 0;
Index: c/src/exec/score/src/coremutex.c
===================================================================
--- c/src/exec/score/src/coremutex.c	(revision 760045f0f34ce2b801066128f340b2267d91b26d)
+++ c/src/exec/score/src/coremutex.c	(revision fb1d8f81a0e38540fb470da89d044dd28b0134ad)
@@ -74,5 +74,7 @@
     the_mutex->holder     = _Thread_Executing;
     the_mutex->holder_id  = _Thread_Executing->Object.id;
-    _Thread_Executing->resource_count++;
+    if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+         _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) )
+      _Thread_Executing->resource_count++;
   } else {
     the_mutex->nest_count = 0;
Index: c/src/exec/score/src/coremutexseize.c
===================================================================
--- c/src/exec/score/src/coremutexseize.c	(revision 760045f0f34ce2b801066128f340b2267d91b26d)
+++ c/src/exec/score/src/coremutexseize.c	(revision fb1d8f81a0e38540fb470da89d044dd28b0134ad)
@@ -44,18 +44,12 @@
 
   executing = _Thread_Executing;
-  switch ( the_mutex->Attributes.discipline ) {
-    case CORE_MUTEX_DISCIPLINES_FIFO:
-    case CORE_MUTEX_DISCIPLINES_PRIORITY:
-    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
-      break;
-    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
-      if ( the_mutex->holder->current_priority > executing->current_priority ) {
-        _Thread_Change_priority(
-          the_mutex->holder,
-          executing->current_priority,
-          FALSE
-        );
-      }
-      break;
+  if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ) {
+    if ( the_mutex->holder->current_priority > executing->current_priority ) {
+      _Thread_Change_priority(
+        the_mutex->holder,
+        executing->current_priority,
+        FALSE
+      );
+    }
   }
 
@@ -64,19 +58,17 @@
 
   if ( _Thread_Executing->Wait.return_code == CORE_MUTEX_STATUS_SUCCESSFUL ) {
-    switch ( the_mutex->Attributes.discipline ) {
-      case CORE_MUTEX_DISCIPLINES_FIFO:
-      case CORE_MUTEX_DISCIPLINES_PRIORITY:
-      case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
-        break;
-      case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
-        if ( the_mutex->Attributes.priority_ceiling <
-                                           executing->current_priority ) {
-          _Thread_Change_priority(
-            executing,
-            the_mutex->Attributes.priority_ceiling,
-            FALSE
-          );
-        };
-        break;
+    /*
+     *  if CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT then nothing to do
+     *  because this task is already the highest priority.
+     */
+
+    if ( _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
+      if (the_mutex->Attributes.priority_ceiling < executing->current_priority){
+        _Thread_Change_priority(
+          executing,
+          the_mutex->Attributes.priority_ceiling,
+          FALSE
+        );
+      }
     }
   }
@@ -102,9 +94,11 @@
     the_mutex->holder_id  = executing->Object.id;
     the_mutex->nest_count = 1;
-    executing->resource_count++;
-    if ( the_mutex->Attributes.discipline !=
-           CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING ) {
-        _ISR_Enable( level );
-        return 0;
+    if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+         _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) )
+      executing->resource_count++;
+
+    if ( !_CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
+      _ISR_Enable( level );
+      return 0;
     }
     /* else must be CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING */
@@ -140,4 +134,20 @@
     return 0;
   }
+
+  if ( _Thread_Is_executing( the_mutex->holder ) ) {
+    switch ( the_mutex->Attributes.lock_nesting_behavior ) {
+      case CORE_MUTEX_NESTING_ACQUIRES:
+        the_mutex->nest_count++;
+        _ISR_Enable( level );
+        return 0;
+      case CORE_MUTEX_NESTING_IS_ERROR:
+        executing->Wait.return_code = CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED;
+        _ISR_Enable( level );
+        return 0;
+      case CORE_MUTEX_NESTING_BLOCKS:
+        break;
+    }
+  }
+
   return 1;
 }
Index: c/src/exec/score/src/coremutexsurrender.c
===================================================================
--- c/src/exec/score/src/coremutexsurrender.c	(revision 760045f0f34ce2b801066128f340b2267d91b26d)
+++ c/src/exec/score/src/coremutexsurrender.c	(revision fb1d8f81a0e38540fb470da89d044dd28b0134ad)
@@ -87,5 +87,7 @@
   }
 
-  holder->resource_count--;
+  if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+       _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) )
+    holder->resource_count--;
   the_mutex->holder    = NULL;
   the_mutex->holder_id = 0;
@@ -97,17 +99,11 @@
    */
 
-  switch ( the_mutex->Attributes.discipline ) {
-    case CORE_MUTEX_DISCIPLINES_FIFO:
-    case CORE_MUTEX_DISCIPLINES_PRIORITY:
-      break;
-    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
-    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
-      if ( holder->resource_count == 0 && 
-           holder->real_priority != holder->current_priority ) {
-         _Thread_Change_priority( holder, holder->real_priority, TRUE );
-      }
-      break;
+  if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+       _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
+    if ( holder->resource_count == 0 && 
+         holder->real_priority != holder->current_priority ) {
+      _Thread_Change_priority( holder, holder->real_priority, TRUE );
+    }
   }
-
 
   if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) {
@@ -128,5 +124,7 @@
       the_mutex->holder     = the_thread;
       the_mutex->holder_id  = the_thread->Object.id;
-      the_thread->resource_count++;
+      if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+           _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) )
+        the_thread->resource_count++;
       the_mutex->nest_count = 1;
 
Index: cpukit/score/ChangeLog
===================================================================
--- cpukit/score/ChangeLog	(revision 760045f0f34ce2b801066128f340b2267d91b26d)
+++ cpukit/score/ChangeLog	(revision fb1d8f81a0e38540fb470da89d044dd28b0134ad)
@@ -1,2 +1,15 @@
+
+2001-08-30	Joel Sherrill <joel@OARcorp.com>
+
+	*  src/coremutex.c, src/coremutexseize.c, src/coremutexsurrender.c,
+	inline/rtems/score/coremutex.inl: The per thread field resource_count
+	should only be manipulated when a mutex is priority ceiling or
+	priority inherit.  This was reported by Chris Johns <ccj@acm.org>
+	who also noticed that the use of switches for all disciplines
+	generated less efficient code than using explicit tests for the one
+	or two cases we were really interested in.  Further review of his
+	modifications made it apparent that the "isa" methods to test mutex
+	discipline were not being used so this modification was swept into
+	the code as well.
 
 2001-08-30	Joel Sherrill <joel@OARcorp.com>
Index: cpukit/score/inline/rtems/score/coremutex.inl
===================================================================
--- cpukit/score/inline/rtems/score/coremutex.inl	(revision 760045f0f34ce2b801066128f340b2267d91b26d)
+++ cpukit/score/inline/rtems/score/coremutex.inl	(revision fb1d8f81a0e38540fb470da89d044dd28b0134ad)
@@ -135,7 +135,8 @@
     the_mutex->holder_id  = executing->Object.id;
     the_mutex->nest_count = 1;
-    executing->resource_count++;
-    if ( the_mutex->Attributes.discipline !=
-           CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING ) {
+    if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+         _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) )
+      executing->resource_count++;
+    if ( !_CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
         _ISR_Enable( level );
         return 0;
Index: cpukit/score/src/coremutex.c
===================================================================
--- cpukit/score/src/coremutex.c	(revision 760045f0f34ce2b801066128f340b2267d91b26d)
+++ cpukit/score/src/coremutex.c	(revision fb1d8f81a0e38540fb470da89d044dd28b0134ad)
@@ -74,5 +74,7 @@
     the_mutex->holder     = _Thread_Executing;
     the_mutex->holder_id  = _Thread_Executing->Object.id;
-    _Thread_Executing->resource_count++;
+    if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+         _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) )
+      _Thread_Executing->resource_count++;
   } else {
     the_mutex->nest_count = 0;
Index: cpukit/score/src/coremutexseize.c
===================================================================
--- cpukit/score/src/coremutexseize.c	(revision 760045f0f34ce2b801066128f340b2267d91b26d)
+++ cpukit/score/src/coremutexseize.c	(revision fb1d8f81a0e38540fb470da89d044dd28b0134ad)
@@ -44,18 +44,12 @@
 
   executing = _Thread_Executing;
-  switch ( the_mutex->Attributes.discipline ) {
-    case CORE_MUTEX_DISCIPLINES_FIFO:
-    case CORE_MUTEX_DISCIPLINES_PRIORITY:
-    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
-      break;
-    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
-      if ( the_mutex->holder->current_priority > executing->current_priority ) {
-        _Thread_Change_priority(
-          the_mutex->holder,
-          executing->current_priority,
-          FALSE
-        );
-      }
-      break;
+  if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ) {
+    if ( the_mutex->holder->current_priority > executing->current_priority ) {
+      _Thread_Change_priority(
+        the_mutex->holder,
+        executing->current_priority,
+        FALSE
+      );
+    }
   }
 
@@ -64,19 +58,17 @@
 
   if ( _Thread_Executing->Wait.return_code == CORE_MUTEX_STATUS_SUCCESSFUL ) {
-    switch ( the_mutex->Attributes.discipline ) {
-      case CORE_MUTEX_DISCIPLINES_FIFO:
-      case CORE_MUTEX_DISCIPLINES_PRIORITY:
-      case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
-        break;
-      case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
-        if ( the_mutex->Attributes.priority_ceiling <
-                                           executing->current_priority ) {
-          _Thread_Change_priority(
-            executing,
-            the_mutex->Attributes.priority_ceiling,
-            FALSE
-          );
-        };
-        break;
+    /*
+     *  if CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT then nothing to do
+     *  because this task is already the highest priority.
+     */
+
+    if ( _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
+      if (the_mutex->Attributes.priority_ceiling < executing->current_priority){
+        _Thread_Change_priority(
+          executing,
+          the_mutex->Attributes.priority_ceiling,
+          FALSE
+        );
+      }
     }
   }
@@ -102,9 +94,11 @@
     the_mutex->holder_id  = executing->Object.id;
     the_mutex->nest_count = 1;
-    executing->resource_count++;
-    if ( the_mutex->Attributes.discipline !=
-           CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING ) {
-        _ISR_Enable( level );
-        return 0;
+    if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+         _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) )
+      executing->resource_count++;
+
+    if ( !_CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
+      _ISR_Enable( level );
+      return 0;
     }
     /* else must be CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING */
@@ -140,4 +134,20 @@
     return 0;
   }
+
+  if ( _Thread_Is_executing( the_mutex->holder ) ) {
+    switch ( the_mutex->Attributes.lock_nesting_behavior ) {
+      case CORE_MUTEX_NESTING_ACQUIRES:
+        the_mutex->nest_count++;
+        _ISR_Enable( level );
+        return 0;
+      case CORE_MUTEX_NESTING_IS_ERROR:
+        executing->Wait.return_code = CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED;
+        _ISR_Enable( level );
+        return 0;
+      case CORE_MUTEX_NESTING_BLOCKS:
+        break;
+    }
+  }
+
   return 1;
 }
Index: cpukit/score/src/coremutexsurrender.c
===================================================================
--- cpukit/score/src/coremutexsurrender.c	(revision 760045f0f34ce2b801066128f340b2267d91b26d)
+++ cpukit/score/src/coremutexsurrender.c	(revision fb1d8f81a0e38540fb470da89d044dd28b0134ad)
@@ -87,5 +87,7 @@
   }
 
-  holder->resource_count--;
+  if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+       _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) )
+    holder->resource_count--;
   the_mutex->holder    = NULL;
   the_mutex->holder_id = 0;
@@ -97,17 +99,11 @@
    */
 
-  switch ( the_mutex->Attributes.discipline ) {
-    case CORE_MUTEX_DISCIPLINES_FIFO:
-    case CORE_MUTEX_DISCIPLINES_PRIORITY:
-      break;
-    case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING:
-    case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT:
-      if ( holder->resource_count == 0 && 
-           holder->real_priority != holder->current_priority ) {
-         _Thread_Change_priority( holder, holder->real_priority, TRUE );
-      }
-      break;
+  if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+       _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) {
+    if ( holder->resource_count == 0 && 
+         holder->real_priority != holder->current_priority ) {
+      _Thread_Change_priority( holder, holder->real_priority, TRUE );
+    }
   }
-
 
   if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) {
@@ -128,5 +124,7 @@
       the_mutex->holder     = the_thread;
       the_mutex->holder_id  = the_thread->Object.id;
-      the_thread->resource_count++;
+      if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) ||
+           _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) )
+        the_thread->resource_count++;
       the_mutex->nest_count = 1;
 
