1 /* Wait for thread termination.
2    Copyright (C) 2000-2021 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    <https://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 static int
__pthread_join_common(pthread_t thread,void ** status,int try,clockid_t clockid,const struct timespec * abstime)28 __pthread_join_common (pthread_t thread, void **status, int try,
29 		       clockid_t clockid,
30 		       const struct timespec *abstime)
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   if (pthread == _pthread_self ())
41     return EDEADLK;
42 
43   __pthread_mutex_lock (&pthread->state_lock);
44 
45   if (try == 0)
46     {
47       pthread_cleanup_push ((void (*)(void *)) __pthread_mutex_unlock,
48 			    &pthread->state_lock);
49 
50       /* Rely on pthread_cond_wait being a cancellation point to make
51 	 pthread_join one too.  */
52       while (pthread->state == PTHREAD_JOINABLE && err != ETIMEDOUT)
53 	err = __pthread_cond_clockwait (&pthread->state_cond,
54 					&pthread->state_lock,
55 					clockid, abstime);
56 
57       pthread_cleanup_pop (0);
58     }
59 
60   switch (pthread->state)
61     {
62     case PTHREAD_JOINABLE:
63       __pthread_mutex_unlock (&pthread->state_lock);
64       if (err != ETIMEDOUT)
65 	err = EBUSY;
66       break;
67 
68     case PTHREAD_EXITED:
69       /* THREAD has already exited.  Salvage its exit status.  */
70       if (status != NULL)
71 	*status = pthread->status;
72 
73       __pthread_mutex_unlock (&pthread->state_lock);
74 
75       __pthread_dealloc (pthread);
76       break;
77 
78     case PTHREAD_TERMINATED:
79       /* Pretend THREAD wasn't there in the first place.  */
80       __pthread_mutex_unlock (&pthread->state_lock);
81       err = ESRCH;
82       break;
83 
84     default:
85       /* Thou shalt not join non-joinable threads!  */
86       __pthread_mutex_unlock (&pthread->state_lock);
87       err = EINVAL;
88       break;
89     }
90 
91   return err;
92 }
93 
94 int
__pthread_join(pthread_t thread,void ** status)95 __pthread_join (pthread_t thread, void **status)
96 {
97   return __pthread_join_common (thread, status, 0, CLOCK_REALTIME, NULL);
98 }
99 weak_alias (__pthread_join, pthread_join);
100 
101 int
__pthread_tryjoin_np(pthread_t thread,void ** status)102 __pthread_tryjoin_np (pthread_t thread, void **status)
103 {
104   return __pthread_join_common (thread, status, 1, CLOCK_REALTIME, NULL);
105 }
106 weak_alias (__pthread_tryjoin_np, pthread_tryjoin_np);
107 
108 int
__pthread_timedjoin_np(pthread_t thread,void ** status,const struct timespec * abstime)109 __pthread_timedjoin_np (pthread_t thread, void **status,
110 			const struct timespec *abstime)
111 {
112   return __pthread_join_common (thread, status, 0, CLOCK_REALTIME, abstime);
113 }
114 weak_alias (__pthread_timedjoin_np, pthread_timedjoin_np);
115 
116 int
__pthread_clockjoin_np(pthread_t thread,void ** status,clockid_t clockid,const struct timespec * abstime)117 __pthread_clockjoin_np (pthread_t thread, void **status,
118 			clockid_t clockid,
119 			const struct timespec *abstime)
120 {
121   return __pthread_join_common (thread, status, 0, clockid, abstime);
122 }
123 weak_alias (__pthread_clockjoin_np, pthread_clockjoin_np);
124