pt-cancel.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Cancel a thread.
  2. Copyright (C) 2002-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 <pthread.h>
  16. #include <pt-internal.h>
  17. #include <shlib-compat.h>
  18. #include <ldsodefs.h>
  19. int
  20. __pthread_cancel (pthread_t t)
  21. {
  22. int err = 0;
  23. struct __pthread *p;
  24. p = __pthread_getid (t);
  25. if (p == NULL)
  26. return ESRCH;
  27. __pthread_mutex_lock (&p->cancel_lock);
  28. if (p->cancel_pending)
  29. {
  30. __pthread_mutex_unlock (&p->cancel_lock);
  31. return 0;
  32. }
  33. p->cancel_pending = 1;
  34. if (p->cancel_state != PTHREAD_CANCEL_ENABLE)
  35. {
  36. __pthread_mutex_unlock (&p->cancel_lock);
  37. return 0;
  38. }
  39. if (p->cancel_type == PTHREAD_CANCEL_ASYNCHRONOUS)
  40. /* CANCEL_LOCK is unlocked by this call. */
  41. err = __pthread_do_cancel (p);
  42. else
  43. {
  44. if (p->cancel_hook != NULL)
  45. /* Thread blocking on a cancellation point. Invoke hook to unblock.
  46. See __pthread_cond_timedwait_internal. */
  47. p->cancel_hook (p->cancel_hook_arg);
  48. __pthread_mutex_unlock (&p->cancel_lock);
  49. }
  50. return err;
  51. }
  52. versioned_symbol (libc, __pthread_cancel, pthread_cancel, GLIBC_2_43);
  53. #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_12, GLIBC_2_43)
  54. compat_symbol (libpthread, __pthread_cancel, pthread_cancel, GLIBC_2_12);
  55. #endif