]>
Commit | Line | Data |
---|---|---|
33574c17 | 1 | /* Wait for thread termination. |
04277e02 | 2 | Copyright (C) 2000-2019 Free Software Foundation, Inc. |
33574c17 ST |
3 | This file is part of the GNU C Library. |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
ad2b41bf ST |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
33574c17 ST |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
ad2b41bf | 13 | Lesser General Public License for more details. |
33574c17 | 14 | |
ad2b41bf ST |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
33574c17 ST |
18 | |
19 | #include <errno.h> | |
20 | #include <pthread.h> | |
21 | #include <stddef.h> | |
22 | ||
23 | #include <pt-internal.h> | |
24 | ||
f96a85e0 ST |
25 | #define __pthread_get_cleanup_stack ___pthread_get_cleanup_stack |
26 | ||
33574c17 ST |
27 | /* Make calling thread wait for termination of thread THREAD. Return |
28 | the exit status of the thread in *STATUS. */ | |
29 | int | |
30 | pthread_join (pthread_t thread, void **status) | |
31 | { | |
32 | struct __pthread *pthread; | |
33 | int err = 0; | |
34 | ||
35 | /* Lookup the thread structure for THREAD. */ | |
36 | pthread = __pthread_getid (thread); | |
37 | if (pthread == NULL) | |
38 | return ESRCH; | |
39 | ||
40 | __pthread_mutex_lock (&pthread->state_lock); | |
41 | pthread_cleanup_push ((void (*)(void *)) __pthread_mutex_unlock, | |
42 | &pthread->state_lock); | |
43 | ||
44 | /* Rely on pthread_cond_wait being a cancellation point to make | |
45 | pthread_join one too. */ | |
46 | while (pthread->state == PTHREAD_JOINABLE) | |
47 | __pthread_cond_wait (&pthread->state_cond, &pthread->state_lock); | |
48 | ||
49 | pthread_cleanup_pop (0); | |
50 | ||
51 | switch (pthread->state) | |
52 | { | |
53 | case PTHREAD_EXITED: | |
54 | /* THREAD has already exited. Salvage its exit status. */ | |
55 | if (status != NULL) | |
56 | *status = pthread->status; | |
57 | ||
58 | __pthread_mutex_unlock (&pthread->state_lock); | |
59 | ||
60 | __pthread_dealloc (pthread); | |
61 | break; | |
62 | ||
63 | case PTHREAD_TERMINATED: | |
64 | /* Pretend THREAD wasn't there in the first place. */ | |
65 | __pthread_mutex_unlock (&pthread->state_lock); | |
66 | err = ESRCH; | |
67 | break; | |
68 | ||
69 | default: | |
70 | /* Thou shalt not join non-joinable threads! */ | |
71 | __pthread_mutex_unlock (&pthread->state_lock); | |
72 | err = EINVAL; | |
73 | break; | |
74 | } | |
75 | ||
76 | return err; | |
77 | } |