| 1 | /* Common definition for pthread_{timed,try}join{_np}. | 
|---|
| 2 | Copyright (C) 2017-2020 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 "pthreadP.h" | 
|---|
| 20 | #include <atomic.h> | 
|---|
| 21 | #include <stap-probe.h> | 
|---|
| 22 | #include <time.h> | 
|---|
| 23 |  | 
|---|
| 24 | static void | 
|---|
| 25 | cleanup (void *arg) | 
|---|
| 26 | { | 
|---|
| 27 | /* If we already changed the waiter ID, reset it.  The call cannot | 
|---|
| 28 | fail for any reason but the thread not having done that yet so | 
|---|
| 29 | there is no reason for a loop.  */ | 
|---|
| 30 | struct pthread *self = THREAD_SELF; | 
|---|
| 31 | atomic_compare_exchange_weak_acquire (&arg, &self, NULL); | 
|---|
| 32 | } | 
|---|
| 33 |  | 
|---|
| 34 | /* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via futex | 
|---|
| 35 | wake-up when the clone terminates.  The memory location contains the | 
|---|
| 36 | thread ID while the clone is running and is reset to zero by the kernel | 
|---|
| 37 | afterwards.  The kernel up to version 3.16.3 does not use the private futex | 
|---|
| 38 | operations for futex wake-up when the clone terminates.  */ | 
|---|
| 39 | static int | 
|---|
| 40 | clockwait_tid (pid_t *tidp, clockid_t clockid, const struct timespec *abstime) | 
|---|
| 41 | { | 
|---|
| 42 | pid_t tid; | 
|---|
| 43 |  | 
|---|
| 44 | if (! valid_nanoseconds (abstime->tv_nsec)) | 
|---|
| 45 | return EINVAL; | 
|---|
| 46 |  | 
|---|
| 47 | /* Repeat until thread terminated.  */ | 
|---|
| 48 | while ((tid = *tidp) != 0) | 
|---|
| 49 | { | 
|---|
| 50 | struct timespec rt; | 
|---|
| 51 |  | 
|---|
| 52 | /* Get the current time. This can only fail if clockid is | 
|---|
| 53 | invalid. */ | 
|---|
| 54 | if (__glibc_unlikely (__clock_gettime (clockid, &rt))) | 
|---|
| 55 | return EINVAL; | 
|---|
| 56 |  | 
|---|
| 57 | /* Compute relative timeout.  */ | 
|---|
| 58 | rt.tv_sec = abstime->tv_sec - rt.tv_sec; | 
|---|
| 59 | rt.tv_nsec = abstime->tv_nsec - rt.tv_nsec; | 
|---|
| 60 | if (rt.tv_nsec < 0) | 
|---|
| 61 | { | 
|---|
| 62 | rt.tv_nsec += 1000000000; | 
|---|
| 63 | --rt.tv_sec; | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | /* Already timed out?  */ | 
|---|
| 67 | if (rt.tv_sec < 0) | 
|---|
| 68 | return ETIMEDOUT; | 
|---|
| 69 |  | 
|---|
| 70 | /* If *tidp == tid, wait until thread terminates or the wait times out. | 
|---|
| 71 | The kernel up to version 3.16.3 does not use the private futex | 
|---|
| 72 | operations for futex wake-up when the clone terminates.  */ | 
|---|
| 73 | if (lll_futex_timed_wait_cancel (tidp, tid, &rt, LLL_SHARED) | 
|---|
| 74 | == -ETIMEDOUT) | 
|---|
| 75 | return ETIMEDOUT; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | return 0; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | int | 
|---|
| 82 | __pthread_clockjoin_ex (pthread_t threadid, void **thread_return, | 
|---|
| 83 | clockid_t clockid, | 
|---|
| 84 | const struct timespec *abstime, bool block) | 
|---|
| 85 | { | 
|---|
| 86 | struct pthread *pd = (struct pthread *) threadid; | 
|---|
| 87 |  | 
|---|
| 88 | /* Make sure the descriptor is valid.  */ | 
|---|
| 89 | if (INVALID_NOT_TERMINATED_TD_P (pd)) | 
|---|
| 90 | /* Not a valid thread handle.  */ | 
|---|
| 91 | return ESRCH; | 
|---|
| 92 |  | 
|---|
| 93 | /* Is the thread joinable?.  */ | 
|---|
| 94 | if (IS_DETACHED (pd)) | 
|---|
| 95 | /* We cannot wait for the thread.  */ | 
|---|
| 96 | return EINVAL; | 
|---|
| 97 |  | 
|---|
| 98 | struct pthread *self = THREAD_SELF; | 
|---|
| 99 | int result = 0; | 
|---|
| 100 |  | 
|---|
| 101 | LIBC_PROBE (pthread_join, 1, threadid); | 
|---|
| 102 |  | 
|---|
| 103 | if ((pd == self | 
|---|
| 104 | || (self->joinid == pd | 
|---|
| 105 | && (pd->cancelhandling | 
|---|
| 106 | & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK | 
|---|
| 107 | | TERMINATED_BITMASK)) == 0)) | 
|---|
| 108 | && !CANCEL_ENABLED_AND_CANCELED (self->cancelhandling)) | 
|---|
| 109 | /* This is a deadlock situation.  The threads are waiting for each | 
|---|
| 110 | other to finish.  Note that this is a "may" error.  To be 100% | 
|---|
| 111 | sure we catch this error we would have to lock the data | 
|---|
| 112 | structures but it is not necessary.  In the unlikely case that | 
|---|
| 113 | two threads are really caught in this situation they will | 
|---|
| 114 | deadlock.  It is the programmer's problem to figure this | 
|---|
| 115 | out.  */ | 
|---|
| 116 | return EDEADLK; | 
|---|
| 117 |  | 
|---|
| 118 | /* Wait for the thread to finish.  If it is already locked something | 
|---|
| 119 | is wrong.  There can only be one waiter.  */ | 
|---|
| 120 | else if (__glibc_unlikely (atomic_compare_exchange_weak_acquire (&pd->joinid, | 
|---|
| 121 | &self, | 
|---|
| 122 | NULL))) | 
|---|
| 123 | /* There is already somebody waiting for the thread.  */ | 
|---|
| 124 | return EINVAL; | 
|---|
| 125 |  | 
|---|
| 126 | /* BLOCK waits either indefinitely or based on an absolute time.  POSIX also | 
|---|
| 127 | states a cancellation point shall occur for pthread_join, and we use the | 
|---|
| 128 | same rationale for posix_timedjoin_np.  Both clockwait_tid and the futex | 
|---|
| 129 | call use the cancellable variant.  */ | 
|---|
| 130 | if (block) | 
|---|
| 131 | { | 
|---|
| 132 | /* During the wait we change to asynchronous cancellation.  If we | 
|---|
| 133 | are cancelled the thread we are waiting for must be marked as | 
|---|
| 134 | un-wait-ed for again.  */ | 
|---|
| 135 | pthread_cleanup_push (cleanup, &pd->joinid); | 
|---|
| 136 |  | 
|---|
| 137 | if (abstime != NULL) | 
|---|
| 138 | result = clockwait_tid (&pd->tid, clockid, abstime); | 
|---|
| 139 | else | 
|---|
| 140 | { | 
|---|
| 141 | pid_t tid; | 
|---|
| 142 | /* We need acquire MO here so that we synchronize with the | 
|---|
| 143 | kernel's store to 0 when the clone terminates. (see above)  */ | 
|---|
| 144 | while ((tid = atomic_load_acquire (&pd->tid)) != 0) | 
|---|
| 145 | lll_futex_wait_cancel (&pd->tid, tid, LLL_SHARED); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | pthread_cleanup_pop (0); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | void *pd_result = pd->result; | 
|---|
| 152 | if (__glibc_likely (result == 0)) | 
|---|
| 153 | { | 
|---|
| 154 | /* We mark the thread as terminated and as joined.  */ | 
|---|
| 155 | pd->tid = -1; | 
|---|
| 156 |  | 
|---|
| 157 | /* Store the return value if the caller is interested.  */ | 
|---|
| 158 | if (thread_return != NULL) | 
|---|
| 159 | *thread_return = pd_result; | 
|---|
| 160 |  | 
|---|
| 161 | /* Free the TCB.  */ | 
|---|
| 162 | __free_tcb (pd); | 
|---|
| 163 | } | 
|---|
| 164 | else | 
|---|
| 165 | pd->joinid = NULL; | 
|---|
| 166 |  | 
|---|
| 167 | LIBC_PROBE (pthread_join_ret, 3, threadid, result, pd_result); | 
|---|
| 168 |  | 
|---|
| 169 | return result; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|