PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/glibc-ports-2.16-a20c2b3c/sysdeps/unix/sysv/linux/mips/nptl/pthread_once.c

#
C | 93 lines | 48 code | 18 blank | 27 comment | 9 complexity | 06089b40d2dcf2ebc4a44a1967893822 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. /* Copyright (C) 2003-2012 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library. If not, see
  14. <http://www.gnu.org/licenses/>. */
  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. __pthread_once (once_control, init_routine)
  27. pthread_once_t *once_control;
  28. void (*init_routine) (void);
  29. {
  30. while (1)
  31. {
  32. int oldval, val, newval;
  33. val = *once_control;
  34. do
  35. {
  36. /* Check if the initialized has already been done. */
  37. if ((val & 2) != 0)
  38. return 0;
  39. oldval = val;
  40. newval = (oldval & 3) | __fork_generation | 1;
  41. val = atomic_compare_and_exchange_val_acq (once_control, newval,
  42. oldval);
  43. }
  44. while (__builtin_expect (val != oldval, 0));
  45. /* Check if another thread already runs the initializer. */
  46. if ((oldval & 1) != 0)
  47. {
  48. /* Check whether the initializer execution was interrupted
  49. by a fork. */
  50. if (((oldval ^ newval) & -4) == 0)
  51. {
  52. /* Same generation, some other thread was faster. Wait. */
  53. lll_futex_wait (once_control, newval, LLL_PRIVATE);
  54. continue;
  55. }
  56. }
  57. /* This thread is the first here. Do the initialization.
  58. Register a cleanup handler so that in case the thread gets
  59. interrupted the initialization can be restarted. */
  60. pthread_cleanup_push (clear_once_control, once_control);
  61. init_routine ();
  62. pthread_cleanup_pop (0);
  63. /* Add one to *once_control. */
  64. atomic_increment (once_control);
  65. /* Wake up all other threads. */
  66. lll_futex_wake (once_control, INT_MAX, LLL_PRIVATE);
  67. break;
  68. }
  69. return 0;
  70. }
  71. weak_alias (__pthread_once, pthread_once)
  72. hidden_def (__pthread_once)