pthread_cancel.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright (C) 2002, 2003, 2004, 2009 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 "pthreadP.h"
  18. #include "atomic.h"
  19. #include <sysdep.h>
  20. #include <unistd.h>
  21. #include <bits/kernel-features.h>
  22. int
  23. pthread_cancel (
  24. pthread_t th)
  25. {
  26. volatile struct pthread *pd = (volatile struct pthread *) th;
  27. /* Make sure the descriptor is valid. */
  28. if (INVALID_TD_P (pd))
  29. /* Not a valid thread handle. */
  30. return ESRCH;
  31. #ifdef SHARED
  32. pthread_cancel_init ();
  33. #endif
  34. int result = 0;
  35. int oldval;
  36. int newval;
  37. do
  38. {
  39. again:
  40. oldval = pd->cancelhandling;
  41. newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK;
  42. /* Avoid doing unnecessary work. The atomic operation can
  43. potentially be expensive if the bug has to be locked and
  44. remote cache lines have to be invalidated. */
  45. if (oldval == newval)
  46. break;
  47. /* If the cancellation is handled asynchronously just send a
  48. signal. We avoid this if possible since it's more
  49. expensive. */
  50. if (CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS (newval))
  51. {
  52. /* Cancelling ourselves needs no signal: deliver the cancellation
  53. directly instead of bouncing through tgkill+SIGCANCEL. This
  54. avoids the syscall, the kernel-built signal frame and the extra
  55. (signal-frame) unwind step. */
  56. if (pd == (volatile struct pthread *) THREAD_SELF)
  57. {
  58. if (atomic_compare_and_exchange_bool_acq (&pd->cancelhandling,
  59. newval, oldval))
  60. goto again;
  61. THREAD_SETMEM (THREAD_SELF, result, PTHREAD_CANCELED);
  62. __do_cancel ();
  63. /* NOTREACHED */
  64. }
  65. /* Mark the cancellation as "in progress". */
  66. if (atomic_compare_and_exchange_bool_acq (&pd->cancelhandling,
  67. oldval | CANCELING_BITMASK,
  68. oldval))
  69. goto again;
  70. /* The cancellation handler will take care of marking the
  71. thread as canceled. */
  72. INTERNAL_SYSCALL_DECL (err);
  73. /* One comment: The PID field in the TCB can temporarily be
  74. changed (in fork). But this must not affect this code
  75. here. Since this function would have to be called while
  76. the thread is executing fork, it would have to happen in
  77. a signal handler. But this is no allowed, pthread_cancel
  78. is not guaranteed to be async-safe. */
  79. pid_t pid = getpid ();
  80. int val;
  81. val = INTERNAL_SYSCALL (tgkill, err, 3, pid, pd->tid, SIGCANCEL);
  82. if (INTERNAL_SYSCALL_ERROR_P (val, err))
  83. result = INTERNAL_SYSCALL_ERRNO (val, err);
  84. break;
  85. }
  86. }
  87. /* Mark the thread as canceled. This has to be done
  88. atomically since other bits could be modified as well. */
  89. while (atomic_compare_and_exchange_bool_acq (&pd->cancelhandling, newval,
  90. oldval));
  91. return result;
  92. }
  93. PTHREAD_STATIC_FN_REQUIRE (pthread_create)