pthread_once.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (C) 2003, 2004, 2007 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. int tmp;
  34. /* Pseudo code:
  35. newval = __fork_generation | 1;
  36. oldval = *once_control;
  37. if ((oldval & 2) == 0)
  38. *once_control = newval;
  39. Do this atomically.
  40. */
  41. newval = __fork_generation | 1;
  42. __asm__ __volatile__ (
  43. "1: ldl_l %0, %2\n"
  44. " and %0, 2, %1\n"
  45. " bne %1, 2f\n"
  46. " mov %3, %1\n"
  47. " stl_c %1, %2\n"
  48. " beq %1, 1b\n"
  49. "2: mb"
  50. : "=&r" (oldval), "=&r" (tmp), "=m" (*once_control)
  51. : "r" (newval), "m" (*once_control));
  52. /* Check if the initializer has already been done. */
  53. if ((oldval & 2) != 0)
  54. return 0;
  55. /* Check if another thread already runs the initializer. */
  56. if ((oldval & 1) == 0)
  57. break;
  58. /* Check whether the initializer execution was interrupted by a fork. */
  59. if (oldval != newval)
  60. break;
  61. /* Same generation, some other thread was faster. Wait. */
  62. lll_futex_wait (once_control, oldval, LLL_PRIVATE);
  63. }
  64. /* This thread is the first here. Do the initialization.
  65. Register a cleanup handler so that in case the thread gets
  66. interrupted the initialization can be restarted. */
  67. pthread_cleanup_push (clear_once_control, once_control);
  68. init_routine ();
  69. pthread_cleanup_pop (0);
  70. /* Add one to *once_control to take the bottom 2 bits from 01 to 10. */
  71. atomic_increment (once_control);
  72. /* Wake up all other threads. */
  73. lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
  74. return 0;
  75. }
  76. weak_alias (__pthread_once, pthread_once)
  77. strong_alias (__pthread_once, __pthread_once_internal)