summaryrefslogtreecommitdiffstats
path: root/cpukit/score/include/rtems
diff options
context:
space:
mode:
Diffstat (limited to 'cpukit/score/include/rtems')
-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
5 files changed, 195 insertions, 61 deletions
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
+ );
+}
+
/** @} */
/** @} */