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.
This commit is contained in:
Sebastian Huber
2016-05-13 07:04:13 +02:00
parent c99eb50b9f
commit 232147ddc1
5 changed files with 70 additions and 32 deletions

View File

@@ -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 },

View File

@@ -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. */

View File

@@ -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)

View File

@@ -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.
*

View File

@@ -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;
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;