pthread_cancel.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (C) 2002-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <signal.h>
  17. #include <stdlib.h>
  18. #include "pthreadP.h"
  19. #include <atomic.h>
  20. #include <sysdep.h>
  21. #include <unistd.h>
  22. int
  23. __pthread_cancel (pthread_t th)
  24. {
  25. volatile struct pthread *pd = (volatile struct pthread *) th;
  26. /* Make sure the descriptor is valid. */
  27. if (INVALID_TD_P (pd))
  28. /* Not a valid thread handle. */
  29. return ESRCH;
  30. #ifdef SHARED
  31. pthread_cancel_init ();
  32. #endif
  33. int result = 0;
  34. int oldval;
  35. int newval;
  36. do
  37. {
  38. again:
  39. oldval = pd->cancelhandling;
  40. newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
  41. /* Avoid doing unnecessary work. The atomic operation can
  42. potentially be expensive if the bug has to be locked and
  43. remote cache lines have to be invalidated. */
  44. if (oldval == newval)
  45. break;
  46. /* If the cancellation is handled asynchronously just send a
  47. signal. We avoid this if possible since it's more
  48. expensive. */
  49. if (CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS (newval))
  50. {
  51. /* Mark the cancellation as "in progress". */
  52. if (atomic_compare_and_exchange_bool_acq (&pd->cancelhandling,
  53. oldval | CANCELING_BITMASK,
  54. oldval))
  55. goto again;
  56. #ifdef SIGCANCEL
  57. /* The cancellation handler will take care of marking the
  58. thread as canceled. */
  59. pid_t pid = __getpid ();
  60. INTERNAL_SYSCALL_DECL (err);
  61. int val = INTERNAL_SYSCALL_CALL (tgkill, err, pid, pd->tid,
  62. SIGCANCEL);
  63. if (INTERNAL_SYSCALL_ERROR_P (val, err))
  64. result = INTERNAL_SYSCALL_ERRNO (val, err);
  65. #else
  66. /* It should be impossible to get here at all, since
  67. pthread_setcanceltype should never have allowed
  68. PTHREAD_CANCEL_ASYNCHRONOUS to be set. */
  69. abort ();
  70. #endif
  71. break;
  72. }
  73. /* A single-threaded process should be able to kill itself, since
  74. there is nothing in the POSIX specification that says that it
  75. cannot. So we set multiple_threads to true so that cancellation
  76. points get executed. */
  77. THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
  78. #ifndef TLS_MULTIPLE_THREADS_IN_TCB
  79. __pthread_multiple_threads = *__libc_multiple_threads_ptr = 1;
  80. #endif
  81. }
  82. /* Mark the thread as canceled. This has to be done
  83. atomically since other bits could be modified as well. */
  84. while (atomic_compare_and_exchange_bool_acq (&pd->cancelhandling, newval,
  85. oldval));
  86. return result;
  87. }
  88. weak_alias (__pthread_cancel, pthread_cancel)
  89. PTHREAD_STATIC_FN_REQUIRE (__pthread_create)