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 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.org/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #if HAVE_CONFIG_H |
---|
20 | #include "config.h" |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <pthread.h> |
---|
24 | #include <errno.h> |
---|
25 | |
---|
26 | #include <rtems/posix/pthreadimpl.h> |
---|
27 | #include <rtems/score/threadimpl.h> |
---|
28 | #include <rtems/score/threadqimpl.h> |
---|
29 | #include <rtems/score/statesimpl.h> |
---|
30 | |
---|
31 | int pthread_join( |
---|
32 | pthread_t thread, |
---|
33 | void **value_ptr |
---|
34 | ) |
---|
35 | { |
---|
36 | Thread_Control *the_thread; |
---|
37 | POSIX_API_Control *api; |
---|
38 | Objects_Locations location; |
---|
39 | void *return_pointer; |
---|
40 | Thread_Control *executing; |
---|
41 | |
---|
42 | on_EINTR: |
---|
43 | the_thread = _Thread_Get( thread, &location ); |
---|
44 | switch ( location ) { |
---|
45 | |
---|
46 | case OBJECTS_LOCAL: |
---|
47 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
48 | |
---|
49 | if ( api->detachstate == PTHREAD_CREATE_DETACHED ) { |
---|
50 | _Objects_Put( &the_thread->Object ); |
---|
51 | return EINVAL; |
---|
52 | } |
---|
53 | |
---|
54 | executing = _Thread_Executing; |
---|
55 | |
---|
56 | if ( executing == the_thread ) { |
---|
57 | _Objects_Put( &the_thread->Object ); |
---|
58 | return EDEADLK; |
---|
59 | } |
---|
60 | |
---|
61 | /* |
---|
62 | * Put ourself on the threads join list |
---|
63 | */ |
---|
64 | |
---|
65 | if ( the_thread->current_state == STATES_WAITING_FOR_JOIN_AT_EXIT ) { |
---|
66 | return_pointer = the_thread->Wait.return_argument; |
---|
67 | _Thread_Clear_state( the_thread, STATES_WAITING_FOR_JOIN_AT_EXIT ); |
---|
68 | } else { |
---|
69 | executing->Wait.return_argument = &return_pointer; |
---|
70 | _Thread_queue_Enqueue( |
---|
71 | &api->Join_List, |
---|
72 | POSIX_THREAD_JOIN_TQ_OPERATIONS, |
---|
73 | executing, |
---|
74 | STATES_WAITING_FOR_JOIN | STATES_INTERRUPTIBLE_BY_SIGNAL, |
---|
75 | WATCHDOG_NO_TIMEOUT, |
---|
76 | 0 |
---|
77 | ); |
---|
78 | } |
---|
79 | _Objects_Put( &the_thread->Object ); |
---|
80 | |
---|
81 | if ( executing->Wait.return_code == EINTR ) |
---|
82 | goto on_EINTR; |
---|
83 | |
---|
84 | if ( value_ptr ) |
---|
85 | *value_ptr = return_pointer; |
---|
86 | return 0; |
---|
87 | |
---|
88 | #if defined(RTEMS_MULTIPROCESSING) |
---|
89 | case OBJECTS_REMOTE: |
---|
90 | #endif |
---|
91 | case OBJECTS_ERROR: |
---|
92 | break; |
---|
93 | } |
---|
94 | |
---|
95 | return ESRCH; |
---|
96 | } |
---|