summaryrefslogtreecommitdiffstats
path: root/cpukit/score
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2016-05-13 08:16:30 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2016-05-20 07:49:39 +0200
commit54550e048d3a49435912797d2024f80671e93267 (patch)
treebf49901187d98cf6a71975bdef7038d3ae0988c2 /cpukit/score
parentscore: Simplify _Thread_Life_action_handler() (diff)
downloadrtems-54550e048d3a49435912797d2024f80671e93267.tar.bz2
posix: Rework pthread_join()
Rework pthread_join() to use _Thread_Join(). Close #2402. Update #2555. Update #2626. Close #2714.
Diffstat (limited to 'cpukit/score')
-rw-r--r--cpukit/score/include/rtems/score/statesimpl.h7
-rw-r--r--cpukit/score/include/rtems/score/thread.h15
-rw-r--r--cpukit/score/include/rtems/score/threadimpl.h20
-rw-r--r--cpukit/score/src/threadrestart.c92
4 files changed, 127 insertions, 7 deletions
diff --git a/cpukit/score/include/rtems/score/statesimpl.h b/cpukit/score/include/rtems/score/statesimpl.h
index a560329a17..54052e2b74 100644
--- a/cpukit/score/include/rtems/score/statesimpl.h
+++ b/cpukit/score/include/rtems/score/statesimpl.h
@@ -385,6 +385,13 @@ RTEMS_INLINE_ROUTINE bool _States_Is_waiting_for_period (
return (the_states & STATES_WAITING_FOR_PERIOD);
}
+RTEMS_INLINE_ROUTINE bool _States_Is_waiting_for_join_at_exit(
+ States_Control the_states
+)
+{
+ return ( the_states & STATES_WAITING_FOR_JOIN_AT_EXIT ) != 0;
+}
+
/**
* This function returns true if the task's state is set in
* way that allows it to be interrupted by a signal.
diff --git a/cpukit/score/include/rtems/score/thread.h b/cpukit/score/include/rtems/score/thread.h
index dce3d3bd38..352fb2e434 100644
--- a/cpukit/score/include/rtems/score/thread.h
+++ b/cpukit/score/include/rtems/score/thread.h
@@ -525,7 +525,8 @@ typedef struct {
typedef enum {
THREAD_LIFE_PROTECTED = 0x1,
THREAD_LIFE_RESTARTING = 0x2,
- THREAD_LIFE_TERMINATING = 0x4
+ THREAD_LIFE_TERMINATING = 0x4,
+ THREAD_LIFE_DETACHED = 0x10
} Thread_Life_state;
/**
@@ -547,6 +548,18 @@ typedef struct {
* @brief The count of pending life change requests.
*/
uint32_t pending_life_change_requests;
+
+#if defined(RTEMS_POSIX_API)
+ /**
+ * @brief The thread exit value.
+ *
+ * It is,
+ * - the value passed to pthread_exit(), or
+ * - PTHREAD_CANCELED in case it is cancelled via pthread_cancel(), or
+ * - NULL.
+ */
+ void *exit_value;
+#endif
} 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 e77352389c..cdd7f9b300 100644
--- a/cpukit/score/include/rtems/score/threadimpl.h
+++ b/cpukit/score/include/rtems/score/threadimpl.h
@@ -217,7 +217,11 @@ Thread_Life_state _Thread_Set_life_protection( Thread_Life_state state );
*/
void _Thread_Kill_zombies( void );
-void _Thread_Exit( Thread_Control *executing );
+void _Thread_Exit(
+ Thread_Control *executing,
+ Thread_Life_state set,
+ void *exit_value
+);
void _Thread_Join(
Thread_Control *the_thread,
@@ -226,7 +230,11 @@ void _Thread_Join(
ISR_lock_Context *lock_context
);
-void _Thread_Cancel( Thread_Control *the_thread, Thread_Control *executing );
+void _Thread_Cancel(
+ Thread_Control *the_thread,
+ Thread_Control *executing,
+ void *exit_value
+);
/**
* @brief Closes the thread.
@@ -950,6 +958,14 @@ RTEMS_INLINE_ROUTINE bool _Thread_Is_life_changing(
& ( THREAD_LIFE_RESTARTING | THREAD_LIFE_TERMINATING ) ) != 0;
}
+RTEMS_INLINE_ROUTINE bool _Thread_Is_joinable(
+ const Thread_Control *the_thread
+)
+{
+ _Assert( _Thread_State_is_owner( the_thread ) );
+ return ( the_thread->Life.state & THREAD_LIFE_DETACHED ) == 0;
+}
+
/**
* @brief Returns true if the thread owns resources, and false otherwise.
*
diff --git a/cpukit/score/src/threadrestart.c b/cpukit/score/src/threadrestart.c
index 4c105af12a..fbda7adbb8 100644
--- a/cpukit/score/src/threadrestart.c
+++ b/cpukit/score/src/threadrestart.c
@@ -88,17 +88,45 @@ static void _Thread_Raise_real_priority(
typedef struct {
ISR_lock_Context Base;
+#if defined(RTEMS_POSIX_API)
+ void *exit_value;
+#endif
} Thread_Join_lock_context;
+#if defined(RTEMS_POSIX_API)
+static Thread_Control *_Thread_Join_flush_filter(
+ Thread_Control *the_thread,
+ Thread_queue_Queue *queue,
+ ISR_lock_Context *lock_context
+)
+{
+ Thread_Join_lock_context *join_lock_context;
+
+ join_lock_context = (Thread_Join_lock_context *) lock_context;
+
+ the_thread->Wait.return_argument = join_lock_context->exit_value;
+
+ return the_thread;
+}
+#endif
+
static void _Thread_Wake_up_joining_threads( Thread_Control *the_thread )
{
Thread_Join_lock_context join_lock_context;
+#if defined(RTEMS_POSIX_API)
+ join_lock_context.exit_value = the_thread->Life.exit_value;
+#endif
+
_Thread_State_acquire( the_thread, &join_lock_context.Base );
_Thread_queue_Flush_critical(
&the_thread->Join_queue.Queue,
THREAD_JOIN_TQ_OPERATIONS,
+#if defined(RTEMS_POSIX_API)
+ _Thread_Join_flush_filter,
+#else
_Thread_queue_Flush_default_filter,
+#endif
NULL,
0,
&join_lock_context.Base
@@ -256,6 +284,35 @@ static Thread_Life_state _Thread_Change_life_locked(
return previous;
}
+static Per_CPU_Control *_Thread_Wait_for_join(
+ Thread_Control *executing,
+ Per_CPU_Control *cpu_self
+)
+{
+#if defined(RTEMS_POSIX_API)
+ ISR_lock_Context lock_context;
+
+ _Thread_State_acquire( executing, &lock_context );
+
+ if (
+ _Thread_Is_joinable( executing )
+ && _Thread_queue_Is_empty( &executing->Join_queue.Queue )
+ ) {
+ _Thread_Set_state_locked( executing, STATES_WAITING_FOR_JOIN_AT_EXIT );
+ _Thread_State_release( executing, &lock_context );
+ _Thread_Dispatch_enable( cpu_self );
+
+ /* Let other threads run */
+
+ cpu_self = _Thread_Dispatch_disable();
+ } else {
+ _Thread_State_release( executing, &lock_context );
+ }
+#endif
+
+ return cpu_self;
+}
+
void _Thread_Life_action_handler(
Thread_Control *executing,
Thread_Action *action,
@@ -283,6 +340,8 @@ void _Thread_Life_action_handler(
cpu_self = _Thread_Dispatch_disable();
if ( _Thread_Is_life_terminating( previous_life_state ) ) {
+ cpu_self = _Thread_Wait_for_join( executing, cpu_self );
+
_Thread_Make_zombie( executing );
_Thread_Dispatch_enable( cpu_self );
@@ -391,6 +450,11 @@ void _Thread_Join(
_Assert( the_thread != executing );
_Assert( _Thread_State_is_owner( the_thread ) );
+#if defined(RTEMS_POSIX_API)
+ executing->Wait.return_code = 0;
+ executing->Wait.return_argument = NULL;
+#endif
+
_Thread_queue_Enqueue_critical(
&the_thread->Join_queue.Queue,
THREAD_JOIN_TQ_OPERATIONS,
@@ -402,7 +466,21 @@ void _Thread_Join(
);
}
-void _Thread_Cancel( Thread_Control *the_thread, Thread_Control *executing )
+static void _Thread_Set_exit_value(
+ Thread_Control *the_thread,
+ void *exit_value
+)
+{
+#if defined(RTEMS_POSIX_API)
+ the_thread->Life.exit_value = exit_value;
+#endif
+}
+
+void _Thread_Cancel(
+ Thread_Control *the_thread,
+ Thread_Control *executing,
+ void *exit_value
+)
{
ISR_lock_Context lock_context;
Thread_Life_state previous;
@@ -413,6 +491,7 @@ void _Thread_Cancel( Thread_Control *the_thread, Thread_Control *executing )
_Thread_State_acquire( the_thread, &lock_context );
+ _Thread_Set_exit_value( the_thread, exit_value );
previous = _Thread_Change_life_locked(
the_thread,
0,
@@ -454,10 +533,14 @@ void _Thread_Close( Thread_Control *the_thread, Thread_Control *executing )
executing,
&lock_context
);
- _Thread_Cancel( the_thread, executing );
+ _Thread_Cancel( the_thread, executing, NULL );
}
-void _Thread_Exit( Thread_Control *executing )
+void _Thread_Exit(
+ Thread_Control *executing,
+ Thread_Life_state set,
+ void *exit_value
+)
{
ISR_lock_Context lock_context;
@@ -470,10 +553,11 @@ void _Thread_Exit( Thread_Control *executing )
);
_Thread_State_acquire( executing, &lock_context );
+ _Thread_Set_exit_value( executing, exit_value );
_Thread_Change_life_locked(
executing,
0,
- THREAD_LIFE_TERMINATING,
+ set,
THREAD_LIFE_PROTECTED
);
_Thread_State_release( executing, &lock_context );