| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2002-2016 Free Software Foundation, Inc. | 
|  | 2 | This file is part of the GNU C Library. | 
|  | 3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | 
|  | 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 <stdlib.h> | 
|  | 21 |  | 
|  | 22 | #include <atomic.h> | 
|  | 23 | #include "pthreadP.h" | 
|  | 24 |  | 
|  | 25 | #include <stap-probe.h> | 
|  | 26 |  | 
|  | 27 |  | 
|  | 28 | static void | 
|  | 29 | cleanup (void *arg) | 
|  | 30 | { | 
|  | 31 | /* If we already changed the waiter ID, reset it.  The call cannot | 
|  | 32 | fail for any reason but the thread not having done that yet so | 
|  | 33 | there is no reason for a loop.  */ | 
|  | 34 | (void) atomic_compare_and_exchange_bool_acq ((struct pthread **) arg, NULL, | 
|  | 35 | THREAD_SELF); | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 |  | 
|  | 39 | int | 
|  | 40 | pthread_join (pthread_t threadid, void **thread_return) | 
|  | 41 | { | 
|  | 42 | struct pthread *pd = (struct pthread *) threadid; | 
|  | 43 |  | 
|  | 44 | /* Make sure the descriptor is valid.  */ | 
|  | 45 | if (INVALID_NOT_TERMINATED_TD_P (pd)) | 
|  | 46 | /* Not a valid thread handle.  */ | 
|  | 47 | return ESRCH; | 
|  | 48 |  | 
|  | 49 | /* Is the thread joinable?.  */ | 
|  | 50 | if (IS_DETACHED (pd)) | 
|  | 51 | /* We cannot wait for the thread.  */ | 
|  | 52 | return EINVAL; | 
|  | 53 |  | 
|  | 54 | struct pthread *self = THREAD_SELF; | 
|  | 55 | int result = 0; | 
|  | 56 |  | 
|  | 57 | LIBC_PROBE (pthread_join, 1, threadid); | 
|  | 58 |  | 
|  | 59 | /* During the wait we change to asynchronous cancellation.  If we | 
|  | 60 | are canceled the thread we are waiting for must be marked as | 
|  | 61 | un-wait-ed for again.  */ | 
|  | 62 | pthread_cleanup_push (cleanup, &pd->joinid); | 
|  | 63 |  | 
|  | 64 | /* Switch to asynchronous cancellation.  */ | 
|  | 65 | int oldtype = CANCEL_ASYNC (); | 
|  | 66 |  | 
|  | 67 | if ((pd == self | 
|  | 68 | || (self->joinid == pd | 
|  | 69 | && (pd->cancelhandling | 
|  | 70 | & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK | 
|  | 71 | | TERMINATED_BITMASK)) == 0)) | 
|  | 72 | && !CANCEL_ENABLED_AND_CANCELED (self->cancelhandling)) | 
|  | 73 | /* This is a deadlock situation.  The threads are waiting for each | 
|  | 74 | other to finish.  Note that this is a "may" error.  To be 100% | 
|  | 75 | sure we catch this error we would have to lock the data | 
|  | 76 | structures but it is not necessary.  In the unlikely case that | 
|  | 77 | two threads are really caught in this situation they will | 
|  | 78 | deadlock.  It is the programmer's problem to figure this | 
|  | 79 | out.  */ | 
|  | 80 | result = EDEADLK; | 
|  | 81 | /* Wait for the thread to finish.  If it is already locked something | 
|  | 82 | is wrong.  There can only be one waiter.  */ | 
|  | 83 | else if (__builtin_expect (atomic_compare_and_exchange_bool_acq (&pd->joinid, | 
|  | 84 | self, | 
|  | 85 | NULL), 0)) | 
|  | 86 | /* There is already somebody waiting for the thread.  */ | 
|  | 87 | result = EINVAL; | 
|  | 88 | else | 
|  | 89 | /* Wait for the child.  */ | 
|  | 90 | lll_wait_tid (pd->tid); | 
|  | 91 |  | 
|  | 92 |  | 
|  | 93 | /* Restore cancellation mode.  */ | 
|  | 94 | CANCEL_RESET (oldtype); | 
|  | 95 |  | 
|  | 96 | /* Remove the handler.  */ | 
|  | 97 | pthread_cleanup_pop (0); | 
|  | 98 |  | 
|  | 99 |  | 
|  | 100 | if (__glibc_likely (result == 0)) | 
|  | 101 | { | 
|  | 102 | /* We mark the thread as terminated and as joined.  */ | 
|  | 103 | pd->tid = -1; | 
|  | 104 |  | 
|  | 105 | /* Store the return value if the caller is interested.  */ | 
|  | 106 | if (thread_return != NULL) | 
|  | 107 | *thread_return = pd->result; | 
|  | 108 |  | 
|  | 109 |  | 
|  | 110 | /* Free the TCB.  */ | 
|  | 111 | __free_tcb (pd); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | LIBC_PROBE (pthread_join_ret, 3, threadid, result, pd->result); | 
|  | 115 |  | 
|  | 116 | return result; | 
|  | 117 | } |