[1be3fad] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @brief Suspends Execution of Calling Thread until Target Thread Terminates |
---|
[5cb175bb] | 5 | * @ingroup POSIXAPI |
---|
[1be3fad] | 6 | */ |
---|
| 7 | |
---|
[03598b1] | 8 | /* |
---|
| 9 | * 16.1.3 Wait for Thread Termination, P1003.1c/Draft 10, p. 147 |
---|
| 10 | * |
---|
[e633f3b] | 11 | * COPYRIGHT (c) 1989-2014. |
---|
[03598b1] | 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 |
---|
[c499856] | 16 | * http://www.rtems.org/license/LICENSE. |
---|
[03598b1] | 17 | */ |
---|
| 18 | |
---|
[f42b726] | 19 | #if HAVE_CONFIG_H |
---|
| 20 | #include "config.h" |
---|
| 21 | #endif |
---|
[03598b1] | 22 | |
---|
| 23 | #include <pthread.h> |
---|
| 24 | #include <errno.h> |
---|
| 25 | |
---|
[0c5317d] | 26 | #include <rtems/posix/pthreadimpl.h> |
---|
[5618c37a] | 27 | #include <rtems/score/threadimpl.h> |
---|
[a112364] | 28 | #include <rtems/score/threadqimpl.h> |
---|
[9f10911] | 29 | #include <rtems/score/statesimpl.h> |
---|
[03598b1] | 30 | |
---|
| 31 | int pthread_join( |
---|
| 32 | pthread_t thread, |
---|
| 33 | void **value_ptr |
---|
| 34 | ) |
---|
| 35 | { |
---|
[e633f3b] | 36 | Thread_Control *the_thread; |
---|
[03598b1] | 37 | POSIX_API_Control *api; |
---|
| 38 | Objects_Locations location; |
---|
| 39 | void *return_pointer; |
---|
[768c5cee] | 40 | Thread_Control *executing; |
---|
[03598b1] | 41 | |
---|
[f05af6b7] | 42 | on_EINTR: |
---|
[e1fc2433] | 43 | the_thread = _Thread_Get( thread, &location ); |
---|
[03598b1] | 44 | switch ( location ) { |
---|
[860c34e] | 45 | |
---|
[03598b1] | 46 | case OBJECTS_LOCAL: |
---|
| 47 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
| 48 | |
---|
| 49 | if ( api->detachstate == PTHREAD_CREATE_DETACHED ) { |
---|
[2d2352b] | 50 | _Objects_Put( &the_thread->Object ); |
---|
[03598b1] | 51 | return EINVAL; |
---|
| 52 | } |
---|
| 53 | |
---|
[768c5cee] | 54 | executing = _Thread_Executing; |
---|
| 55 | |
---|
| 56 | if ( executing == the_thread ) { |
---|
[2d2352b] | 57 | _Objects_Put( &the_thread->Object ); |
---|
[03598b1] | 58 | return EDEADLK; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /* |
---|
| 62 | * Put ourself on the threads join list |
---|
| 63 | */ |
---|
| 64 | |
---|
[f39f667a] | 65 | if ( the_thread->current_state == STATES_WAITING_FOR_JOIN_AT_EXIT ) { |
---|
[25715ecc] | 66 | return_pointer = the_thread->Wait.return_argument; |
---|
[f39f667a] | 67 | _Thread_Clear_state( the_thread, STATES_WAITING_FOR_JOIN_AT_EXIT ); |
---|
[25715ecc] | 68 | } else { |
---|
[768c5cee] | 69 | executing->Wait.return_argument = &return_pointer; |
---|
[07332ae] | 70 | _Thread_queue_Enqueue( |
---|
| 71 | &api->Join_List, |
---|
[1e1a91ed] | 72 | POSIX_THREAD_JOIN_TQ_OPERATIONS, |
---|
[07332ae] | 73 | executing, |
---|
[f329353] | 74 | STATES_WAITING_FOR_JOIN | STATES_INTERRUPTIBLE_BY_SIGNAL, |
---|
[f5d6c8b] | 75 | WATCHDOG_NO_TIMEOUT, |
---|
| 76 | 0 |
---|
[07332ae] | 77 | ); |
---|
[25715ecc] | 78 | } |
---|
[2d2352b] | 79 | _Objects_Put( &the_thread->Object ); |
---|
[03598b1] | 80 | |
---|
[768c5cee] | 81 | if ( executing->Wait.return_code == EINTR ) |
---|
[f05af6b7] | 82 | goto on_EINTR; |
---|
| 83 | |
---|
[03598b1] | 84 | if ( value_ptr ) |
---|
| 85 | *value_ptr = return_pointer; |
---|
| 86 | return 0; |
---|
[860c34e] | 87 | |
---|
| 88 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 89 | case OBJECTS_REMOTE: |
---|
| 90 | #endif |
---|
| 91 | case OBJECTS_ERROR: |
---|
| 92 | break; |
---|
[03598b1] | 93 | } |
---|
| 94 | |
---|
[860c34e] | 95 | return ESRCH; |
---|
[03598b1] | 96 | } |
---|