[rtems commit] score: Rework _Thread_Restart_other()

Sebastian Huber sebh at rtems.org
Fri May 20 05:58:42 UTC 2016


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri May 13 11:22:39 2016 +0200

score: Rework _Thread_Restart_other()

Rework _Thread_Restart_other() to use _Thread_Change_life_locked().
Cope with concurrent change requests by means of a pending request
counter.

Update #2555.
Update #2626.

---

 cpukit/libmisc/monitor/mon-prmisc.c           |  2 +-
 cpukit/score/include/rtems/score/statesimpl.h |  4 +-
 cpukit/score/include/rtems/score/thread.h     |  5 ++
 cpukit/score/src/threadrestart.c              | 88 ++++++++++++++++++++++++---
 4 files changed, 87 insertions(+), 12 deletions(-)

diff --git a/cpukit/libmisc/monitor/mon-prmisc.c b/cpukit/libmisc/monitor/mon-prmisc.c
index 4c7ee9d..a49b084 100644
--- a/cpukit/libmisc/monitor/mon-prmisc.c
+++ b/cpukit/libmisc/monitor/mon-prmisc.c
@@ -116,7 +116,7 @@ rtems_monitor_dump_priority(rtems_task_priority priority)
 static const rtems_assoc_t rtems_monitor_state_assoc[] = {
     { "DELAY",  STATES_DELAYING, 0 },
     { "DORM",   STATES_DORMANT, 0 },
-    { "RESTA",  STATES_RESTARTING, 0 },
+    { "LIFE",   STATES_LIFE_IS_CHANGING, 0 },
     { "SUSP",   STATES_SUSPENDED, 0 },
     { "Wbar",   STATES_WAITING_FOR_BARRIER, 0 },
     { "Wbuf",   STATES_WAITING_FOR_BUFFER, 0 },
diff --git a/cpukit/score/include/rtems/score/statesimpl.h b/cpukit/score/include/rtems/score/statesimpl.h
index 8aebba0..a560329 100644
--- a/cpukit/score/include/rtems/score/statesimpl.h
+++ b/cpukit/score/include/rtems/score/statesimpl.h
@@ -78,8 +78,8 @@ extern "C" {
 #define STATES_WAITING_FOR_BSD_WAKEUP          0x80000
 /** This macro corresponds to a task being a zombie. */
 #define STATES_ZOMBIE                          0x200000
-/** This macro corresponds to a task restarting. */
-#define STATES_RESTARTING                      0x800000
+/** This macro corresponds to a task those life is changing. */
+#define STATES_LIFE_IS_CHANGING                0x800000
 /** This macro corresponds to a task waiting for a join. */
 #define STATES_WAITING_FOR_JOIN                0x1000000
 /** This macro corresponds to a task waiting for a <sys/lock.h> mutex. */
diff --git a/cpukit/score/include/rtems/score/thread.h b/cpukit/score/include/rtems/score/thread.h
index 9b35b85..6d41d4d 100644
--- a/cpukit/score/include/rtems/score/thread.h
+++ b/cpukit/score/include/rtems/score/thread.h
@@ -543,6 +543,11 @@ typedef struct {
    * @brief The current thread life state.
    */
   Thread_Life_state  state;
+
+  /**
+   * @brief The count of pending life change requests.
+   */
+  uint32_t pending_life_change_requests;
 } Thread_Life_control;
 
 #if defined(RTEMS_SMP)
diff --git a/cpukit/score/src/threadrestart.c b/cpukit/score/src/threadrestart.c
index 8adbfa2..4abfd55 100644
--- a/cpukit/score/src/threadrestart.c
+++ b/cpukit/score/src/threadrestart.c
@@ -368,7 +368,7 @@ static void _Thread_Start_life_change(
   the_thread->budget_algorithm = the_thread->Start.budget_algorithm;
   the_thread->budget_callout   = the_thread->Start.budget_callout;
 
-  _Thread_Set_state( the_thread, STATES_RESTARTING );
+  _Thread_Set_state( the_thread, STATES_LIFE_IS_CHANGING );
   _Thread_queue_Extract_with_proxy( the_thread );
   _Thread_Timer_remove( the_thread );
   _Thread_Raise_real_priority( the_thread, priority );
@@ -407,6 +407,64 @@ static void _Thread_Request_life_change(
   }
 }
 
+static void _Thread_Add_life_change_request( Thread_Control *the_thread )
+{
+  uint32_t pending_requests;
+
+  _Assert( _Thread_State_is_owner( the_thread ) );
+
+  pending_requests = the_thread->Life.pending_life_change_requests;
+  the_thread->Life.pending_life_change_requests = pending_requests + 1;
+
+  if ( pending_requests == 0 ) {
+    _Thread_Set_state_locked( the_thread, STATES_LIFE_IS_CHANGING );
+  }
+}
+
+static void _Thread_Remove_life_change_request( Thread_Control *the_thread )
+{
+  ISR_lock_Context lock_context;
+  uint32_t         pending_requests;
+
+  _Thread_State_acquire( the_thread, &lock_context );
+
+  pending_requests = the_thread->Life.pending_life_change_requests;
+  the_thread->Life.pending_life_change_requests = pending_requests - 1;
+
+  if ( pending_requests == 1 ) {
+    /*
+     * Do not remove states used for thread queues to avoid race conditions on
+     * SMP configurations.  We could interrupt an extract operation on another
+     * processor disregarding the thread wait flags.  Rely on
+     * _Thread_queue_Extract_with_proxy() for removal of these states.
+     */
+    _Thread_Clear_state_locked(
+      the_thread,
+      STATES_LIFE_IS_CHANGING | STATES_SUSPENDED
+        | ( STATES_BLOCKED & ~STATES_LOCALLY_BLOCKED )
+    );
+  }
+
+  _Thread_State_release( the_thread, &lock_context );
+}
+
+static void _Thread_Finalize_life_change(
+  Thread_Control   *the_thread,
+  Priority_Control  priority
+)
+{
+  _Thread_queue_Extract_with_proxy( the_thread );
+  _Thread_Timer_remove( the_thread );
+  _Thread_Change_priority(
+    the_thread,
+    priority,
+    NULL,
+    _Thread_Raise_real_priority_filter,
+    false
+  );
+  _Thread_Remove_life_change_request( the_thread );
+}
+
 void _Thread_Join(
   Thread_Control    *the_thread,
   States_Control     waiting_for_join,
@@ -486,7 +544,8 @@ bool _Thread_Restart_other(
   ISR_lock_Context               *lock_context
 )
 {
-  Per_CPU_Control *cpu_self;
+  Thread_Life_state  previous;
+  Per_CPU_Control   *cpu_self;
 
   _Thread_State_acquire_critical( the_thread, lock_context );
 
@@ -496,16 +555,27 @@ bool _Thread_Restart_other(
   }
 
   the_thread->Start.Entry = *entry;
+  previous = _Thread_Change_life_locked(
+    the_thread,
+    0,
+    THREAD_LIFE_RESTARTING,
+    0
+  );
 
   cpu_self = _Thread_Dispatch_disable_critical( lock_context );
-  _Thread_State_release( the_thread, lock_context );
 
-  _Thread_Request_life_change(
-    the_thread,
-    NULL,
-    the_thread->Start.initial_priority,
-    THREAD_LIFE_RESTARTING
-  );
+  if ( _Thread_Is_life_change_allowed( previous ) ) {
+    _Thread_Add_life_change_request( the_thread );
+    _Thread_State_release( the_thread, lock_context );
+
+    _Thread_Finalize_life_change(
+      the_thread,
+      the_thread->Start.initial_priority
+    );
+  } else {
+    _Thread_Clear_state_locked( the_thread, STATES_SUSPENDED );
+    _Thread_State_release( the_thread, lock_context );
+  }
 
   _Thread_Dispatch_enable( cpu_self );
   return true;



More information about the vc mailing list