pthread_once.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #include "pthreadP.h"
  16. #include <lowlevellock.h>
  17. unsigned long int __fork_generation attribute_hidden;
  18. static void
  19. clear_once_control (void *arg)
  20. {
  21. pthread_once_t *once_control = (pthread_once_t *) arg;
  22. *once_control = 0;
  23. lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
  24. }
  25. int
  26. attribute_protected
  27. __pthread_once (pthread_once_t *once_control, void (*init_routine) (void))
  28. {
  29. for (;;)
  30. {
  31. int oldval;
  32. int newval;
  33. /* Pseudo code:
  34. newval = __fork_generation | 1;
  35. oldval = *once_control;
  36. if ((oldval & 2) == 0)
  37. *once_control = newval;
  38. Do this atomically.
  39. */
  40. do
  41. {
  42. newval = __fork_generation | 1;
  43. oldval = *once_control;
  44. if (oldval & 2)
  45. break;
  46. } while (atomic_compare_and_exchange_val_acq (once_control, newval, oldval) != oldval);
  47. /* Check if the initializer has already been done. */
  48. if ((oldval & 2) != 0)
  49. return 0;
  50. /* Check if another thread already runs the initializer. */
  51. if ((oldval & 1) == 0)
  52. break;
  53. /* Check whether the initializer execution was interrupted by a fork. */
  54. if (oldval != newval)
  55. break;
  56. /* Same generation, some other thread was faster. Wait. */
  57. lll_futex_wait (once_control, oldval, LLL_PRIVATE);
  58. }
  59. /* This thread is the first here. Do the initialization.
  60. Register a cleanup handler so that in case the thread gets
  61. interrupted the initialization can be restarted. */
  62. pthread_cleanup_push (clear_once_control, once_control);
  63. init_routine ();
  64. pthread_cleanup_pop (0);
  65. /* Say that the initialisation is done. */
  66. *once_control = __fork_generation | 2;
  67. /* Wake up all other threads. */
  68. lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
  69. return 0;
  70. }
  71. weak_alias (__pthread_once, pthread_once)
  72. strong_alias (__pthread_once, __pthread_once_internal)
  73. #if defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__PIC__)
  74. /* When statically linked, if pthread_create is used, this file
  75. will be brought in. The exception handling code in GCC assumes
  76. that if pthread_create is available, so are these. */
  77. const void *include_pthread_getspecific attribute_hidden = pthread_getspecific;
  78. const void *include_pthread_setspecific attribute_hidden = pthread_setspecific;
  79. const void *include_pthread_key_create attribute_hidden = pthread_key_create;
  80. #endif