pte_callUserDestroyRoutines.c 8.0 KB

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