[PATCH 06/12] score: Simplify CORE mutex

Sebastian Huber sebastian.huber at embedded-brains.de
Fri May 27 13:50:35 UTC 2016


Remove superfluous support for simple binary semaphores.  With this we
can get rid of the CORE_MUTEX_NESTING_BLOCKS variant.
---
 cpukit/rtems/include/rtems/rtems/semimpl.h       |  1 +
 cpukit/rtems/src/semcreate.c                     | 43 ++++++++++++------------
 cpukit/rtems/src/semdelete.c                     | 18 +++++-----
 cpukit/rtems/src/semflush.c                      |  5 ++-
 cpukit/rtems/src/semobtain.c                     |  5 ++-
 cpukit/rtems/src/semrelease.c                    | 13 +++++++
 cpukit/score/include/rtems/score/coremutex.h     |  7 ----
 cpukit/score/include/rtems/score/coremuteximpl.h |  2 --
 cpukit/score/src/coremutexsurrender.c            |  3 --
 testsuites/sptests/spintrcritical22/init.c       |  7 ++--
 10 files changed, 57 insertions(+), 47 deletions(-)

diff --git a/cpukit/rtems/include/rtems/rtems/semimpl.h b/cpukit/rtems/include/rtems/rtems/semimpl.h
index 813f885..f3dfa02 100644
--- a/cpukit/rtems/include/rtems/rtems/semimpl.h
+++ b/cpukit/rtems/include/rtems/rtems/semimpl.h
@@ -28,6 +28,7 @@ extern "C" {
 
 typedef enum {
   SEMAPHORE_VARIANT_MUTEX,
+  SEMAPHORE_VARIANT_SIMPLE_BINARY,
   SEMAPHORE_VARIANT_COUNTING
 #if defined(RTEMS_SMP)
   ,
diff --git a/cpukit/rtems/src/semcreate.c b/cpukit/rtems/src/semcreate.c
index be8f9f5..455182b 100644
--- a/cpukit/rtems/src/semcreate.c
+++ b/cpukit/rtems/src/semcreate.c
@@ -143,9 +143,6 @@ rtems_status_code rtems_semaphore_create(
     the_semaphore->discipline = SEMAPHORE_DISCIPLINE_FIFO;
   }
 
-  /*
-   *  Initialize it as a counting semaphore.
-   */
   if ( _Attributes_Is_counting_semaphore( attribute_set ) ) {
     the_semaphore->variant = SEMAPHORE_VARIANT_COUNTING;
     _CORE_semaphore_Initialize(
@@ -153,6 +150,13 @@ rtems_status_code rtems_semaphore_create(
       count
     );
     status = STATUS_SUCCESSFUL;
+  } else if ( _Attributes_Is_simple_binary_semaphore( attribute_set ) ) {
+    the_semaphore->variant = SEMAPHORE_VARIANT_SIMPLE_BINARY;
+    _CORE_semaphore_Initialize(
+      &the_semaphore->Core_control.semaphore,
+      count != 0
+    );
+    status = STATUS_SUCCESSFUL;
 #if defined(RTEMS_SMP)
   } else if ( _Attributes_Is_multiprocessor_resource_sharing( attribute_set ) ) {
     the_semaphore->variant = SEMAPHORE_VARIANT_MRSP;
@@ -166,6 +170,8 @@ rtems_status_code rtems_semaphore_create(
   } else {
     the_semaphore->variant = SEMAPHORE_VARIANT_MUTEX;
 
+    _Assert( _Attributes_Is_binary_semaphore( attribute_set ) );
+
     /*
      *  It is either simple binary semaphore or a more powerful mutex
      *  style binary semaphore.  This is the mutex style.
@@ -175,25 +181,20 @@ rtems_status_code rtems_semaphore_create(
     else
       the_mutex_attr.discipline = CORE_MUTEX_DISCIPLINES_FIFO;
 
-    if ( _Attributes_Is_binary_semaphore( attribute_set ) ) {
-      the_mutex_attr.priority_ceiling      = _RTEMS_tasks_Priority_to_Core(
-                                               priority_ceiling
-                                             );
-      the_mutex_attr.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
-      the_mutex_attr.only_owner_release    = false;
-
-      if ( the_mutex_attr.discipline == CORE_MUTEX_DISCIPLINES_PRIORITY ) {
-        if ( _Attributes_Is_inherit_priority( attribute_set ) ) {
-          the_mutex_attr.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
-          the_mutex_attr.only_owner_release = true;
-        } else if ( _Attributes_Is_priority_ceiling( attribute_set ) ) {
-          the_mutex_attr.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
-          the_mutex_attr.only_owner_release = true;
-        }
+    the_mutex_attr.priority_ceiling      = _RTEMS_tasks_Priority_to_Core(
+                                             priority_ceiling
+                                           );
+    the_mutex_attr.lock_nesting_behavior = CORE_MUTEX_NESTING_ACQUIRES;
+    the_mutex_attr.only_owner_release    = false;
+
+    if ( the_mutex_attr.discipline == CORE_MUTEX_DISCIPLINES_PRIORITY ) {
+      if ( _Attributes_Is_inherit_priority( attribute_set ) ) {
+        the_mutex_attr.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT;
+        the_mutex_attr.only_owner_release = true;
+      } else if ( _Attributes_Is_priority_ceiling( attribute_set ) ) {
+        the_mutex_attr.discipline = CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING;
+        the_mutex_attr.only_owner_release = true;
       }
-    } else /* must be simple binary semaphore */ {
-      the_mutex_attr.lock_nesting_behavior = CORE_MUTEX_NESTING_BLOCKS;
-      the_mutex_attr.only_owner_release = false;
     }
 
     status = _CORE_mutex_Initialize(
diff --git a/cpukit/rtems/src/semdelete.c b/cpukit/rtems/src/semdelete.c
index 45c356f..768e686 100644
--- a/cpukit/rtems/src/semdelete.c
+++ b/cpukit/rtems/src/semdelete.c
@@ -28,7 +28,6 @@ rtems_status_code rtems_semaphore_delete(
 {
   Semaphore_Control    *the_semaphore;
   Thread_queue_Context  queue_context;
-  rtems_attribute       attribute_set;
   Status_Control        status;
 
   _Objects_Allocator_lock();
@@ -46,8 +45,6 @@ rtems_status_code rtems_semaphore_delete(
     return RTEMS_INVALID_ID;
   }
 
-  attribute_set = the_semaphore->attribute_set;
-
   _Thread_queue_Acquire_critical(
     &the_semaphore->Core_control.Wait_queue,
     &queue_context.Lock_context
@@ -55,10 +52,7 @@ rtems_status_code rtems_semaphore_delete(
 
   switch ( the_semaphore->variant ) {
     case SEMAPHORE_VARIANT_MUTEX:
-      if (
-        _CORE_mutex_Is_locked( &the_semaphore->Core_control.mutex )
-          && !_Attributes_Is_simple_binary_semaphore( attribute_set )
-      ) {
+      if ( _CORE_mutex_Is_locked( &the_semaphore->Core_control.mutex ) ) {
         status = STATUS_RESOURCE_IN_USE;
       } else {
         status = STATUS_SUCCESSFUL;
@@ -71,7 +65,10 @@ rtems_status_code rtems_semaphore_delete(
       break;
 #endif
     default:
-      _Assert( the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING );
+      _Assert(
+        the_semaphore->variant == SEMAPHORE_VARIANT_SIMPLE_BINARY
+          || the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING
+      );
       status = STATUS_SUCCESSFUL;
       break;
   }
@@ -102,7 +99,10 @@ rtems_status_code rtems_semaphore_delete(
       break;
 #endif
     default:
-      _Assert( the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING );
+      _Assert(
+        the_semaphore->variant == SEMAPHORE_VARIANT_SIMPLE_BINARY
+          || the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING
+      );
       _CORE_semaphore_Destroy(
         &the_semaphore->Core_control.semaphore,
         _Semaphore_Get_operations( the_semaphore ),
diff --git a/cpukit/rtems/src/semflush.c b/cpukit/rtems/src/semflush.c
index f768bbd..2410ed2 100644
--- a/cpukit/rtems/src/semflush.c
+++ b/cpukit/rtems/src/semflush.c
@@ -63,7 +63,10 @@ rtems_status_code rtems_semaphore_flush( rtems_id id )
       );
       break;
     default:
-      _Assert( the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING );
+      _Assert(
+        the_semaphore->variant == SEMAPHORE_VARIANT_SIMPLE_BINARY
+          || the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING
+      );
       _CORE_semaphore_Flush(
         &the_semaphore->Core_control.semaphore,
         _Semaphore_Get_operations( the_semaphore ),
diff --git a/cpukit/rtems/src/semobtain.c b/cpukit/rtems/src/semobtain.c
index 00474da..b261747 100644
--- a/cpukit/rtems/src/semobtain.c
+++ b/cpukit/rtems/src/semobtain.c
@@ -92,7 +92,10 @@ rtems_status_code rtems_semaphore_obtain(
       );
       break;
     default:
-      _Assert( the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING );
+      _Assert(
+        the_semaphore->variant == SEMAPHORE_VARIANT_SIMPLE_BINARY
+          || the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING
+      );
       status = _CORE_semaphore_Seize(
         &the_semaphore->Core_control.semaphore,
         _Semaphore_Get_operations( the_semaphore ),
diff --git a/cpukit/rtems/src/semrelease.c b/cpukit/rtems/src/semrelease.c
index d18901a..3e13334 100644
--- a/cpukit/rtems/src/semrelease.c
+++ b/cpukit/rtems/src/semrelease.c
@@ -61,6 +61,19 @@ rtems_status_code rtems_semaphore_release( rtems_id id )
         &queue_context
       );
       break;
+    case SEMAPHORE_VARIANT_SIMPLE_BINARY:
+      status = _CORE_semaphore_Surrender(
+        &the_semaphore->Core_control.semaphore,
+        _Semaphore_Get_operations( the_semaphore ),
+        1,
+        &queue_context
+      );
+      _Assert(
+        status == STATUS_SUCCESSFUL
+          || status == STATUS_MAXIMUM_COUNT_EXCEEDED
+      );
+      status = STATUS_SUCCESSFUL;
+      break;
     default:
       _Assert( the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING );
       status = _CORE_semaphore_Surrender(
diff --git a/cpukit/score/include/rtems/score/coremutex.h b/cpukit/score/include/rtems/score/coremutex.h
index 41f1c4f..f869409 100644
--- a/cpukit/score/include/rtems/score/coremutex.h
+++ b/cpukit/score/include/rtems/score/coremutex.h
@@ -87,13 +87,6 @@ typedef enum {
    */
   CORE_MUTEX_NESTING_IS_ERROR,
 #endif
-  /**
-   *    This sequence performs as indicated:
-   *        + lock(m)
-   *        + lock(m)   - deadlocks or timeouts
-   *        + unlock(m) - releases
-   */
-  CORE_MUTEX_NESTING_BLOCKS
 }  CORE_mutex_Nesting_behaviors;
 
 /**
diff --git a/cpukit/score/include/rtems/score/coremuteximpl.h b/cpukit/score/include/rtems/score/coremuteximpl.h
index e29d4b7..ccc4148 100644
--- a/cpukit/score/include/rtems/score/coremuteximpl.h
+++ b/cpukit/score/include/rtems/score/coremuteximpl.h
@@ -238,8 +238,6 @@ RTEMS_INLINE_ROUTINE Status_Control _CORE_mutex_Seize_interrupt_trylock(
           _CORE_mutex_Release( the_mutex, queue_context );
           return STATUS_NESTING_NOT_ALLOWED;
       #endif
-      case CORE_MUTEX_NESTING_BLOCKS:
-        break;
     }
   }
 
diff --git a/cpukit/score/src/coremutexsurrender.c b/cpukit/score/src/coremutexsurrender.c
index 1da9827..6047409 100644
--- a/cpukit/score/src/coremutexsurrender.c
+++ b/cpukit/score/src/coremutexsurrender.c
@@ -76,9 +76,6 @@ Status_Control _CORE_mutex_Surrender(
             _CORE_mutex_Release( the_mutex, queue_context );
             return STATUS_NESTING_NOT_ALLOWED;
         #endif
-        case CORE_MUTEX_NESTING_BLOCKS:
-          /* Currently no API exercises this behavior. */
-          break;
       }
     #else
       _CORE_mutex_Release( the_mutex, queue_context );
diff --git a/testsuites/sptests/spintrcritical22/init.c b/testsuites/sptests/spintrcritical22/init.c
index 9b5c5e1..6c8198b 100644
--- a/testsuites/sptests/spintrcritical22/init.c
+++ b/testsuites/sptests/spintrcritical22/init.c
@@ -48,12 +48,13 @@ static void release_semaphore(rtems_id timer, void *arg)
   /* The arg is NULL */
   test_context *ctx = &ctx_instance;
   rtems_status_code sc;
-  CORE_mutex_Control *mtx = &ctx->semaphore_control->Core_control.mutex;
 
   if (
     _Thread_Wait_flags_get(ctx->main_task_control)
       == (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_INTEND_TO_BLOCK)
   ) {
+    CORE_semaphore_Control *sem;
+
     ctx->done = true;
 
     sc = rtems_semaphore_release(ctx->semaphore_id);
@@ -63,8 +64,8 @@ static void release_semaphore(rtems_id timer, void *arg)
       _Thread_Wait_flags_get(ctx->main_task_control)
         == (THREAD_WAIT_CLASS_OBJECT | THREAD_WAIT_STATE_READY_AGAIN)
     );
-    rtems_test_assert(mtx->nest_count == 1);
-    rtems_test_assert(mtx->holder == ctx->main_task_control);
+    sem = &ctx->semaphore_control->Core_control.semaphore;
+    rtems_test_assert(sem->count == 0);
   } else {
     sc = rtems_semaphore_release(ctx->semaphore_id);
     rtems_test_assert(sc == RTEMS_SUCCESSFUL);
-- 
1.8.4.5




More information about the devel mailing list