[rtems commit] score: Simplify thread stack free

Sebastian Huber sebh at rtems.org
Wed Feb 12 15:11:58 UTC 2020


Module:    rtems
Branch:    master
Commit:    0bde56b1b4ef7468188712c86e6bd2793532ddc9
Changeset: http://git.rtems.org/rtems/commit/?id=0bde56b1b4ef7468188712c86e6bd2793532ddc9

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Sat Dec  7 16:28:21 2019 +0100

score: Simplify thread stack free

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 6b14560..f4671de 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 5c62efc..336be28 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 3ff6c94..1289a30 100644
--- a/cpukit/include/rtems/score/threadimpl.h
+++ b/cpukit/include/rtems/score/threadimpl.h
@@ -125,17 +125,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.
  *
  * This routine initializes the specified the thread.  It allocates
diff --git a/cpukit/score/src/threadinitialize.c b/cpukit/score/src/threadinitialize.c
index 3b04ed2..c6e8abf 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 83aef28..6ff9b44 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 312a10c..8d8e229 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 );
 }



More information about the vc mailing list