[rtems commit] score: Add _Thread_Join() and _Thread_Cancel()

Sebastian Huber sebh at rtems.org
Fri May 20 05:57:52 UTC 2016


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

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

score: Add _Thread_Join() and _Thread_Cancel()

Split _Thread_Close() into _Thread_Join() and _Thread_Cancel() to
prepare for a re-use in pthread_join() and pthread_cancel().

Update #2555.
Update #2626.

---

 cpukit/libmisc/monitor/mon-prmisc.c           |  1 -
 cpukit/score/include/rtems/score/statesimpl.h |  2 -
 cpukit/score/include/rtems/score/thread.h     |  9 ---
 cpukit/score/include/rtems/score/threadimpl.h |  9 +++
 cpukit/score/src/threadrestart.c              | 81 ++++++++++++++++++++-------
 5 files changed, 70 insertions(+), 32 deletions(-)

diff --git a/cpukit/libmisc/monitor/mon-prmisc.c b/cpukit/libmisc/monitor/mon-prmisc.c
index 526103b..4c7ee9d 100644
--- a/cpukit/libmisc/monitor/mon-prmisc.c
+++ b/cpukit/libmisc/monitor/mon-prmisc.c
@@ -138,7 +138,6 @@ static const rtems_assoc_t rtems_monitor_state_assoc[] = {
     { "Wslmtx", STATES_WAITING_FOR_SYS_LOCK_MUTEX, 0 },
     { "Wslsem", STATES_WAITING_FOR_SYS_LOCK_SEMAPHORE, 0 },
     { "Wsysev", STATES_WAITING_FOR_SYSTEM_EVENT, 0 },
-    { "Wterm",  STATES_WAITING_FOR_TERMINATION, 0 },
     { "Wtime",  STATES_WAITING_FOR_TIME, 0 },
     { "Wwkup",  STATES_WAITING_FOR_BSD_WAKEUP, 0 },
     { "ZOMBI",  STATES_ZOMBIE, 0 },
diff --git a/cpukit/score/include/rtems/score/statesimpl.h b/cpukit/score/include/rtems/score/statesimpl.h
index f3c2293..8aebba0 100644
--- a/cpukit/score/include/rtems/score/statesimpl.h
+++ b/cpukit/score/include/rtems/score/statesimpl.h
@@ -76,8 +76,6 @@ extern "C" {
 #define STATES_WAITING_FOR_SYSTEM_EVENT        0x40000
 /** This macro corresponds to a task waiting for BSD wakeup. */
 #define STATES_WAITING_FOR_BSD_WAKEUP          0x80000
-/** This macro corresponds to a task waiting for a task termination. */
-#define STATES_WAITING_FOR_TERMINATION         0x100000
 /** This macro corresponds to a task being a zombie. */
 #define STATES_ZOMBIE                          0x200000
 /** This macro corresponds to a task restarting. */
diff --git a/cpukit/score/include/rtems/score/thread.h b/cpukit/score/include/rtems/score/thread.h
index d69967b..9b35b85 100644
--- a/cpukit/score/include/rtems/score/thread.h
+++ b/cpukit/score/include/rtems/score/thread.h
@@ -543,15 +543,6 @@ typedef struct {
    * @brief The current thread life state.
    */
   Thread_Life_state  state;
-
-  /**
-   * @brief The terminator thread of this thread.
-   *
-   * In case the thread is terminated and another thread (the terminator) waits
-   * for the actual termination completion, then this field references the
-   * terminator thread.
-   */
-  Thread_Control    *terminator;
 } Thread_Life_control;
 
 #if defined(RTEMS_SMP)
diff --git a/cpukit/score/include/rtems/score/threadimpl.h b/cpukit/score/include/rtems/score/threadimpl.h
index e68f119..eee896d 100644
--- a/cpukit/score/include/rtems/score/threadimpl.h
+++ b/cpukit/score/include/rtems/score/threadimpl.h
@@ -213,6 +213,15 @@ void _Thread_Kill_zombies( void );
 
 void _Thread_Exit( Thread_Control *executing );
 
+void _Thread_Join(
+  Thread_Control    *the_thread,
+  States_Control     waiting_for_join,
+  Thread_Control    *executing,
+  ISR_lock_Context  *lock_context
+);
+
+void _Thread_Cancel( Thread_Control *the_thread, Thread_Control *executing );
+
 /**
  * @brief Closes the thread.
  *
diff --git a/cpukit/score/src/threadrestart.c b/cpukit/score/src/threadrestart.c
index e2f82a3..9ab053c 100644
--- a/cpukit/score/src/threadrestart.c
+++ b/cpukit/score/src/threadrestart.c
@@ -32,6 +32,8 @@
 #include <rtems/score/watchdogimpl.h>
 #include <rtems/score/wkspace.h>
 
+#define THREAD_JOIN_TQ_OPERATIONS &_Thread_queue_Operations_priority
+
 static void _Thread_Life_action_handler(
   Thread_Control   *executing,
   Thread_Action    *action,
@@ -84,10 +86,29 @@ static void _Thread_Raise_real_priority(
   );
 }
 
+typedef struct {
+  ISR_lock_Context  Base;
+} Thread_Join_lock_context;
+
+static void _Thread_Wake_up_joining_threads( Thread_Control *the_thread )
+{
+  Thread_Join_lock_context join_lock_context;
+
+  _Thread_State_acquire( the_thread, &join_lock_context.Base );
+  _Thread_queue_Flush_critical(
+    &the_thread->Join_queue.Queue,
+    THREAD_JOIN_TQ_OPERATIONS,
+    _Thread_queue_Flush_default_filter,
+    NULL,
+    0,
+    &join_lock_context.Base
+  );
+}
+
 static void _Thread_Make_zombie( Thread_Control *the_thread )
 {
-  ISR_lock_Context lock_context;
-  Thread_Zombie_control *zombies = &_Thread_Zombies;
+  ISR_lock_Context       lock_context;
+  Thread_Zombie_control *zombies;
 
   if ( _Thread_Owns_resources( the_thread ) ) {
     _Terminate(
@@ -102,10 +123,12 @@ static void _Thread_Make_zombie( Thread_Control *the_thread )
     &the_thread->Object
   );
 
+  _Thread_Wake_up_joining_threads( the_thread );
   _Thread_Set_state( the_thread, STATES_ZOMBIE );
   _Thread_queue_Extract_with_proxy( the_thread );
   _Thread_Timer_remove( the_thread );
 
+  zombies = &_Thread_Zombies;
   _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context );
   _Chain_Append_unprotected( &zombies->Chain, &the_thread->Object.Node );
   _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context );
@@ -291,13 +314,6 @@ void _Thread_Life_action_handler(
   if ( _Thread_Is_life_terminating( previous_life_state ) ) {
     _Thread_Make_zombie( executing );
 
-    if ( executing->Life.terminator != NULL ) {
-      _Thread_Clear_state(
-        executing->Life.terminator,
-        STATES_WAITING_FOR_TERMINATION
-      );
-    }
-
     _Thread_Enable_dispatch();
     RTEMS_UNREACHABLE();
   } else {
@@ -373,23 +389,34 @@ static void _Thread_Request_life_change(
   }
 }
 
-void _Thread_Close( Thread_Control *the_thread, Thread_Control *executing )
+void _Thread_Join(
+  Thread_Control    *the_thread,
+  States_Control     waiting_for_join,
+  Thread_Control    *executing,
+  ISR_lock_Context  *lock_context
+)
+{
+  _Assert( the_thread != executing );
+  _Assert( _Thread_State_is_owner( the_thread ) );
+
+  _Thread_queue_Enqueue_critical(
+    &the_thread->Join_queue.Queue,
+    THREAD_JOIN_TQ_OPERATIONS,
+    executing,
+    waiting_for_join,
+    WATCHDOG_NO_TIMEOUT,
+    0,
+    lock_context
+  );
+}
+
+void _Thread_Cancel( Thread_Control *the_thread, Thread_Control *executing )
 {
   _Assert( the_thread != executing );
 
   if ( _States_Is_dormant( the_thread->current_state ) ) {
     _Thread_Make_zombie( the_thread );
   } else {
-    if ( !_Thread_Is_life_terminating( executing->Life.state ) ) {
-      /*
-       * Wait for termination of victim thread.  If the executing thread is
-       * also terminated, then do not wait.  This avoids potential cyclic
-       * dependencies and thus dead lock.
-       */
-       the_thread->Life.terminator = executing;
-       _Thread_Set_state( executing, STATES_WAITING_FOR_TERMINATION );
-    }
-
     _Thread_Request_life_change(
       the_thread,
       executing,
@@ -399,6 +426,20 @@ void _Thread_Close( Thread_Control *the_thread, Thread_Control *executing )
   }
 }
 
+void _Thread_Close( Thread_Control *the_thread, Thread_Control *executing )
+{
+  ISR_lock_Context lock_context;
+
+  _Thread_State_acquire( the_thread, &lock_context );
+  _Thread_Join(
+    the_thread,
+    STATES_WAITING_FOR_JOIN,
+    executing,
+    &lock_context
+  );
+  _Thread_Cancel( the_thread, executing );
+}
+
 void _Thread_Exit( Thread_Control *executing )
 {
   ISR_lock_Context lock_context;




More information about the vc mailing list