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

5
Last change on this file since dbb30e26 was 125f248, checked in by Sebastian Huber <sebastian.huber@…>, on 11/16/16 at 15:39:43

score: Add thread queue enqueue callout

Replace the expected thread dispatch disable level with a thread queue
enqueue callout. This enables the use of _Thread_Dispatch_direct() in
the thread queue enqueue procedure. This avoids impossible exection
paths, e.g. Per_CPU_Control::dispatch_necessary is always true.

  • 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_do_nothing_enqueue_callout( &queue_context );
43  _Thread_queue_Context_set_no_timeout( &queue_context );
44  the_thread = _Thread_Get( thread, &queue_context.Lock_context.Lock_context );
45
46  if ( the_thread == NULL ) {
47    return ESRCH;
48  }
49
50  cpu_self = _Per_CPU_Get();
51  executing = _Per_CPU_Get_executing( cpu_self );
52
53  if ( executing == the_thread ) {
54    _ISR_lock_ISR_enable( &queue_context.Lock_context.Lock_context );
55    return EDEADLK;
56  }
57
58  _Thread_State_acquire_critical(
59    the_thread,
60    &queue_context.Lock_context.Lock_context
61  );
62
63  if ( !_Thread_Is_joinable( the_thread ) ) {
64    _Thread_State_release( the_thread, &queue_context.Lock_context.Lock_context );
65    return EINVAL;
66  }
67
68  if ( _States_Is_waiting_for_join_at_exit( the_thread->current_state ) ) {
69    value = the_thread->Life.exit_value;
70    _Thread_Clear_state_locked( the_thread, STATES_WAITING_FOR_JOIN_AT_EXIT );
71    _Thread_Dispatch_disable_with_CPU(
72      cpu_self,
73      &queue_context.Lock_context.Lock_context
74    );
75    _Thread_State_release( the_thread, &queue_context.Lock_context.Lock_context );
76    _Thread_Dispatch_enable( cpu_self );
77  } else {
78    _Thread_Join(
79      the_thread,
80      STATES_INTERRUPTIBLE_BY_SIGNAL | STATES_WAITING_FOR_JOIN,
81      executing,
82      &queue_context
83    );
84
85    if ( _POSIX_Get_error_after_wait( executing ) != 0 ) {
86      _Assert( _POSIX_Get_error_after_wait( executing ) == EINTR );
87      return EINTR;
88    }
89
90    value = executing->Wait.return_argument;
91  }
92
93  if ( value_ptr != NULL ) {
94    *value_ptr = value;
95  }
96
97  return 0;
98}
99
100int pthread_join( pthread_t thread, void **value_ptr )
101{
102  int error;
103
104  do {
105    error = _POSIX_Threads_Join( thread, value_ptr );
106  } while ( error == EINTR );
107
108  return error;
109}
Note: See TracBrowser for help on using the repository browser.