pt-setcancelstate.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Set the cancel state for the calling 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 <shlib-compat.h>
  17. #include <pt-internal.h>
  18. int
  19. __pthread_setcancelstate (int state, int *oldstate)
  20. {
  21. struct __pthread *p = _pthread_self ();
  22. int cancelled;
  23. switch (state)
  24. {
  25. default:
  26. return EINVAL;
  27. case PTHREAD_CANCEL_ENABLE:
  28. case PTHREAD_CANCEL_DISABLE:
  29. break;
  30. }
  31. __pthread_mutex_lock (&p->cancel_lock);
  32. if (oldstate != NULL)
  33. *oldstate = p->cancel_state;
  34. p->cancel_state = state;
  35. cancelled = (p->cancel_state == PTHREAD_CANCEL_ENABLE) && p->cancel_pending == 1 && (p->cancel_type == PTHREAD_CANCEL_ASYNCHRONOUS);
  36. if (cancelled)
  37. /* Do not achieve cancel when called again, notably from __pthread_exit itself. */
  38. p->cancel_pending = 2;
  39. __pthread_mutex_unlock (&p->cancel_lock);
  40. if (cancelled)
  41. __pthread_exit (PTHREAD_CANCELED);
  42. return 0;
  43. }
  44. libc_hidden_def (__pthread_setcancelstate)
  45. versioned_symbol (libc, __pthread_setcancelstate, pthread_setcancelstate, GLIBC_2_21);
  46. #if OTHER_SHLIB_COMPAT (libpthread, GLIBC_2_12, GLIBC_2_21)
  47. compat_symbol (libc, __pthread_setcancelstate, pthread_setcancelstate, GLIBC_2_12);
  48. #endif