1 | /* |
---|
2 | * 16.1.3 Wait for Thread Termination, P1003.1c/Draft 10, p. 147 |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2011. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #if HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <pthread.h> |
---|
19 | #include <errno.h> |
---|
20 | |
---|
21 | #include <rtems/system.h> |
---|
22 | #include <rtems/score/thread.h> |
---|
23 | #include <rtems/posix/pthread.h> |
---|
24 | |
---|
25 | int pthread_join( |
---|
26 | pthread_t thread, |
---|
27 | void **value_ptr |
---|
28 | ) |
---|
29 | { |
---|
30 | register Thread_Control *the_thread; |
---|
31 | POSIX_API_Control *api; |
---|
32 | Objects_Locations location; |
---|
33 | void *return_pointer; |
---|
34 | |
---|
35 | on_EINTR: |
---|
36 | the_thread = _Thread_Get( thread, &location ); |
---|
37 | switch ( location ) { |
---|
38 | |
---|
39 | case OBJECTS_LOCAL: |
---|
40 | api = the_thread->API_Extensions[ THREAD_API_POSIX ]; |
---|
41 | |
---|
42 | if ( api->detachstate == PTHREAD_CREATE_DETACHED ) { |
---|
43 | _Thread_Enable_dispatch(); |
---|
44 | return EINVAL; |
---|
45 | } |
---|
46 | |
---|
47 | if ( _Thread_Is_executing( the_thread ) ) { |
---|
48 | _Thread_Enable_dispatch(); |
---|
49 | return EDEADLK; |
---|
50 | } |
---|
51 | |
---|
52 | /* |
---|
53 | * Put ourself on the threads join list |
---|
54 | */ |
---|
55 | |
---|
56 | if ( the_thread->current_state == |
---|
57 | (STATES_WAITING_FOR_JOIN_AT_EXIT | STATES_TRANSIENT) ) { |
---|
58 | return_pointer = the_thread->Wait.return_argument; |
---|
59 | _Thread_Clear_state( |
---|
60 | the_thread, |
---|
61 | (STATES_WAITING_FOR_JOIN_AT_EXIT | STATES_TRANSIENT) |
---|
62 | ); |
---|
63 | _Thread_Enable_dispatch(); |
---|
64 | } else { |
---|
65 | _Thread_Executing->Wait.return_argument = &return_pointer; |
---|
66 | _Thread_queue_Enter_critical_section( &api->Join_List ); |
---|
67 | _Thread_queue_Enqueue( &api->Join_List, WATCHDOG_NO_TIMEOUT ); |
---|
68 | _Thread_Enable_dispatch(); |
---|
69 | |
---|
70 | if ( _Thread_Executing->Wait.return_code == EINTR ) |
---|
71 | goto on_EINTR; |
---|
72 | } |
---|
73 | |
---|
74 | if ( value_ptr ) |
---|
75 | *value_ptr = return_pointer; |
---|
76 | return 0; |
---|
77 | |
---|
78 | #if defined(RTEMS_MULTIPROCESSING) |
---|
79 | case OBJECTS_REMOTE: |
---|
80 | #endif |
---|
81 | case OBJECTS_ERROR: |
---|
82 | break; |
---|
83 | } |
---|
84 | |
---|
85 | return ESRCH; |
---|
86 | } |
---|