[PATCH 1/2] taskmode.c: Ensure all error checking is done before modifying executing

Joel Sherrill joel at rtems.org
Tue Dec 5 16:13:25 UTC 2017


Also use single conditional expressions to simplify error checking.
Combined, this all resulted in a block of SMP enabled error checking.

Updates #3000.
---
 cpukit/rtems/src/taskmode.c | 33 +++++++++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/cpukit/rtems/src/taskmode.c b/cpukit/rtems/src/taskmode.c
index a345409..49648ac 100644
--- a/cpukit/rtems/src/taskmode.c
+++ b/cpukit/rtems/src/taskmode.c
@@ -43,6 +43,33 @@ rtems_status_code rtems_task_mode(
   if ( !previous_mode_set )
     return RTEMS_INVALID_ADDRESS;
 
+#if defined( RTEMS_SMP )
+  /*
+   * When in SMP, you cannot disable preemption for a thread or 
+   * alter its interrupt level. It must be fully preemptible with
+   * all interrupts enabled.
+   */
+  if ( rtems_configuration_is_smp_enabled() ) {
+    if ( mask & RTEMS_PREEMPT_MASK ) {
+      if ( !_Modes_Is_preempt( mode_set ) ) {
+        return RTEMS_NOT_IMPLEMENTED;
+      }
+    }
+
+    if ( mask & RTEMS_INTERRUPT_MASK ) {
+      if (_Modes_Get_interrupt_level( mode_set ) != 0 ) {
+        return RTEMS_NOT_IMPLEMENTED;
+      }
+    }
+  }
+#endif
+
+  /*
+   * Complete all error checking before doing any operations which
+   * impact the executing thread. There should be no errors returned
+   * past this point.
+   */
+ 
   executing     = _Thread_Get_executing();
   api = executing->API_Extensions[ THREAD_API_RTEMS ];
   asr = &api->Signal;
@@ -63,18 +90,18 @@ rtems_status_code rtems_task_mode(
    *  These are generic thread scheduling characteristics.
    */
   preempt_enabled = false;
+#if !defined( RTEMS_SMP )
   if ( mask & RTEMS_PREEMPT_MASK ) {
-#if defined( RTEMS_SMP )
     if ( rtems_configuration_is_smp_enabled() &&
          !_Modes_Is_preempt( mode_set ) ) {
       return RTEMS_NOT_IMPLEMENTED;
     }
-#endif
     bool is_preempt_enabled = _Modes_Is_preempt( mode_set );
 
     preempt_enabled = !executing->is_preemptible && is_preempt_enabled;
     executing->is_preemptible = is_preempt_enabled;
   }
+#endif
 
   if ( mask & RTEMS_TIMESLICE_MASK ) {
     if ( _Modes_Is_timeslice(mode_set) ) {
@@ -88,8 +115,10 @@ rtems_status_code rtems_task_mode(
   /*
    *  Set the new interrupt level
    */
+#if !defined( RTEMS_SMP )
   if ( mask & RTEMS_INTERRUPT_MASK )
     _Modes_Set_interrupt_level( mode_set );
+#endif
 
   /*
    *  This is specific to the RTEMS API
-- 
1.8.3.1



More information about the devel mailing list