cancellation.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (C) 2002, 2003, 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 <setjmp.h>
  16. #include <stdlib.h>
  17. #include "pthreadP.h"
  18. /* The next two functions are similar to pthread_setcanceltype() but
  19. more specialized for the use in the cancelable functions like write().
  20. They do not need to check parameters etc. */
  21. int
  22. attribute_hidden
  23. __pthread_enable_asynccancel (void)
  24. {
  25. struct pthread *self = THREAD_SELF;
  26. int oldval = THREAD_GETMEM (self, cancelhandling);
  27. while (1)
  28. {
  29. int newval = oldval | CANCELTYPE_BITMASK;
  30. if (newval == oldval)
  31. break;
  32. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
  33. oldval);
  34. if (__builtin_expect (curval == oldval, 1))
  35. {
  36. if (CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS (newval))
  37. {
  38. THREAD_SETMEM (self, result, PTHREAD_CANCELED);
  39. __do_cancel ();
  40. }
  41. break;
  42. }
  43. /* Prepare the next round. */
  44. oldval = curval;
  45. }
  46. return oldval;
  47. }
  48. void
  49. internal_function attribute_hidden
  50. __pthread_disable_asynccancel (int oldtype)
  51. {
  52. /* If asynchronous cancellation was enabled before we do not have
  53. anything to do. */
  54. if (oldtype & CANCELTYPE_BITMASK)
  55. return;
  56. struct pthread *self = THREAD_SELF;
  57. int newval;
  58. int oldval = THREAD_GETMEM (self, cancelhandling);
  59. while (1)
  60. {
  61. newval = oldval & ~CANCELTYPE_BITMASK;
  62. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
  63. oldval);
  64. if (__builtin_expect (curval == oldval, 1))
  65. break;
  66. /* Prepare the next round. */
  67. oldval = curval;
  68. }
  69. /* We cannot return when we are being canceled. Upon return the
  70. thread might be things which would have to be undone. The
  71. following loop should loop until the cancellation signal is
  72. delivered. */
  73. while (__builtin_expect ((newval & (CANCELING_BITMASK | CANCELED_BITMASK))
  74. == CANCELING_BITMASK, 0))
  75. {
  76. lll_futex_wait (&self->cancelhandling, newval, LLL_PRIVATE);
  77. newval = THREAD_GETMEM (self, cancelhandling);
  78. }
  79. }