cleanup.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * cleanup.c
  3. *
  4. * Description:
  5. * This translation unit implements routines associated
  6. * with cleaning up threads.
  7. *
  8. *
  9. * --------------------------------------------------------------------------
  10. *
  11. * Pthreads-win32 - POSIX Threads Library for Win32
  12. * Copyright(C) 1998 John E. Bossom
  13. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  14. *
  15. * Contact Email: [email protected]
  16. *
  17. * The current list of contributors is contained
  18. * in the file CONTRIBUTORS included with the source
  19. * code distribution. The list can also be seen at the
  20. * following World Wide Web location:
  21. * http://sources.redhat.com/pthreads-win32/contributors.html
  22. *
  23. * This library is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU Lesser General Public
  25. * License as published by the Free Software Foundation; either
  26. * version 2 of the License, or (at your option) any later version.
  27. *
  28. * This library is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  31. * Lesser General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU Lesser General Public
  34. * License along with this library in the file COPYING.LIB;
  35. * if not, write to the Free Software Foundation, Inc.,
  36. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  37. */
  38. #include "pthread.h"
  39. #include "implement.h"
  40. /*
  41. * The functions ptw32_pop_cleanup and ptw32_push_cleanup
  42. * are implemented here for applications written in C with no
  43. * SEH or C++ destructor support.
  44. */
  45. ptw32_cleanup_t *
  46. ptw32_pop_cleanup (int execute)
  47. /*
  48. * ------------------------------------------------------
  49. * DOCPUBLIC
  50. * This function pops the most recently pushed cleanup
  51. * handler. If execute is nonzero, then the cleanup handler
  52. * is executed if non-null.
  53. *
  54. * PARAMETERS
  55. * execute
  56. * if nonzero, execute the cleanup handler
  57. *
  58. *
  59. * DESCRIPTION
  60. * This function pops the most recently pushed cleanup
  61. * handler. If execute is nonzero, then the cleanup handler
  62. * is executed if non-null.
  63. * NOTE: specify 'execute' as nonzero to avoid duplication
  64. * of common cleanup code.
  65. *
  66. * RESULTS
  67. * N/A
  68. *
  69. * ------------------------------------------------------
  70. */
  71. {
  72. ptw32_cleanup_t *cleanup;
  73. cleanup = (ptw32_cleanup_t *) pthread_getspecific (ptw32_cleanupKey);
  74. if (cleanup != NULL)
  75. {
  76. if (execute && (cleanup->routine != NULL))
  77. {
  78. (*cleanup->routine) (cleanup->arg);
  79. }
  80. pthread_setspecific (ptw32_cleanupKey, (void *) cleanup->prev);
  81. }
  82. return (cleanup);
  83. } /* ptw32_pop_cleanup */
  84. void
  85. ptw32_push_cleanup (ptw32_cleanup_t * cleanup,
  86. ptw32_cleanup_callback_t routine, void *arg)
  87. /*
  88. * ------------------------------------------------------
  89. * DOCPUBLIC
  90. * This function pushes a new cleanup handler onto the thread's stack
  91. * of cleanup handlers. Each cleanup handler pushed onto the stack is
  92. * popped and invoked with the argument 'arg' when
  93. * a) the thread exits by calling 'pthread_exit',
  94. * b) when the thread acts on a cancellation request,
  95. * c) or when the thread calls pthread_cleanup_pop with a nonzero
  96. * 'execute' argument
  97. *
  98. * PARAMETERS
  99. * cleanup
  100. * a pointer to an instance of pthread_cleanup_t,
  101. *
  102. * routine
  103. * pointer to a cleanup handler,
  104. *
  105. * arg
  106. * parameter to be passed to the cleanup handler
  107. *
  108. *
  109. * DESCRIPTION
  110. * This function pushes a new cleanup handler onto the thread's stack
  111. * of cleanup handlers. Each cleanup handler pushed onto the stack is
  112. * popped and invoked with the argument 'arg' when
  113. * a) the thread exits by calling 'pthread_exit',
  114. * b) when the thread acts on a cancellation request,
  115. * c) or when the thrad calls pthread_cleanup_pop with a nonzero
  116. * 'execute' argument
  117. * NOTE: pthread_push_cleanup, ptw32_pop_cleanup must be paired
  118. * in the same lexical scope.
  119. *
  120. * RESULTS
  121. * pthread_cleanup_t *
  122. * pointer to the previous cleanup
  123. *
  124. * ------------------------------------------------------
  125. */
  126. {
  127. cleanup->routine = routine;
  128. cleanup->arg = arg;
  129. cleanup->prev = (ptw32_cleanup_t *) pthread_getspecific (ptw32_cleanupKey);
  130. pthread_setspecific (ptw32_cleanupKey, (void *) cleanup);
  131. } /* ptw32_push_cleanup */