critical.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Copyright (C) 2005-2015 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Offloading and Multi Processing Library
  4. (libgomp).
  5. Libgomp is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* This file handles the CRITICAL construct. */
  21. #include "libgomp.h"
  22. #include <stdlib.h>
  23. static gomp_mutex_t default_lock;
  24. void
  25. GOMP_critical_start (void)
  26. {
  27. /* There is an implicit flush on entry to a critical region. */
  28. __atomic_thread_fence (MEMMODEL_RELEASE);
  29. gomp_mutex_lock (&default_lock);
  30. }
  31. void
  32. GOMP_critical_end (void)
  33. {
  34. gomp_mutex_unlock (&default_lock);
  35. }
  36. #ifndef HAVE_SYNC_BUILTINS
  37. static gomp_mutex_t create_lock_lock;
  38. #endif
  39. void
  40. GOMP_critical_name_start (void **pptr)
  41. {
  42. gomp_mutex_t *plock;
  43. /* If a mutex fits within the space for a pointer, and is zero initialized,
  44. then use the pointer space directly. */
  45. if (GOMP_MUTEX_INIT_0
  46. && sizeof (gomp_mutex_t) <= sizeof (void *)
  47. && __alignof (gomp_mutex_t) <= sizeof (void *))
  48. plock = (gomp_mutex_t *)pptr;
  49. /* Otherwise we have to be prepared to malloc storage. */
  50. else
  51. {
  52. plock = *pptr;
  53. if (plock == NULL)
  54. {
  55. #ifdef HAVE_SYNC_BUILTINS
  56. gomp_mutex_t *nlock = gomp_malloc (sizeof (gomp_mutex_t));
  57. gomp_mutex_init (nlock);
  58. plock = __sync_val_compare_and_swap (pptr, NULL, nlock);
  59. if (plock != NULL)
  60. {
  61. gomp_mutex_destroy (nlock);
  62. free (nlock);
  63. }
  64. else
  65. plock = nlock;
  66. #else
  67. gomp_mutex_lock (&create_lock_lock);
  68. plock = *pptr;
  69. if (plock == NULL)
  70. {
  71. plock = gomp_malloc (sizeof (gomp_mutex_t));
  72. gomp_mutex_init (plock);
  73. __sync_synchronize ();
  74. *pptr = plock;
  75. }
  76. gomp_mutex_unlock (&create_lock_lock);
  77. #endif
  78. }
  79. }
  80. gomp_mutex_lock (plock);
  81. }
  82. void
  83. GOMP_critical_name_end (void **pptr)
  84. {
  85. gomp_mutex_t *plock;
  86. /* If a mutex fits within the space for a pointer, and is zero initialized,
  87. then use the pointer space directly. */
  88. if (GOMP_MUTEX_INIT_0
  89. && sizeof (gomp_mutex_t) <= sizeof (void *)
  90. && __alignof (gomp_mutex_t) <= sizeof (void *))
  91. plock = (gomp_mutex_t *)pptr;
  92. else
  93. plock = *pptr;
  94. gomp_mutex_unlock (plock);
  95. }
  96. /* This mutex is used when atomic operations don't exist for the target
  97. in the mode requested. The result is not globally atomic, but works so
  98. long as all parallel references are within #pragma omp atomic directives.
  99. According to responses received from omp@openmp.org, appears to be within
  100. spec. Which makes sense, since that's how several other compilers
  101. handle this situation as well. */
  102. static gomp_mutex_t atomic_lock;
  103. void
  104. GOMP_atomic_start (void)
  105. {
  106. gomp_mutex_lock (&atomic_lock);
  107. }
  108. void
  109. GOMP_atomic_end (void)
  110. {
  111. gomp_mutex_unlock (&atomic_lock);
  112. }
  113. #if !GOMP_MUTEX_INIT_0
  114. static void __attribute__((constructor))
  115. initialize_critical (void)
  116. {
  117. gomp_mutex_init (&default_lock);
  118. gomp_mutex_init (&atomic_lock);
  119. #ifndef HAVE_SYNC_BUILTINS
  120. gomp_mutex_init (&create_lock_lock);
  121. #endif
  122. }
  123. #endif