pthread_once.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * pthread_once.c
  3. *
  4. * Description:
  5. * This translation unit implements miscellaneous thread functions.
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  10. * Copyright(C) 2008 Jason Schmidlapp
  11. *
  12. * Contact Email: [email protected]
  13. *
  14. *
  15. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  16. * Copyright(C) 1998 John E. Bossom
  17. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  18. *
  19. * Contact Email: [email protected]
  20. *
  21. * The original list of contributors to the Pthreads-win32 project
  22. * is contained in the file CONTRIBUTORS.ptw32 included with the
  23. * source code distribution. The list can also be seen at the
  24. * following World Wide Web location:
  25. * http://sources.redhat.com/pthreads-win32/contributors.html
  26. *
  27. * This library is free software; you can redistribute it and/or
  28. * modify it under the terms of the GNU Lesser General Public
  29. * License as published by the Free Software Foundation; either
  30. * version 2 of the License, or (at your option) any later version.
  31. *
  32. * This library is distributed in the hope that it will be useful,
  33. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  35. * Lesser General Public License for more details.
  36. *
  37. * You should have received a copy of the GNU Lesser General Public
  38. * License along with this library in the file COPYING.LIB;
  39. * if not, write to the Free Software Foundation, Inc.,
  40. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  41. */
  42. #include "pte_osal.h"
  43. #include "pthread.h"
  44. #include "implement.h"
  45. #define PTE_ONCE_STARTED 1
  46. #define PTE_ONCE_INIT 0
  47. #define PTE_ONCE_DONE 2
  48. static void
  49. pte_once_init_routine_cleanup(void * arg)
  50. {
  51. pthread_once_t * once_control = (pthread_once_t *) arg;
  52. (void) PTE_ATOMIC_EXCHANGE(&once_control->state,PTE_ONCE_INIT);
  53. if (PTE_ATOMIC_EXCHANGE_ADD((int*)&once_control->semaphore, 0L)) /* MBR fence */
  54. {
  55. pte_osSemaphorePost((pte_osSemaphoreHandle) once_control->semaphore, 1);
  56. }
  57. }
  58. int
  59. pthread_once (pthread_once_t * once_control, void (*init_routine) (void))
  60. /*
  61. * ------------------------------------------------------
  62. * DOCPUBLIC
  63. * If any thread in a process with a once_control parameter
  64. * makes a call to pthread_once(), the first call will summon
  65. * the init_routine(), but subsequent calls will not. The
  66. * once_control parameter determines whether the associated
  67. * initialization routine has been called. The init_routine()
  68. * is complete upon return of pthread_once().
  69. * This function guarantees that one and only one thread
  70. * executes the initialization routine, init_routine when
  71. * access is controlled by the pthread_once_t control
  72. * key.
  73. *
  74. * pthread_once() is not a cancelation point, but the init_routine
  75. * can be. If it's cancelled then the effect on the once_control is
  76. * as if pthread_once had never been entered.
  77. *
  78. *
  79. * PARAMETERS
  80. * once_control
  81. * pointer to an instance of pthread_once_t
  82. *
  83. * init_routine
  84. * pointer to an initialization routine
  85. *
  86. *
  87. * DESCRIPTION
  88. * See above.
  89. *
  90. * RESULTS
  91. * 0 success,
  92. * EINVAL once_control or init_routine is NULL
  93. *
  94. * ------------------------------------------------------
  95. */
  96. {
  97. int result;
  98. int state;
  99. pte_osSemaphoreHandle sema;
  100. if (once_control == NULL || init_routine == NULL)
  101. {
  102. result = EINVAL;
  103. goto FAIL0;
  104. }
  105. else
  106. {
  107. result = 0;
  108. }
  109. while ((state =
  110. PTE_ATOMIC_COMPARE_EXCHANGE(&once_control->state,
  111. PTE_ONCE_STARTED,
  112. PTE_ONCE_INIT))
  113. != PTE_ONCE_DONE)
  114. {
  115. if (PTE_ONCE_INIT == state)
  116. {
  117. pthread_cleanup_push(pte_once_init_routine_cleanup, (void *) once_control);
  118. (*init_routine)();
  119. pthread_cleanup_pop(0);
  120. (void) PTE_ATOMIC_EXCHANGE(&once_control->state,PTE_ONCE_DONE);
  121. /*
  122. * we didn't create the semaphore.
  123. * it is only there if there is someone waiting.
  124. */
  125. if (PTE_ATOMIC_EXCHANGE_ADD((int*)&once_control->semaphore, 0L)) /* MBR fence */
  126. {
  127. pte_osSemaphorePost((pte_osSemaphoreHandle) once_control->semaphore,once_control->numSemaphoreUsers);
  128. }
  129. }
  130. else
  131. {
  132. PTE_ATOMIC_INCREMENT(&once_control->numSemaphoreUsers);
  133. if (!PTE_ATOMIC_EXCHANGE_ADD((int*)&once_control->semaphore, 0L)) /* MBR fence */
  134. {
  135. pte_osSemaphoreCreate(0, (pte_osSemaphoreHandle*) &sema);
  136. if (PTE_ATOMIC_COMPARE_EXCHANGE((int *) &once_control->semaphore,
  137. (int) sema,
  138. 0))
  139. {
  140. pte_osSemaphoreDelete((pte_osSemaphoreHandle)sema);
  141. }
  142. }
  143. /*
  144. * Check 'state' again in case the initting thread has finished or
  145. * cancelled and left before seeing that there was a semaphore.
  146. */
  147. if (PTE_ATOMIC_EXCHANGE_ADD(&once_control->state, 0L) == PTE_ONCE_STARTED)
  148. {
  149. pte_osSemaphorePend((pte_osSemaphoreHandle) once_control->semaphore,NULL);
  150. }
  151. if (0 == PTE_ATOMIC_DECREMENT(&once_control->numSemaphoreUsers))
  152. {
  153. /* we were last */
  154. if ((sema =
  155. (pte_osSemaphoreHandle) PTE_ATOMIC_EXCHANGE((int *) &once_control->semaphore,0)))
  156. {
  157. pte_osSemaphoreDelete(sema);
  158. }
  159. }
  160. }
  161. }
  162. /*
  163. * ------------
  164. * Failure Code
  165. * ------------
  166. */
  167. FAIL0:
  168. return (result);
  169. } /* pthread_once */