x86: Support IBT and SHSTK in Intel CET [BZ #21598]
[glibc.git] / nptl / pthread_join_common.c
blob702fcd154564d0cd94b64a8668b6f90a9b6e5094
1 /* Common definition for pthread_{timed,try}join{_np}.
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #include "pthreadP.h"
20 #include <atomic.h>
21 #include <stap-probe.h>
23 static void
24 cleanup (void *arg)
26 /* If we already changed the waiter ID, reset it. The call cannot
27 fail for any reason but the thread not having done that yet so
28 there is no reason for a loop. */
29 struct pthread *self = THREAD_SELF;
30 atomic_compare_exchange_weak_acquire (&arg, &self, NULL);
33 int
34 __pthread_timedjoin_ex (pthread_t threadid, void **thread_return,
35 const struct timespec *abstime, bool block)
37 struct pthread *pd = (struct pthread *) threadid;
39 /* Make sure the descriptor is valid. */
40 if (INVALID_NOT_TERMINATED_TD_P (pd))
41 /* Not a valid thread handle. */
42 return ESRCH;
44 /* Is the thread joinable?. */
45 if (IS_DETACHED (pd))
46 /* We cannot wait for the thread. */
47 return EINVAL;
49 struct pthread *self = THREAD_SELF;
50 int result = 0;
52 LIBC_PROBE (pthread_join, 1, threadid);
54 if ((pd == self
55 || (self->joinid == pd
56 && (pd->cancelhandling
57 & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
58 | TERMINATED_BITMASK)) == 0))
59 && !CANCEL_ENABLED_AND_CANCELED (self->cancelhandling))
60 /* This is a deadlock situation. The threads are waiting for each
61 other to finish. Note that this is a "may" error. To be 100%
62 sure we catch this error we would have to lock the data
63 structures but it is not necessary. In the unlikely case that
64 two threads are really caught in this situation they will
65 deadlock. It is the programmer's problem to figure this
66 out. */
67 return EDEADLK;
69 /* Wait for the thread to finish. If it is already locked something
70 is wrong. There can only be one waiter. */
71 else if (__glibc_unlikely (atomic_compare_exchange_weak_acquire (&pd->joinid,
72 &self,
73 NULL)))
74 /* There is already somebody waiting for the thread. */
75 return EINVAL;
77 if (block)
79 /* During the wait we change to asynchronous cancellation. If we
80 are cancelled the thread we are waiting for must be marked as
81 un-wait-ed for again. */
82 pthread_cleanup_push (cleanup, &pd->joinid);
84 int oldtype = CANCEL_ASYNC ();
86 if (abstime != NULL)
87 result = lll_timedwait_tid (pd->tid, abstime);
88 else
89 lll_wait_tid (pd->tid);
91 CANCEL_RESET (oldtype);
93 pthread_cleanup_pop (0);
96 if (__glibc_likely (result == 0))
98 /* We mark the thread as terminated and as joined. */
99 pd->tid = -1;
101 /* Store the return value if the caller is interested. */
102 if (thread_return != NULL)
103 *thread_return = pd->result;
105 /* Free the TCB. */
106 __free_tcb (pd);
108 else
109 pd->joinid = NULL;
111 LIBC_PROBE (pthread_join_ret, 3, threadid, result, pd->result);
113 return result;
115 hidden_def (__pthread_timedjoin_ex)