source: rtems/cpukit/posix/src/pthreadjoin.c @ ed24ed4e

5
Last change on this file since ed24ed4e was ed24ed4e, checked in by Sebastian Huber <sebastian.huber@…>, on 02/08/18 at 09:47:16

Use _Thread_Dispatch_direct()

Use _Thread_Dispatch_direct() for operations that block the executing
thread. This ensures that we get a fatal error
(INTERNAL_ERROR_BAD_THREAD_DISPATCH_DISABLE_LEVEL) if we try to block in
an invalid context, e.g. during system start or an interrupt handler.

  • Property mode set to 100644
File size: 2.7 KB
Line 
1/**
2 * @file
3 *
4 * @brief Suspends Execution of Calling Thread until Target Thread Terminates
5 * @ingroup POSIXAPI
6 */
7
8/*
9 *  16.1.3 Wait for Thread Termination, P1003.1c/Draft 10, p. 147
10 *
11 *  COPYRIGHT (c) 1989-2014.
12 *  On-Line Applications Research Corporation (OAR).
13 *
14 *  Copyright (c) 2016 embedded brains GmbH.
15 *
16 *  The license and distribution terms for this file may be
17 *  found in the file LICENSE in this distribution or at
18 *  http://www.rtems.org/license/LICENSE.
19 */
20
21#if HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <pthread.h>
26#include <errno.h>
27
28#include <rtems/posix/threadsup.h>
29#include <rtems/posix/posixapi.h>
30#include <rtems/score/threadimpl.h>
31#include <rtems/score/statesimpl.h>
32
33static int _POSIX_Threads_Join( pthread_t thread, void **value_ptr )
34{
35  Thread_Control       *the_thread;
36  Thread_queue_Context  queue_context;
37  Per_CPU_Control      *cpu_self;
38  Thread_Control       *executing;
39  void                 *value;
40
41  _Thread_queue_Context_initialize( &queue_context );
42  _Thread_queue_Context_set_enqueue_do_nothing_extra( &queue_context );
43  the_thread = _Thread_Get( thread, &queue_context.Lock_context.Lock_context );
44
45  if ( the_thread == NULL ) {
46    return ESRCH;
47  }
48
49  cpu_self = _Per_CPU_Get();
50  executing = _Per_CPU_Get_executing( cpu_self );
51
52  if ( executing == the_thread ) {
53    _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context );
54    return EDEADLK;
55  }
56
57  _Thread_State_acquire_critical(
58    the_thread,
59    &queue_context.Lock_context.Lock_context
60  );
61
62  if ( !_Thread_Is_joinable( the_thread ) ) {
63    _Thread_State_release( the_thread, &queue_context.Lock_context.Lock_context );
64    return EINVAL;
65  }
66
67  if ( _States_Is_waiting_for_join_at_exit( the_thread->current_state ) ) {
68    value = the_thread->Life.exit_value;
69    _Thread_Clear_state_locked( the_thread, STATES_WAITING_FOR_JOIN_AT_EXIT );
70    _Thread_Dispatch_disable_with_CPU(
71      cpu_self,
72      &queue_context.Lock_context.Lock_context
73    );
74    _Thread_State_release( the_thread, &queue_context.Lock_context.Lock_context );
75    _Thread_Dispatch_direct( cpu_self );
76  } else {
77    _Thread_Join(
78      the_thread,
79      STATES_INTERRUPTIBLE_BY_SIGNAL | STATES_WAITING_FOR_JOIN,
80      executing,
81      &queue_context
82    );
83
84    if ( _POSIX_Get_error_after_wait( executing ) != 0 ) {
85      _Assert( _POSIX_Get_error_after_wait( executing ) == EINTR );
86      return EINTR;
87    }
88
89    value = executing->Wait.return_argument;
90  }
91
92  if ( value_ptr != NULL ) {
93    *value_ptr = value;
94  }
95
96  return 0;
97}
98
99int pthread_join( pthread_t thread, void **value_ptr )
100{
101  int error;
102
103  do {
104    error = _POSIX_Threads_Join( thread, value_ptr );
105  } while ( error == EINTR );
106
107  return error;
108}
Note: See TracBrowser for help on using the repository browser.