ptw32_callUserDestroyRoutines.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * ptw32_callUserDestroyRoutines.c
  3. *
  4. * Description:
  5. * This translation unit implements routines which are private to
  6. * the implementation and may be used throughout it.
  7. *
  8. * --------------------------------------------------------------------------
  9. *
  10. * Pthreads-win32 - POSIX Threads Library for Win32
  11. * Copyright(C) 1998 John E. Bossom
  12. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  13. *
  14. * Contact Email: [email protected]
  15. *
  16. * The current list of contributors is contained
  17. * in the file CONTRIBUTORS included with the source
  18. * code distribution. The list can also be seen at the
  19. * following World Wide Web location:
  20. * http://sources.redhat.com/pthreads-win32/contributors.html
  21. *
  22. * This library is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU Lesser General Public
  24. * License as published by the Free Software Foundation; either
  25. * version 2 of the License, or (at your option) any later version.
  26. *
  27. * This library is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. * Lesser General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Lesser General Public
  33. * License along with this library in the file COPYING.LIB;
  34. * if not, write to the Free Software Foundation, Inc.,
  35. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  36. */
  37. #include "pthread.h"
  38. #include "implement.h"
  39. #if defined(__CLEANUP_CXX)
  40. # if defined(_MSC_VER)
  41. # include <eh.h>
  42. # elif defined(__WATCOMC__)
  43. # include <eh.h>
  44. # include <exceptio.h>
  45. # else
  46. # if defined(__GNUC__) && __GNUC__ < 3
  47. # include <new.h>
  48. # else
  49. # include <new>
  50. using
  51. std::terminate;
  52. # endif
  53. # endif
  54. #endif
  55. void
  56. ptw32_callUserDestroyRoutines (pthread_t thread)
  57. /*
  58. * -------------------------------------------------------------------
  59. * DOCPRIVATE
  60. *
  61. * This the routine runs through all thread keys and calls
  62. * the destroy routines on the user's data for the current thread.
  63. * It simulates the behaviour of POSIX Threads.
  64. *
  65. * PARAMETERS
  66. * thread
  67. * an instance of pthread_t
  68. *
  69. * RETURNS
  70. * N/A
  71. * -------------------------------------------------------------------
  72. */
  73. {
  74. ThreadKeyAssoc * assoc;
  75. if (thread.p != NULL)
  76. {
  77. ptw32_mcs_local_node_t threadLock;
  78. ptw32_mcs_local_node_t keyLock;
  79. int assocsRemaining;
  80. int iterations = 0;
  81. ptw32_thread_t * sp = (ptw32_thread_t *) thread.p;
  82. /*
  83. * Run through all Thread<-->Key associations
  84. * for the current thread.
  85. *
  86. * Do this process at most PTHREAD_DESTRUCTOR_ITERATIONS times.
  87. */
  88. do
  89. {
  90. assocsRemaining = 0;
  91. iterations++;
  92. ptw32_mcs_lock_acquire(&(sp->threadLock), &threadLock);
  93. /*
  94. * The pointer to the next assoc is stored in the thread struct so that
  95. * the assoc destructor in pthread_key_delete can adjust it
  96. * if it deletes this assoc. This can happen if we fail to acquire
  97. * both locks below, and are forced to release all of our locks,
  98. * leaving open the opportunity for pthread_key_delete to get in
  99. * before us.
  100. */
  101. sp->nextAssoc = sp->keys;
  102. ptw32_mcs_lock_release(&threadLock);
  103. for (;;)
  104. {
  105. void * value;
  106. pthread_key_t k;
  107. void (*destructor) (void *);
  108. /*
  109. * First we need to serialise with pthread_key_delete by locking
  110. * both assoc guards, but in the reverse order to our convention,
  111. * so we must be careful to avoid deadlock.
  112. */
  113. ptw32_mcs_lock_acquire(&(sp->threadLock), &threadLock);
  114. if ((assoc = (ThreadKeyAssoc *)sp->nextAssoc) == NULL)
  115. {
  116. /* Finished */
  117. ptw32_mcs_lock_release(&threadLock);
  118. break;
  119. }
  120. else
  121. {
  122. /*
  123. * assoc->key must be valid because assoc can't change or be
  124. * removed from our chain while we hold at least one lock. If
  125. * the assoc was on our key chain then the key has not been
  126. * deleted yet.
  127. *
  128. * Now try to acquire the second lock without deadlocking.
  129. * If we fail, we need to relinquish the first lock and the
  130. * processor and then try to acquire them all again.
  131. */
  132. if (ptw32_mcs_lock_try_acquire(&(assoc->key->keyLock), &keyLock) == EBUSY)
  133. {
  134. ptw32_mcs_lock_release(&threadLock);
  135. Sleep(0);
  136. /*
  137. * Go around again.
  138. * If pthread_key_delete has removed this assoc in the meantime,
  139. * sp->nextAssoc will point to a new assoc.
  140. */
  141. continue;
  142. }
  143. }
  144. /* We now hold both locks */
  145. sp->nextAssoc = assoc->nextKey;
  146. /*
  147. * Key still active; pthread_key_delete
  148. * will block on these same mutexes before
  149. * it can release actual key; therefore,
  150. * key is valid and we can call the destroy
  151. * routine;
  152. */
  153. k = assoc->key;
  154. destructor = k->destructor;
  155. value = TlsGetValue(k->key);
  156. TlsSetValue (k->key, NULL);
  157. // Every assoc->key exists and has a destructor
  158. if (value != NULL && iterations <= PTHREAD_DESTRUCTOR_ITERATIONS)
  159. {
  160. /*
  161. * Unlock both locks before the destructor runs.
  162. * POSIX says pthread_key_delete can be run from destructors,
  163. * and that probably includes with this key as target.
  164. * pthread_setspecific can also be run from destructors and
  165. * also needs to be able to access the assocs.
  166. */
  167. ptw32_mcs_lock_release(&threadLock);
  168. ptw32_mcs_lock_release(&keyLock);
  169. assocsRemaining++;
  170. #if defined(__cplusplus)
  171. try
  172. {
  173. /*
  174. * Run the caller's cleanup routine.
  175. */
  176. destructor (value);
  177. }
  178. catch (...)
  179. {
  180. /*
  181. * A system unexpected exception has occurred
  182. * running the user's destructor.
  183. * We get control back within this block in case
  184. * the application has set up it's own terminate
  185. * handler. Since we are leaving the thread we
  186. * should not get any internal pthreads
  187. * exceptions.
  188. */
  189. terminate ();
  190. }
  191. #else /* __cplusplus */
  192. /*
  193. * Run the caller's cleanup routine.
  194. */
  195. destructor (value);
  196. #endif /* __cplusplus */
  197. }
  198. else
  199. {
  200. /*
  201. * Remove association from both the key and thread chains
  202. * and reclaim it's memory resources.
  203. */
  204. ptw32_tkAssocDestroy (assoc);
  205. ptw32_mcs_lock_release(&threadLock);
  206. ptw32_mcs_lock_release(&keyLock);
  207. }
  208. }
  209. }
  210. while (assocsRemaining);
  211. }
  212. } /* ptw32_callUserDestroyRoutines */