thread-cancel.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Thread cancellation support.
  2. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. <https://www.gnu.org/licenses/>. */
  15. #include <hurd/signal.h>
  16. #include <hurd/interrupt.h>
  17. #include <assert.h>
  18. #include <thread_state.h>
  19. error_t
  20. hurd_thread_cancel (thread_t thread)
  21. {
  22. struct hurd_sigstate *ss = _hurd_thread_sigstate (thread);
  23. struct machine_thread_all_state state;
  24. int state_change;
  25. error_t err;
  26. if (! ss)
  27. return EINVAL;
  28. if (ss == _hurd_self_sigstate ())
  29. {
  30. /* We are cancelling ourselves, so it is easy to succeed
  31. quickly. Since this function is not a cancellation point, we
  32. just leave the flag set pending the next cancellation point
  33. (hurd_check_cancel or RPC) and return success. */
  34. ss->cancel = 1;
  35. return 0;
  36. }
  37. assert (! __spin_lock_locked (&ss->critical_section_lock));
  38. __spin_lock (&ss->critical_section_lock);
  39. __spin_lock (&ss->lock);
  40. err = __thread_suspend (thread);
  41. __spin_unlock (&ss->lock);
  42. if (! err)
  43. {
  44. /* Set the flag telling the thread its operation is being cancelled. */
  45. ss->cancel = 1;
  46. /* Interrupt any interruptible RPC now in progress. */
  47. state.set = 0;
  48. _hurdsig_abort_rpcs (ss, 0, 0, &state, &state_change, NULL);
  49. if (state_change)
  50. err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
  51. (natural_t *) &state.basic,
  52. MACHINE_THREAD_STATE_COUNT);
  53. if (ss->cancel_hook)
  54. /* The code being cancelled has a special wakeup function.
  55. Calling this should make the thread wake up and check the
  56. cancellation flag. */
  57. (*ss->cancel_hook) ();
  58. __thread_resume (thread);
  59. }
  60. _hurd_critical_section_unlock (ss);
  61. return err;
  62. }
  63. int
  64. hurd_check_cancel (void)
  65. {
  66. struct hurd_sigstate *ss = _hurd_self_sigstate ();
  67. int cancel;
  68. __spin_lock (&ss->lock);
  69. assert (! __spin_lock_locked (&ss->critical_section_lock));
  70. cancel = ss->cancel;
  71. ss->cancel = 0;
  72. __spin_unlock (&ss->lock);
  73. return cancel;
  74. }