Use a #pragma to suppress a bogus GCC 10 warning instead of an assert [BZ 27832].
[glibc.git] / nptl / pthread_cancel.c
blobfd04bedf6cf3d99d362125b4ec8d5a289de967f6
1 /* Copyright (C) 2002-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <signal.h>
21 #include <stdlib.h>
22 #include "pthreadP.h"
23 #include <atomic.h>
24 #include <sysdep.h>
25 #include <unistd.h>
26 #include <unwind-link.h>
27 #include <stdio.h>
28 #include <gnu/lib-names.h>
30 int
31 __pthread_cancel (pthread_t th)
33 volatile struct pthread *pd = (volatile struct pthread *) th;
35 /* Make sure the descriptor is valid. */
36 if (INVALID_TD_P (pd))
37 /* Not a valid thread handle. */
38 return ESRCH;
40 #ifdef SHARED
41 /* Trigger an error if libgcc_s cannot be loaded. */
43 struct unwind_link *unwind_link = __libc_unwind_link_get ();
44 if (unwind_link == NULL)
45 __libc_fatal (LIBGCC_S_SO
46 " must be installed for pthread_cancel to work\n");
48 #endif
49 int result = 0;
50 int oldval;
51 int newval;
54 again:
55 oldval = pd->cancelhandling;
56 newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
58 /* Avoid doing unnecessary work. The atomic operation can
59 potentially be expensive if the bug has to be locked and
60 remote cache lines have to be invalidated. */
61 if (oldval == newval)
62 break;
64 /* If the cancellation is handled asynchronously just send a
65 signal. We avoid this if possible since it's more
66 expensive. */
67 if (CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS (newval))
69 /* Mark the cancellation as "in progress". */
70 if (atomic_compare_and_exchange_bool_acq (&pd->cancelhandling,
71 oldval | CANCELING_BITMASK,
72 oldval))
73 goto again;
75 /* The cancellation handler will take care of marking the
76 thread as canceled. */
77 pid_t pid = __getpid ();
79 int val = INTERNAL_SYSCALL_CALL (tgkill, pid, pd->tid,
80 SIGCANCEL);
81 if (INTERNAL_SYSCALL_ERROR_P (val))
82 result = INTERNAL_SYSCALL_ERRNO (val);
84 break;
87 /* A single-threaded process should be able to kill itself, since
88 there is nothing in the POSIX specification that says that it
89 cannot. So we set multiple_threads to true so that cancellation
90 points get executed. */
91 THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
92 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
93 __libc_multiple_threads = 1;
94 #endif
96 /* Mark the thread as canceled. This has to be done
97 atomically since other bits could be modified as well. */
98 while (atomic_compare_and_exchange_bool_acq (&pd->cancelhandling, newval,
99 oldval));
101 return result;
103 weak_alias (__pthread_cancel, pthread_cancel)
105 PTHREAD_STATIC_FN_REQUIRE (__pthread_create)