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-2011. |
---|
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.com/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/system.h> |
---|
27 | #include <rtems/score/thread.h> |
---|
28 | #include <rtems/posix/pthread.h> |
---|
29 | |
---|
30 | int pthread_join( |
---|
31 | pthread_t thread, |
---|
32 | void **value_ptr |
---|
33 | ) |
---|
34 | { |
---|
35 | register Thread_Control *the_thread; |
---|
36 | POSIX_API_Control *api; |
---|
37 | Objects_Locations location; |
---|
38 | void *return_pointer; |
---|
39 | |
---|
40 | on_EINTR: |
---|
41 | the_thread = _Thread_Get( thread, &location ); |
---|
42 | switch ( location ) { |
---|
43 | |
---|
44 | case OBJECTS_LOCAL: |
---|
45 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
46 | |
---|
47 | if ( api->detachstate == PTHREAD_CREATE_DETACHED ) { |
---|
48 | _Thread_Enable_dispatch(); |
---|
49 | return EINVAL; |
---|
50 | } |
---|
51 | |
---|
52 | if ( _Thread_Is_executing( the_thread ) ) { |
---|
53 | _Thread_Enable_dispatch(); |
---|
54 | return EDEADLK; |
---|
55 | } |
---|
56 | |
---|
57 | /* |
---|
58 | * Put ourself on the threads join list |
---|
59 | */ |
---|
60 | |
---|
61 | if ( the_thread->current_state == |
---|
62 | (STATES_WAITING_FOR_JOIN_AT_EXIT | STATES_TRANSIENT) ) { |
---|
63 | return_pointer = the_thread->Wait.return_argument; |
---|
64 | _Thread_Clear_state( |
---|
65 | the_thread, |
---|
66 | (STATES_WAITING_FOR_JOIN_AT_EXIT | STATES_TRANSIENT) |
---|
67 | ); |
---|
68 | } else { |
---|
69 | _Thread_Executing->Wait.return_argument = &return_pointer; |
---|
70 | _Thread_queue_Enter_critical_section( &api->Join_List ); |
---|
71 | _Thread_queue_Enqueue( &api->Join_List, WATCHDOG_NO_TIMEOUT ); |
---|
72 | } |
---|
73 | _Thread_Enable_dispatch(); |
---|
74 | |
---|
75 | if ( _Thread_Executing->Wait.return_code == EINTR ) |
---|
76 | goto on_EINTR; |
---|
77 | |
---|
78 | if ( value_ptr ) |
---|
79 | *value_ptr = return_pointer; |
---|
80 | return 0; |
---|
81 | |
---|
82 | #if defined(RTEMS_MULTIPROCESSING) |
---|
83 | case OBJECTS_REMOTE: |
---|
84 | #endif |
---|
85 | case OBJECTS_ERROR: |
---|
86 | break; |
---|
87 | } |
---|
88 | |
---|
89 | return ESRCH; |
---|
90 | } |
---|