]> git.ipfire.org Git - thirdparty/glibc.git/blob - htl/pt-join.c
hurd: Bump remaining LGPL2+ htl licences to LGPL 2.1+
[thirdparty/glibc.git] / htl / pt-join.c
1 /* Wait for thread termination.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
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
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.
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
13 Lesser General Public License for more details.
14
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/>. */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stddef.h>
22
23 #include <pt-internal.h>
24
25 /* Make calling thread wait for termination of thread THREAD. Return
26 the exit status of the thread in *STATUS. */
27 int
28 pthread_join (pthread_t thread, void **status)
29 {
30 struct __pthread *pthread;
31 int err = 0;
32
33 /* Lookup the thread structure for THREAD. */
34 pthread = __pthread_getid (thread);
35 if (pthread == NULL)
36 return ESRCH;
37
38 __pthread_mutex_lock (&pthread->state_lock);
39 pthread_cleanup_push ((void (*)(void *)) __pthread_mutex_unlock,
40 &pthread->state_lock);
41
42 /* Rely on pthread_cond_wait being a cancellation point to make
43 pthread_join one too. */
44 while (pthread->state == PTHREAD_JOINABLE)
45 __pthread_cond_wait (&pthread->state_cond, &pthread->state_lock);
46
47 pthread_cleanup_pop (0);
48
49 switch (pthread->state)
50 {
51 case PTHREAD_EXITED:
52 /* THREAD has already exited. Salvage its exit status. */
53 if (status != NULL)
54 *status = pthread->status;
55
56 __pthread_mutex_unlock (&pthread->state_lock);
57
58 __pthread_dealloc (pthread);
59 break;
60
61 case PTHREAD_TERMINATED:
62 /* Pretend THREAD wasn't there in the first place. */
63 __pthread_mutex_unlock (&pthread->state_lock);
64 err = ESRCH;
65 break;
66
67 default:
68 /* Thou shalt not join non-joinable threads! */
69 __pthread_mutex_unlock (&pthread->state_lock);
70 err = EINVAL;
71 break;
72 }
73
74 return err;
75 }