cleanup_defer_compat.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "pthreadP.h"
  17. void
  18. attribute_protected
  19. _pthread_cleanup_push_defer (
  20. struct _pthread_cleanup_buffer *buffer,
  21. void (*routine) (void *),
  22. void *arg)
  23. {
  24. struct pthread *self = THREAD_SELF;
  25. buffer->__routine = routine;
  26. buffer->__arg = arg;
  27. buffer->__prev = THREAD_GETMEM (self, cleanup);
  28. int cancelhandling = THREAD_GETMEM (self, cancelhandling);
  29. /* Disable asynchronous cancellation for now. */
  30. if (__builtin_expect (cancelhandling & CANCELTYPE_BITMASK, 0))
  31. while (1)
  32. {
  33. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
  34. cancelhandling
  35. & ~CANCELTYPE_BITMASK,
  36. cancelhandling);
  37. if (__builtin_expect (curval == cancelhandling, 1))
  38. /* Successfully replaced the value. */
  39. break;
  40. /* Prepare for the next round. */
  41. cancelhandling = curval;
  42. }
  43. buffer->__canceltype = (cancelhandling & CANCELTYPE_BITMASK
  44. ? PTHREAD_CANCEL_ASYNCHRONOUS
  45. : PTHREAD_CANCEL_DEFERRED);
  46. THREAD_SETMEM (self, cleanup, buffer);
  47. }
  48. strong_alias (_pthread_cleanup_push_defer, __pthread_cleanup_push_defer)
  49. void
  50. attribute_protected
  51. _pthread_cleanup_pop_restore (
  52. struct _pthread_cleanup_buffer *buffer,
  53. int execute)
  54. {
  55. struct pthread *self = THREAD_SELF;
  56. THREAD_SETMEM (self, cleanup, buffer->__prev);
  57. int cancelhandling;
  58. if (__builtin_expect (buffer->__canceltype != PTHREAD_CANCEL_DEFERRED, 0)
  59. && ((cancelhandling = THREAD_GETMEM (self, cancelhandling))
  60. & CANCELTYPE_BITMASK) == 0)
  61. {
  62. while (1)
  63. {
  64. int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling,
  65. cancelhandling
  66. | CANCELTYPE_BITMASK,
  67. cancelhandling);
  68. if (__builtin_expect (curval == cancelhandling, 1))
  69. /* Successfully replaced the value. */
  70. break;
  71. /* Prepare for the next round. */
  72. cancelhandling = curval;
  73. }
  74. CANCELLATION_P (self);
  75. }
  76. /* If necessary call the cleanup routine after we removed the
  77. current cleanup block from the list. */
  78. if (execute)
  79. buffer->__routine (buffer->__arg);
  80. }
  81. strong_alias (_pthread_cleanup_pop_restore, __pthread_cleanup_pop_restore)