libc-cancellation.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Copyright (C) 2002, 2003 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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <setjmp.h>
  17. #include <stdlib.h>
  18. #include "pthreadP.h"
  19. #include "atomic.h"
  20. #include <bits/libc-lock.h>
  21. #ifndef NOT_IN_libc
  22. /* The next two functions are similar to pthread_setcanceltype() but
  23. more specialized for the use in the cancelable functions like write().
  24. They do not need to check parameters etc. */
  25. int
  26. attribute_hidden
  27. __libc_enable_asynccancel (void)
  28. {
  29. struct pthread *self = THREAD_SELF;
  30. int oldval = THREAD_GETMEM (self, cancelhandling);
  31. while (1)
  32. {
  33. int newval = oldval | CANCELTYPE_BITMASK;
  34. if (__builtin_expect ((oldval & CANCELED_BITMASK) != 0, 0))
  35. {
  36. /* If we are already exiting or if PTHREAD_CANCEL_DISABLED,
  37. stop right here. */
  38. if ((oldval & (EXITING_BITMASK | CANCELSTATE_BITMASK)) != 0)
  39. break;
  40. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
  41. newval, oldval);
  42. if (__builtin_expect (curval != oldval, 0))
  43. {
  44. /* Somebody else modified the word, try again. */
  45. oldval = curval;
  46. continue;
  47. }
  48. THREAD_SETMEM (self, result, PTHREAD_CANCELED);
  49. __do_cancel ();
  50. /* NOTREACHED */
  51. }
  52. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
  53. oldval);
  54. if (__builtin_expect (curval == oldval, 1))
  55. break;
  56. /* Prepare the next round. */
  57. oldval = curval;
  58. }
  59. return oldval;
  60. }
  61. void
  62. internal_function attribute_hidden
  63. __libc_disable_asynccancel (int oldtype)
  64. {
  65. /* If asynchronous cancellation was enabled before we do not have
  66. anything to do. */
  67. if (oldtype & CANCELTYPE_BITMASK)
  68. return;
  69. struct pthread *self = THREAD_SELF;
  70. int oldval = THREAD_GETMEM (self, cancelhandling);
  71. while (1)
  72. {
  73. int newval = oldval & ~CANCELTYPE_BITMASK;
  74. if (newval == oldval)
  75. break;
  76. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval,
  77. oldval);
  78. if (__builtin_expect (curval == oldval, 1))
  79. break;
  80. /* Prepare the next round. */
  81. oldval = curval;
  82. }
  83. }
  84. void
  85. __libc_cleanup_routine (struct __pthread_cleanup_frame *f)
  86. {
  87. if (f->__do_it)
  88. f->__cancel_routine (f->__cancel_arg);
  89. }
  90. #endif