cleanup.c 4.8 KB

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