[PATCH 03/21] score: Simplify thread stack free

Sebastian Huber sebastian.huber at embedded-brains.de
Mon Dec 16 14:28:08 UTC 2019


Update #3835.
---
 cpukit/include/rtems/score/stackimpl.h  |  9 +++++++++
 cpukit/include/rtems/score/thread.h     |  9 +++++----
 cpukit/include/rtems/score/threadimpl.h | 11 -----------
 cpukit/score/src/threadinitialize.c     | 27 +++++++++------------------
 cpukit/score/src/threadrestart.c        |  3 ++-
 cpukit/score/src/threadstackfree.c      | 32 +++++++-------------------------
 6 files changed, 32 insertions(+), 59 deletions(-)

diff --git a/cpukit/include/rtems/score/stackimpl.h b/cpukit/include/rtems/score/stackimpl.h
index 6b14560c9b..f4671dea60 100644
--- a/cpukit/include/rtems/score/stackimpl.h
+++ b/cpukit/include/rtems/score/stackimpl.h
@@ -115,6 +115,15 @@ RTEMS_INLINE_ROUTINE size_t _Stack_Ensure_minimum (
  */
 void *_Stack_Allocate( size_t stack_size );
 
+/**
+ * @brief Free the stack area allocated by _Stack_Allocate().
+ *
+ * Do nothing if the stack area is NULL.
+ *
+ * @param stack_area The stack area to free, or NULL.
+ */
+void _Stack_Free( void *stack_area );
+
 /** @} */
 
 #ifdef __cplusplus
diff --git a/cpukit/include/rtems/score/thread.h b/cpukit/include/rtems/score/thread.h
index 5c62efc1f7..336be281bd 100644
--- a/cpukit/include/rtems/score/thread.h
+++ b/cpukit/include/rtems/score/thread.h
@@ -199,10 +199,11 @@ typedef struct {
   uint32_t                             isr_level;
   /** This field is the initial priority. */
   Priority_Control                     initial_priority;
-  #if defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
-    /** This field indicates whether the SuperCore allocated the stack. */
-    bool                                 core_allocated_stack;
-  #endif
+  /**
+   * @brief This field is a pointer to the allocated stack area, otherwise it
+   * is NULL.
+   */
+  void                                *allocated_stack;
   /** This field is the stack information. */
   Stack_Control                        Initial_stack;
   #if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
diff --git a/cpukit/include/rtems/score/threadimpl.h b/cpukit/include/rtems/score/threadimpl.h
index 937175f713..6382436fa1 100644
--- a/cpukit/include/rtems/score/threadimpl.h
+++ b/cpukit/include/rtems/score/threadimpl.h
@@ -130,17 +130,6 @@ void _Thread_Create_idle(void);
  */
 void _Thread_Start_multitasking( void ) RTEMS_NO_RETURN;
 
-/**
- * @brief Deallocates thread stack.
- *
- * Deallocate the Thread's stack.
- *
- * @param[out] the_thread The thread to deallocate the stack of.
- */
-void _Thread_Stack_Free(
-  Thread_Control *the_thread
-);
-
 /**
  * @brief Initializes thread.
  *
diff --git a/cpukit/score/src/threadinitialize.c b/cpukit/score/src/threadinitialize.c
index 3b04ed26ab..c6e8abf979 100644
--- a/cpukit/score/src/threadinitialize.c
+++ b/cpukit/score/src/threadinitialize.c
@@ -86,30 +86,21 @@ bool _Thread_Initialize(
       (char *) the_thread + add_on->source_offset;
   }
 
-  /*
-   *  Allocate and Initialize the stack for this thread.
-   */
-  #if !defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
+  /* Allocate the stack for this thread */
+#if defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
+  if ( stack_area == NULL ) {
+#endif
     stack_size = _Stack_Ensure_minimum( stack_size );
     stack_area = _Stack_Allocate( stack_size );
 
     if ( stack_area == NULL ) {
       return false;
     }
-  #else
-    if ( stack_area == NULL ) {
-      stack_size = _Stack_Ensure_minimum( stack_size );
-      stack_area = _Stack_Allocate( stack_size );
-
-      if ( stack_area == NULL ) {
-        return false;
-      }
 
-      the_thread->Start.core_allocated_stack = true;
-    } else {
-      the_thread->Start.core_allocated_stack = false;
-    }
-  #endif
+    the_thread->Start.allocated_stack = stack_area;
+#if defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
+  }
+#endif
 
   _Stack_Initialize(
      &the_thread->Start.Initial_stack,
@@ -320,6 +311,6 @@ failed:
     _Workspace_Free( fp_area );
   #endif
 
-   _Thread_Stack_Free( the_thread );
+  _Stack_Free( the_thread->Start.allocated_stack );
   return false;
 }
diff --git a/cpukit/score/src/threadrestart.c b/cpukit/score/src/threadrestart.c
index 83aef28c30..6ff9b44515 100644
--- a/cpukit/score/src/threadrestart.c
+++ b/cpukit/score/src/threadrestart.c
@@ -26,6 +26,7 @@
 #include <rtems/score/chainimpl.h>
 #include <rtems/score/isrlock.h>
 #include <rtems/score/schedulerimpl.h>
+#include <rtems/score/stackimpl.h>
 #include <rtems/score/sysstate.h>
 #include <rtems/score/threadqimpl.h>
 #include <rtems/score/userextimpl.h>
@@ -184,7 +185,7 @@ static void _Thread_Free( Thread_Control *the_thread )
    *  Free the rest of the memory associated with this task
    *  and set the associated pointers to NULL for safety.
    */
-  _Thread_Stack_Free( the_thread );
+  _Stack_Free( the_thread->Start.allocated_stack );
 
   _Workspace_Free( the_thread->Start.tls_area );
 
diff --git a/cpukit/score/src/threadstackfree.c b/cpukit/score/src/threadstackfree.c
index 312a10c7f3..8d8e2296a7 100644
--- a/cpukit/score/src/threadstackfree.c
+++ b/cpukit/score/src/threadstackfree.c
@@ -1,8 +1,9 @@
 /**
- *  @file
+ * @file
  *
- *  @brief Deallocate Thread Stack
- *  @ingroup RTEMSScoreThread
+ * @ingroup RTEMSScoreStack
+ *
+ * @brief Deallocate Thread Stack
  */
 
 /*
@@ -18,29 +19,10 @@
 #include "config.h"
 #endif
 
-#include <rtems/score/threadimpl.h>
+#include <rtems/score/stackimpl.h>
 #include <rtems/config.h>
 
-void _Thread_Stack_Free(
-  Thread_Control *the_thread
-)
+void _Stack_Free( void *stack_area )
 {
-  rtems_stack_free_hook stack_free_hook =
-    rtems_configuration_get_stack_free_hook();
-
-  #if defined(RTEMS_SCORE_THREAD_ENABLE_USER_PROVIDED_STACK_VIA_API)
-    /*
-     *  If the API provided the stack space, then don't free it.
-     */
-    if ( !the_thread->Start.core_allocated_stack )
-      return;
-  #endif
-
-  /*
-   * Call ONLY the CPU table stack free hook, or the
-   * the RTEMS workspace free.  This is so the free
-   * routine properly matches the allocation of the stack.
-   */
-
-  (*stack_free_hook)( the_thread->Start.Initial_stack.area );
+  ( *rtems_configuration_get_stack_free_hook() )( stack_area );
 }
-- 
2.16.4



More information about the devel mailing list