summaryrefslogtreecommitdiffstats
path: root/cpukit/score
diff options
context:
space:
mode:
Diffstat (limited to 'cpukit/score')
-rw-r--r--cpukit/score/Makefile.am2
-rw-r--r--cpukit/score/include/rtems/score/statesimpl.h4
-rw-r--r--cpukit/score/include/rtems/score/thread.h46
-rw-r--r--cpukit/score/include/rtems/score/threadimpl.h56
-rw-r--r--cpukit/score/include/rtems/score/userext.h134
-rw-r--r--cpukit/score/include/rtems/score/userextimpl.h16
-rw-r--r--cpukit/score/src/heapfree.c27
-rw-r--r--cpukit/score/src/threadclose.c101
-rw-r--r--cpukit/score/src/threadinitialize.c2
-rw-r--r--cpukit/score/src/threadrestart.c299
-rw-r--r--cpukit/score/src/userextiterate.c14
11 files changed, 511 insertions, 190 deletions
diff --git a/cpukit/score/Makefile.am b/cpukit/score/Makefile.am
index c6dd47b781..e2d23a4a69 100644
--- a/cpukit/score/Makefile.am
+++ b/cpukit/score/Makefile.am
@@ -272,7 +272,7 @@ libscore_a_SOURCES += src/rbtree.c \
## THREAD_C_FILES
libscore_a_SOURCES += src/thread.c src/threadchangepriority.c \
- src/threadclearstate.c src/threadclose.c src/threadcreateidle.c \
+ src/threadclearstate.c src/threadcreateidle.c \
src/threaddelayended.c src/threaddispatch.c \
src/threadenabledispatch.c src/threaddisabledispatch.c \
src/threadget.c src/threadhandler.c src/threadinitialize.c \
diff --git a/cpukit/score/include/rtems/score/statesimpl.h b/cpukit/score/include/rtems/score/statesimpl.h
index 913922b30c..842d108236 100644
--- a/cpukit/score/include/rtems/score/statesimpl.h
+++ b/cpukit/score/include/rtems/score/statesimpl.h
@@ -78,6 +78,10 @@ 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 which is in an interruptible
* blocking state.
diff --git a/cpukit/score/include/rtems/score/thread.h b/cpukit/score/include/rtems/score/thread.h
index d853aa035a..31fbbfa7d4 100644
--- a/cpukit/score/include/rtems/score/thread.h
+++ b/cpukit/score/include/rtems/score/thread.h
@@ -11,6 +11,8 @@
* COPYRIGHT (c) 1989-2014.
* On-Line Applications Research Corporation (OAR).
*
+ * Copyright (c) 2014 embedded brains GmbH.
+ *
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.org/license/LICENSE.
@@ -396,8 +398,47 @@ typedef struct {
Chain_Control Chain;
} Thread_Action_control;
+/**
+ * @brief Thread life states.
+ *
+ * The thread life states are orthogonal to the thread states used for
+ * synchronization primitives and blocking operations. They reflect the state
+ * changes triggered with thread restart and delete requests.
+ */
+typedef enum {
+ THREAD_LIFE_NORMAL = 0x0,
+ THREAD_LIFE_PROTECTED = 0x1,
+ THREAD_LIFE_RESTARTING = 0x2,
+ THREAD_LIFE_PROTECTED_RESTARTING = 0x3,
+ THREAD_LIFE_TERMINATING = 0x4,
+ THREAD_LIFE_PROTECTED_TERMINATING = 0x5,
+ THREAD_LIFE_RESTARTING_TERMINTING = 0x6,
+ THREAD_LIFE_PROTECTED_RESTARTING_TERMINTING = 0x7
+} Thread_Life_state;
+
+/**
+ * @brief Thread life control.
+ */
typedef struct {
+ /**
+ * @brief Thread life action used to react upon thread restart and delete
+ * requests.
+ */
Thread_Action Action;
+
+ /**
+ * @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;
/**
@@ -470,9 +511,10 @@ struct Thread_Control_struct {
* thread and thread dispatching is necessary. On SMP a thread dispatch on a
* remote processor needs help from an inter-processor interrupt, thus it
* will take some time to complete the state change. A lot of things can
- * happen in the meantime.
+ * happen in the meantime. This field is volatile since it is polled in
+ * _Thread_Kill_zombies().
*/
- bool is_executing;
+ volatile bool is_executing;
#if __RTEMS_HAVE_SYS_CPUSET_H__
/**
diff --git a/cpukit/score/include/rtems/score/threadimpl.h b/cpukit/score/include/rtems/score/threadimpl.h
index 2e31753817..d0c7933aa3 100644
--- a/cpukit/score/include/rtems/score/threadimpl.h
+++ b/cpukit/score/include/rtems/score/threadimpl.h
@@ -11,6 +11,8 @@
* COPYRIGHT (c) 1989-2008.
* On-Line Applications Research Corporation (OAR).
*
+ * Copyright (c) 2014 embedded brains GmbH.
+ *
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.org/license/LICENSE.
@@ -194,6 +196,8 @@ bool _Thread_Restart(
Thread_Entry_numeric_type numeric_argument
);
+bool _Thread_Set_life_protection( bool protect );
+
void _Thread_Life_action_handler(
Thread_Control *executing,
Thread_Action *action,
@@ -202,16 +206,24 @@ void _Thread_Life_action_handler(
);
/**
- * @brief Frees all memory associated with the specified thread.
+ * @brief Kills all zombie threads in the system.
*
- * This routine frees all memory associated with the specified
- * thread and removes it from the local object table so no further
- * operations on this thread are allowed.
+ * Threads change into the zombie state as the last step in the thread
+ * termination sequence right before a context switch to the heir thread is
+ * initiated. Since the thread stack is still in use during this phase we have
+ * to postpone the thread stack reclamation until this point. On SMP
+ * configurations we may have to busy wait for context switch completion here.
*/
-void _Thread_Close(
- Objects_Information *information,
- Thread_Control *the_thread
-);
+void _Thread_Kill_zombies( void );
+
+/**
+ * @brief Closes the thread.
+ *
+ * Closes the thread object and starts the thread termination sequence. In
+ * case the executing thread is not terminated, then this function waits until
+ * the terminating thread reached the zombie state.
+ */
+void _Thread_Close( Thread_Control *the_thread, Thread_Control *executing );
/**
* @brief Removes any set states for @a the_thread.
@@ -710,6 +722,34 @@ RTEMS_INLINE_ROUTINE void _Thread_Add_post_switch_action(
_Thread_Action_release_and_ISR_enable( cpu, level );
}
+RTEMS_INLINE_ROUTINE bool _Thread_Is_life_restarting(
+ Thread_Life_state life_state
+)
+{
+ return ( life_state & THREAD_LIFE_RESTARTING ) != 0;
+}
+
+RTEMS_INLINE_ROUTINE bool _Thread_Is_life_terminating(
+ Thread_Life_state life_state
+)
+{
+ return ( life_state & THREAD_LIFE_TERMINATING ) != 0;
+}
+
+RTEMS_INLINE_ROUTINE bool _Thread_Is_life_protected(
+ Thread_Life_state life_state
+)
+{
+ return ( life_state & THREAD_LIFE_PROTECTED ) != 0;
+}
+
+RTEMS_INLINE_ROUTINE bool _Thread_Is_life_changing(
+ Thread_Life_state life_state
+)
+{
+ return ( life_state & THREAD_LIFE_RESTARTING_TERMINTING ) != 0;
+}
+
#if !defined(__DYNAMIC_REENT__)
/**
* This routine returns the C library re-enterant pointer.
diff --git a/cpukit/score/include/rtems/score/userext.h b/cpukit/score/include/rtems/score/userext.h
index 91e7f97bd3..2bd8f8a88b 100644
--- a/cpukit/score/include/rtems/score/userext.h
+++ b/cpukit/score/include/rtems/score/userext.h
@@ -43,9 +43,7 @@ typedef void User_extensions_routine RTEMS_COMPILER_DEPRECATED_ATTRIBUTE;
* @brief Task create extension.
*
* It corresponds to _Thread_Initialize() (used by the rtems_task_create()
- * directive). The first parameter points to the currently executing thread
- * which created the new thread. The second parameter points to the created
- * thread.
+ * directive and pthread_create()).
*
* It is invoked after the new thread has been completely initialized, but
* before it is placed on a ready chain.
@@ -59,140 +57,173 @@ typedef void User_extensions_routine RTEMS_COMPILER_DEPRECATED_ATTRIBUTE;
*
* It can be assumed that the executing thread locked the allocator mutex.
* The only exception is the creation of the idle thread. In this case the
- * allocator mutex is not locked. Since the allocator mutex is non-recursive,
- * it is prohibited to call the normal memory allocation routines. It is
- * possible to use internal rountines like _Workspace_Allocate() or
- * _Heap_Allocate() for heaps which are protected by the allocator mutex.
+ * allocator mutex is not locked. Since the allocator mutex allows nesting the
+ * normal memory allocation routines can be used.
+ *
+ * @param[in] executing The executing thread.
+ * @param[in] created The created thread.
*
- * @retval true The thread create extension was successful.
+ * @retval true Successful operation.
* @retval false A thread create user extension will frequently attempt to
* allocate resources. If this allocation fails, then the extension should
* return @a false and the entire thread create operation will fail.
*/
typedef bool ( *User_extensions_thread_create_extension )(
- Thread_Control *,
- Thread_Control *
+ Thread_Control *executing,
+ Thread_Control *created
);
/**
* @brief Task delete extension.
*
* It corresponds to _Thread_Close() (used by the rtems_task_delete()
- * directive). The first parameter points to the currently executing thread
- * which deleted the thread. The second parameter points to the deleted
- * thread.
+ * directive, pthread_exit() and pthread_cancel()).
*
- * It is invoked before all resources of the thread are deleted.
+ * It is invoked before all resources of the thread are deleted. The executing
+ * and deleted arguments are never equal.
*
* Thread dispatching is enabled. The executing thread locked the allocator
* mutex.
+ *
+ * @param[in] executing The executing thread.
+ * @param[in] deleted The deleted thread.
*/
typedef void( *User_extensions_thread_delete_extension )(
- Thread_Control *,
- Thread_Control *
+ Thread_Control *executing,
+ Thread_Control *deleted
);
/**
* @brief Task start extension.
*
* It corresponds to _Thread_Start() (used by the rtems_task_start()
- * directive). The first parameter points to the currently executing thread
- * which started the thread. The second parameter points to the started
- * thread.
+ * directive).
*
* It is invoked after the environment of the thread has been loaded and the
* thread has been made ready.
*
* Thread dispatching is disabled. The executing thread is not the holder of
* the allocator mutex.
+ *
+ * @param[in] executing The executing thread.
+ * @param[in] started The started thread.
*/
typedef void( *User_extensions_thread_start_extension )(
- Thread_Control *,
- Thread_Control *
+ Thread_Control *executing,
+ Thread_Control *started
);
/**
* @brief Task restart extension.
*
* It corresponds to _Thread_Restart() (used by the rtems_task_restart()
- * directive). The first parameter points to the currently executing thread
- * which restarted the thread. The second parameter points to the restarted
- * thread.
+ * directive).
*
* It is invoked in the context of the restarted thread right before the
- * execution context is restarted. The executing and restarted arguments are
- * equal. The thread stack reflects the previous execution context.
+ * execution context is reloaded. The executing and restarted arguments are
+ * always equal. The thread stack reflects the previous execution context.
*
* Thread dispatching is enabled. The thread is not the holder of the
- * allocator mutex.
+ * allocator mutex. The thread life is protected. Thread restart and delete
+ * requests issued by restart extensions lead to recursion.
+ *
+ * @param[in] executing The executing thread.
+ * @param[in] restarted The executing thread. Yes, the executing thread.
*/
typedef void( *User_extensions_thread_restart_extension )(
- Thread_Control *,
- Thread_Control *
+ Thread_Control *executing,
+ Thread_Control *restarted
);
/**
* @brief Task switch extension.
*
- * It corresponds to _Thread_Dispatch(). The first parameter points to the
- * currently executing thread. The second parameter points to the heir thread.
+ * It corresponds to _Thread_Dispatch().
*
* It is invoked before the context switch from the executing to the heir
* thread.
*
* Thread dispatching is disabled. The state of the allocator mutex is
- * arbitrary.
+ * arbitrary. Interrupts are disabled and the per-CPU lock is acquired on SMP
+ * configurations.
*
- * The context switches initiated through _Thread_Start_multitasking() and
- * _Thread_Stop_multitasking() are not covered by this extension. The
- * executing thread may run with a minimal setup, for example with a freed task
- * stack.
+ * The context switches initiated through _Thread_Start_multitasking() are not
+ * covered by this extension.
+ *
+ * @param[in] executing The executing thread.
+ * @param[in] heir The heir thread.
*/
typedef void( *User_extensions_thread_switch_extension )(
- Thread_Control *,
- Thread_Control *
+ Thread_Control *executing,
+ Thread_Control *heir
);
/**
* @brief Task begin extension.
*
- * It corresponds to _Thread_Handler(). The first parameter points to the
- * currently executing thread which begins now execution.
+ * It corresponds to _Thread_Handler().
*
* Thread dispatching is disabled. The executing thread is not the holder of
* the allocator mutex.
+ *
+ * @param[in] executing The executing thread.
*/
typedef void( *User_extensions_thread_begin_extension )(
- Thread_Control *
+ Thread_Control *executing
);
/**
* @brief Task exitted extension.
*
- * It corresponds to _Thread_Handler(). The first parameter points to the
- * currently executing thread which exitted before.
+ * It corresponds to _Thread_Handler() after a return of the entry function.
*
* Thread dispatching is disabled. The state of the allocator mutex is
* arbitrary.
+ *
+ * @param[in] executing The executing thread.
*/
typedef void( *User_extensions_thread_exitted_extension )(
- Thread_Control *
+ Thread_Control *executing
);
/**
* @brief Fatal error extension.
*
- * It corresponds to _Terminate() (used by the
- * rtems_fatal_error_occurred() directive). The first parameter contains the
- * error source. The second parameter indicates if it was an internal error.
- * The third parameter contains the error code.
+ * It corresponds to _Terminate() (used by the rtems_fatal() directive).
*
* This extension should not call any RTEMS directives.
+ *
+ * @param[in] source The fatal source indicating the subsystem the fatal
+ * condition originated in.
+ * @param[in] is_internal Indicates if the fatal condition was generated
+ * internally to the executive.
+ * @param[in] code The fatal error code. This value must be interpreted with
+ * respect to the source.
*/
typedef void( *User_extensions_fatal_extension )(
- Internal_errors_Source,
- bool,
- Internal_errors_t
+ Internal_errors_Source source,
+ bool is_internal,
+ Internal_errors_t code
+);
+
+/**
+ * @brief Task termination extension.
+ *
+ * This extension is invoked by _Thread_Life_action_handler() in case a
+ * termination request is recognized.
+ *
+ * It is invoked in the context of the terminated thread right before the
+ * thread dispatch to the heir thread. The POSIX cleanup and key destructors
+ * execute in this context.
+ *
+ * Thread dispatching is enabled. The thread is not the holder of the
+ * allocator mutex. The thread life is protected. Thread restart and delete
+ * requests issued by terminate extensions lead to recursion.
+ *
+ * @param[in] terminated The terminated thread.
+ */
+typedef void( *User_extensions_thread_terminate_extension )(
+ Thread_Control *terminated
);
/**
@@ -207,6 +238,7 @@ typedef struct {
User_extensions_thread_begin_extension thread_begin;
User_extensions_thread_exitted_extension thread_exitted;
User_extensions_fatal_extension fatal;
+ User_extensions_thread_terminate_extension thread_terminate;
} User_extensions_Table;
/**
diff --git a/cpukit/score/include/rtems/score/userextimpl.h b/cpukit/score/include/rtems/score/userextimpl.h
index 25c6f00d0b..04808e1f17 100644
--- a/cpukit/score/include/rtems/score/userextimpl.h
+++ b/cpukit/score/include/rtems/score/userextimpl.h
@@ -142,6 +142,12 @@ void _User_extensions_Fatal_visitor(
const User_extensions_Table *callouts
);
+void _User_extensions_Thread_terminate_visitor(
+ Thread_Control *executing,
+ void *arg,
+ const User_extensions_Table *callouts
+);
+
/**
* @brief Iterates through all user extensions and calls the visitor for each.
*
@@ -239,6 +245,16 @@ static inline void _User_extensions_Fatal(
_User_extensions_Iterate( &ctx, _User_extensions_Fatal_visitor );
}
+static inline void _User_extensions_Thread_terminate(
+ Thread_Control *executing
+)
+{
+ _User_extensions_Iterate(
+ executing,
+ _User_extensions_Thread_terminate_visitor
+ );
+}
+
/** @} */
/** @} */
diff --git a/cpukit/score/src/heapfree.c b/cpukit/score/src/heapfree.c
index c45c294f3f..4e69146232 100644
--- a/cpukit/score/src/heapfree.c
+++ b/cpukit/score/src/heapfree.c
@@ -83,26 +83,13 @@
bool do_free = true;
Heap_Block *const next = block->Protection_begin.next_delayed_free_block;
- /*
- * Sometimes after a free the allocated area is still in use. An example
- * is the task stack of a thread that deletes itself. The thread dispatch
- * disable level is a way to detect this use case.
- */
- if ( _Thread_Dispatch_is_enabled() ) {
- if ( next == NULL ) {
- _Heap_Protection_delay_block_free( heap, block );
- do_free = false;
- } else if ( next == HEAP_PROTECTION_OBOLUS ) {
- _Heap_Protection_check_free_block( heap, block );
- } else {
- _Heap_Protection_block_error( heap, block );
- }
- } else if ( next == NULL ) {
- /*
- * This is a hack to prevent heavy workspace fragmentation which would
- * lead to test suite failures.
- */
- _Heap_Protection_free_all_delayed_blocks( heap );
+ if ( next == NULL ) {
+ _Heap_Protection_delay_block_free( heap, block );
+ do_free = false;
+ } else if ( next == HEAP_PROTECTION_OBOLUS ) {
+ _Heap_Protection_check_free_block( heap, block );
+ } else {
+ _Heap_Protection_block_error( heap, block );
}
return do_free;
diff --git a/cpukit/score/src/threadclose.c b/cpukit/score/src/threadclose.c
deleted file mode 100644
index 12896f5fee..0000000000
--- a/cpukit/score/src/threadclose.c
+++ /dev/null
@@ -1,101 +0,0 @@
-/**
- * @file
- *
- * @brief Thread Close
- * @ingroup ScoreThread
- */
-
-/*
- * COPYRIGHT (c) 1989-2011.
- * On-Line Applications Research Corporation (OAR).
- *
- * The license and distribution terms for this file may be
- * found in the file LICENSE in this distribution or at
- * http://www.rtems.org/license/LICENSE.
- */
-
-#if HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <rtems/score/threadimpl.h>
-#include <rtems/score/schedulerimpl.h>
-#include <rtems/score/threadqimpl.h>
-#include <rtems/score/userextimpl.h>
-#include <rtems/score/watchdogimpl.h>
-#include <rtems/score/wkspace.h>
-
-void _Thread_Close(
- Objects_Information *information,
- Thread_Control *the_thread
-)
-{
- /*
- * Now we are in a dispatching critical section again and we
- * can take the thread OUT of the published set. It is invalid
- * to use this thread's Id after this call. This will prevent
- * any other task from attempting to initiate a call on this task.
- */
- _Objects_Invalidate_Id( information, &the_thread->Object );
-
- /*
- * We assume the Allocator Mutex is locked when we get here.
- * This provides sufficient protection to let the user extensions
- * run but as soon as we get back, we will make the thread
- * disappear and set a transient state on it. So we temporarily
- * unnest dispatching.
- */
- _Thread_Unnest_dispatch();
-
- _User_extensions_Thread_delete( the_thread );
-
- _Thread_Disable_dispatch();
-
- /*
- * Now we are in a dispatching critical section again and we
- * can take the thread OUT of the published set. It is invalid
- * to use this thread's Id OR name after this call.
- */
- _Objects_Close( information, &the_thread->Object );
-
- /*
- * By setting the dormant state, the thread will not be considered
- * for scheduling when we remove any blocking states.
- */
- _Thread_Set_state( the_thread, STATES_DORMANT );
-
- if ( !_Thread_queue_Extract_with_proxy( the_thread ) ) {
- if ( _Watchdog_Is_active( &the_thread->Timer ) )
- (void) _Watchdog_Remove( &the_thread->Timer );
- }
-
- /*
- * Free the per-thread scheduling information.
- */
- _Scheduler_Free( the_thread );
-
- /*
- * The thread might have been FP. So deal with that.
- */
-#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
-#if ( CPU_USE_DEFERRED_FP_SWITCH == TRUE )
- if ( _Thread_Is_allocated_fp( the_thread ) )
- _Thread_Deallocate_fp();
-#endif
- the_thread->fp_context = NULL;
-
- _Workspace_Free( the_thread->Start.fp_context );
-#endif
-
- /*
- * Free the rest of the memory associated with this task
- * and set the associated pointers to NULL for safety.
- */
- _Thread_Stack_Free( the_thread );
- the_thread->Start.stack = NULL;
-
- _Workspace_Free( the_thread->extensions );
- the_thread->extensions = NULL;
-
- _Workspace_Free( the_thread->Start.tls_area );
-}
diff --git a/cpukit/score/src/threadinitialize.c b/cpukit/score/src/threadinitialize.c
index c851320936..fd52742c41 100644
--- a/cpukit/score/src/threadinitialize.c
+++ b/cpukit/score/src/threadinitialize.c
@@ -244,6 +244,8 @@ bool _Thread_Initialize(
&the_thread->Life.Action,
_Thread_Life_action_handler
);
+ the_thread->Life.state = THREAD_LIFE_NORMAL;
+ the_thread->Life.terminator = NULL;
/*
* Open the object
diff --git a/cpukit/score/src/threadrestart.c b/cpukit/score/src/threadrestart.c
index b8fbd2384f..985329f470 100644
--- a/cpukit/score/src/threadrestart.c
+++ b/cpukit/score/src/threadrestart.c
@@ -9,6 +9,8 @@
* COPYRIGHT (c) 1989-1999.
* On-Line Applications Research Corporation (OAR).
*
+ * Copyright (c) 2014 embedded brains GmbH.
+ *
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.org/license/LICENSE.
@@ -19,9 +21,129 @@
#endif
#include <rtems/score/threadimpl.h>
+#include <rtems/score/apimutex.h>
+#include <rtems/score/assert.h>
+#include <rtems/score/chainimpl.h>
+#include <rtems/score/isrlock.h>
+#include <rtems/score/schedulerimpl.h>
+#include <rtems/score/sysstate.h>
#include <rtems/score/threadqimpl.h>
#include <rtems/score/userextimpl.h>
#include <rtems/score/watchdogimpl.h>
+#include <rtems/score/wkspace.h>
+
+typedef struct {
+ Chain_Control Chain;
+ ISR_lock_Control Lock;
+} Thread_Zombie_control;
+
+static Thread_Zombie_control _Thread_Zombies = {
+ .Chain = CHAIN_INITIALIZER_EMPTY( _Thread_Zombies.Chain ),
+ .Lock = ISR_LOCK_INITIALIZER( "thread zombies" )
+};
+
+static void _Thread_Make_zombie( Thread_Control *the_thread )
+{
+ ISR_lock_Context lock_context;
+ Thread_Zombie_control *zombies = &_Thread_Zombies;
+
+ _Thread_Set_state( the_thread, STATES_ZOMBIE );
+ _Thread_queue_Extract_with_proxy( the_thread );
+ _Watchdog_Remove( &the_thread->Timer );
+
+ _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 );
+}
+
+static void _Thread_Free( Thread_Control *the_thread )
+{
+ _User_extensions_Thread_delete( the_thread );
+
+ /*
+ * Free the per-thread scheduling information.
+ */
+ _Scheduler_Free( the_thread );
+
+ /*
+ * The thread might have been FP. So deal with that.
+ */
+#if ( CPU_HARDWARE_FP == TRUE ) || ( CPU_SOFTWARE_FP == TRUE )
+#if ( CPU_USE_DEFERRED_FP_SWITCH == TRUE )
+ if ( _Thread_Is_allocated_fp( the_thread ) )
+ _Thread_Deallocate_fp();
+#endif
+
+ _Workspace_Free( the_thread->Start.fp_context );
+#endif
+
+ /*
+ * Free the rest of the memory associated with this task
+ * and set the associated pointers to NULL for safety.
+ */
+ _Thread_Stack_Free( the_thread );
+
+ _Workspace_Free( the_thread->extensions );
+
+ _Workspace_Free( the_thread->Start.tls_area );
+
+ _Objects_Free(
+ _Objects_Get_information_id( the_thread->Object.id ),
+ &the_thread->Object
+ );
+}
+
+static void _Thread_Wait_for_execution_stop( Thread_Control *the_thread )
+{
+#if defined(RTEMS_SMP)
+ /*
+ * It is very unlikely that we see an executing thread here. It can happen
+ * in case the thread termination sequence is interrupted by a slow interrupt
+ * service on a remote processor.
+ */
+ while (the_thread->is_executing) {
+ /* Wait */
+ }
+#else
+ (void) the_thread;
+#endif
+}
+
+void _Thread_Kill_zombies( void )
+{
+ ISR_lock_Context lock_context;
+ Thread_Zombie_control *zombies = &_Thread_Zombies;
+ Thread_Control *the_thread;
+
+ _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context );
+
+ the_thread = (Thread_Control *) _Chain_Get_unprotected( &zombies->Chain );
+ while ( the_thread != NULL ) {
+ _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context );
+
+ _Thread_Wait_for_execution_stop( the_thread );
+ _Thread_Free( the_thread );
+
+ _ISR_lock_ISR_disable_and_acquire( &zombies->Lock, &lock_context );
+
+ the_thread = (Thread_Control *) _Chain_Get_unprotected( &zombies->Chain );
+ }
+
+ _ISR_lock_Release_and_ISR_enable( &zombies->Lock, &lock_context );
+}
+
+static void _Thread_Start_life_change_for_executing(
+ Thread_Control *executing
+)
+{
+ _Assert( executing->Timer.state == WATCHDOG_INACTIVE );
+ _Assert(
+ executing->current_state == STATES_READY
+ || executing->current_state == STATES_SUSPENDED
+ );
+
+ _Thread_Add_post_switch_action( executing, &executing->Life.Action );
+}
void _Thread_Life_action_handler(
Thread_Control *executing,
@@ -30,15 +152,59 @@ void _Thread_Life_action_handler(
ISR_Level level
)
{
+ Thread_Life_state previous_life_state;
+
(void) action;
+
+ previous_life_state = executing->Life.state;
+ executing->Life.state = THREAD_LIFE_PROTECTED;
+
_Thread_Action_release_and_ISR_enable( cpu, level );
- _User_extensions_Thread_restart( the_thread );
+ if ( _Thread_Is_life_terminating( previous_life_state ) ) {
+ _User_extensions_Thread_terminate( executing );
+ } else {
+ _Assert( _Thread_Is_life_restarting( previous_life_state ) );
+
+ _User_extensions_Thread_restart( executing );
+ }
_Thread_Disable_dispatch();
- _Thread_Load_environment( executing );
- _Thread_Restart_self( executing );
+ 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();
+
+ _Assert_Not_reached();
+ } else {
+ _Assert( _Thread_Is_life_restarting( previous_life_state ) );
+
+ if ( _Thread_Is_life_terminating( executing->Life.state ) ) {
+ /* Someone deleted us in the mean-time */
+ _Thread_Start_life_change_for_executing( executing );
+ } else {
+ _Assert( executing->Timer.state == WATCHDOG_INACTIVE );
+ _Assert(
+ executing->current_state == STATES_READY
+ || executing->current_state == STATES_SUSPENDED
+ );
+
+ executing->Life.state = THREAD_LIFE_NORMAL;
+
+ _Thread_Load_environment( executing );
+ _Thread_Restart_self( executing );
+
+ _Assert_Not_reached();
+ }
+ }
}
static void _Thread_Start_life_change(
@@ -55,7 +221,7 @@ static void _Thread_Start_life_change(
_Thread_Set_transient( the_thread );
_Thread_queue_Extract_with_proxy( the_thread );
_Watchdog_Remove( &the_thread->Timer );
- _Thread_Set_priority( the_thread, priority );
+ _Scheduler_Set_priority_if_higher( the_thread, priority );
_Thread_Add_post_switch_action( the_thread, &the_thread->Life.Action );
_Thread_Ready( the_thread );
_Thread_Request_dispatch_if_executing( the_thread );
@@ -64,10 +230,72 @@ static void _Thread_Start_life_change(
static void _Thread_Request_life_change(
Thread_Control *the_thread,
Thread_Control *executing,
- Priority_Control priority
+ Priority_Control priority,
+ Thread_Life_state additional_life_state
)
{
- _Thread_Start_life_change( the_thread, priority );
+ Thread_Life_state previous_life_state;
+ Per_CPU_Control *cpu;
+ ISR_Level level;
+
+ cpu = _Thread_Action_ISR_disable_and_acquire( the_thread, &level );
+ previous_life_state = the_thread->Life.state;
+ the_thread->Life.state = previous_life_state | additional_life_state;
+ _Thread_Action_release_and_ISR_enable( cpu, level );
+
+ if ( the_thread == executing ) {
+ executing->real_priority = priority;
+
+ _Scheduler_Set_priority_if_higher( the_thread, priority );
+ _Thread_Start_life_change_for_executing( executing );
+ } else if ( previous_life_state == THREAD_LIFE_NORMAL ) {
+ _Thread_Start_life_change( the_thread, priority );
+ } else {
+ _Thread_Clear_state( the_thread, STATES_SUSPENDED );
+
+ if ( _Thread_Is_life_terminating( additional_life_state ) ) {
+ the_thread->real_priority = _Scheduler_Highest_priority_of_two(
+ the_thread->real_priority,
+ priority
+ );
+
+ _Scheduler_Change_priority_if_higher( the_thread, priority, false );
+ }
+ }
+}
+
+void _Thread_Close( Thread_Control *the_thread, Thread_Control *executing )
+{
+ _Assert( _Thread_Is_life_protected( executing->Life.state ) );
+
+ _Objects_Close(
+ _Objects_Get_information_id( the_thread->Object.id ),
+ &the_thread->Object
+ );
+
+ if ( _States_Is_dormant( the_thread->current_state ) ) {
+ _Thread_Make_zombie( the_thread );
+ } else {
+ if (
+ the_thread != executing
+ && !_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,
+ executing->current_priority,
+ THREAD_LIFE_TERMINATING
+ );
+ }
}
bool _Thread_Restart(
@@ -84,7 +312,8 @@ bool _Thread_Restart(
_Thread_Request_life_change(
the_thread,
executing,
- the_thread->Start.initial_priority
+ the_thread->Start.initial_priority,
+ THREAD_LIFE_RESTARTING
);
return true;
@@ -92,3 +321,59 @@ bool _Thread_Restart(
return false;
}
+
+bool _Thread_Set_life_protection( bool protect )
+{
+ bool previous_life_protection;
+ ISR_Level level;
+ Per_CPU_Control *cpu;
+ Thread_Control *executing;
+ Thread_Life_state previous_life_state;
+
+ cpu = _Thread_Action_ISR_disable_and_acquire_for_executing( &level );
+ executing = cpu->executing;
+
+ previous_life_state = executing->Life.state;
+ previous_life_protection = _Thread_Is_life_protected( previous_life_state );
+
+ if ( protect ) {
+ executing->Life.state = previous_life_state | THREAD_LIFE_PROTECTED;
+ } else {
+ executing->Life.state = previous_life_state & ~THREAD_LIFE_PROTECTED;
+ }
+
+ _Thread_Action_release_and_ISR_enable( cpu, level );
+
+#if defined(RTEMS_SMP)
+ /*
+ * On SMP configurations it is possible that a life change of an executing
+ * thread is requested, but this thread didn't notice it yet. The life
+ * change is first marked in the life state field and then all scheduling and
+ * other thread state updates are performed. The last step is to issues an
+ * inter-processor interrupt if necessary. Since this takes some time we
+ * have to synchronize here.
+ */
+ if (
+ !_Thread_Is_life_protected( previous_life_state )
+ && _Thread_Is_life_changing( previous_life_state )
+ ) {
+ _Thread_Disable_dispatch();
+ _Thread_Enable_dispatch();
+
+ _Assert_Not_reached();
+ }
+#endif
+
+ if (
+ !protect
+ && _Thread_Is_life_changing( previous_life_state )
+ ) {
+ _Thread_Disable_dispatch();
+ _Thread_Start_life_change_for_executing( executing );
+ _Thread_Enable_dispatch();
+
+ _Assert_Not_reached();
+ }
+
+ return previous_life_protection;
+}
diff --git a/cpukit/score/src/userextiterate.c b/cpukit/score/src/userextiterate.c
index c9125016de..beeee9570b 100644
--- a/cpukit/score/src/userextiterate.c
+++ b/cpukit/score/src/userextiterate.c
@@ -124,6 +124,20 @@ void _User_extensions_Fatal_visitor(
}
}
+void _User_extensions_Thread_terminate_visitor(
+ Thread_Control *executing,
+ void *arg,
+ const User_extensions_Table *callouts
+)
+{
+ User_extensions_thread_terminate_extension callout =
+ callouts->thread_terminate;
+
+ if ( callout != NULL ) {
+ (*callout)( executing );
+ }
+}
+
void _User_extensions_Iterate(
void *arg,
User_extensions_Visitor visitor