bar.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright (C) 2005-2022 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 is a Linux specific implementation of a barrier synchronization
  21. mechanism for libgomp. This type is private to the library. This
  22. implementation uses atomic instructions and the futex syscall. */
  23. #include <limits.h>
  24. #include "wait.h"
  25. void
  26. gomp_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
  27. {
  28. if (__builtin_expect (state & BAR_WAS_LAST, 0))
  29. {
  30. /* Next time we'll be awaiting TOTAL threads again. */
  31. bar->awaited = bar->total;
  32. __atomic_store_n (&bar->generation, bar->generation + BAR_INCR,
  33. MEMMODEL_RELEASE);
  34. futex_wake ((int *) &bar->generation, INT_MAX);
  35. }
  36. else
  37. {
  38. do
  39. do_wait ((int *) &bar->generation, state);
  40. while (__atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE) == state);
  41. }
  42. }
  43. void
  44. gomp_barrier_wait (gomp_barrier_t *bar)
  45. {
  46. gomp_barrier_wait_end (bar, gomp_barrier_wait_start (bar));
  47. }
  48. /* Like gomp_barrier_wait, except that if the encountering thread
  49. is not the last one to hit the barrier, it returns immediately.
  50. The intended usage is that a thread which intends to gomp_barrier_destroy
  51. this barrier calls gomp_barrier_wait, while all other threads
  52. call gomp_barrier_wait_last. When gomp_barrier_wait returns,
  53. the barrier can be safely destroyed. */
  54. void
  55. gomp_barrier_wait_last (gomp_barrier_t *bar)
  56. {
  57. gomp_barrier_state_t state = gomp_barrier_wait_start (bar);
  58. if (state & BAR_WAS_LAST)
  59. gomp_barrier_wait_end (bar, state);
  60. }
  61. void
  62. gomp_team_barrier_wake (gomp_barrier_t *bar, int count)
  63. {
  64. futex_wake ((int *) &bar->generation, count == 0 ? INT_MAX : count);
  65. }
  66. void
  67. gomp_team_barrier_wait_end (gomp_barrier_t *bar, gomp_barrier_state_t state)
  68. {
  69. unsigned int generation, gen;
  70. if (__builtin_expect (state & BAR_WAS_LAST, 0))
  71. {
  72. /* Next time we'll be awaiting TOTAL threads again. */
  73. struct gomp_thread *thr = gomp_thread ();
  74. struct gomp_team *team = thr->ts.team;
  75. bar->awaited = bar->total;
  76. team->work_share_cancelled = 0;
  77. if (__builtin_expect (team->task_count, 0))
  78. {
  79. gomp_barrier_handle_tasks (state);
  80. state &= ~BAR_WAS_LAST;
  81. }
  82. else
  83. {
  84. state &= ~BAR_CANCELLED;
  85. state += BAR_INCR - BAR_WAS_LAST;
  86. __atomic_store_n (&bar->generation, state, MEMMODEL_RELEASE);
  87. futex_wake ((int *) &bar->generation, INT_MAX);
  88. return;
  89. }
  90. }
  91. generation = state;
  92. state &= ~BAR_CANCELLED;
  93. do
  94. {
  95. do_wait ((int *) &bar->generation, generation);
  96. gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
  97. if (__builtin_expect (gen & BAR_TASK_PENDING, 0))
  98. {
  99. gomp_barrier_handle_tasks (state);
  100. gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
  101. }
  102. generation |= gen & BAR_WAITING_FOR_TASK;
  103. }
  104. while (gen != state + BAR_INCR);
  105. }
  106. void
  107. gomp_team_barrier_wait (gomp_barrier_t *bar)
  108. {
  109. gomp_team_barrier_wait_end (bar, gomp_barrier_wait_start (bar));
  110. }
  111. void
  112. gomp_team_barrier_wait_final (gomp_barrier_t *bar)
  113. {
  114. gomp_barrier_state_t state = gomp_barrier_wait_final_start (bar);
  115. if (__builtin_expect (state & BAR_WAS_LAST, 0))
  116. bar->awaited_final = bar->total;
  117. gomp_team_barrier_wait_end (bar, state);
  118. }
  119. bool
  120. gomp_team_barrier_wait_cancel_end (gomp_barrier_t *bar,
  121. gomp_barrier_state_t state)
  122. {
  123. unsigned int generation, gen;
  124. if (__builtin_expect (state & BAR_WAS_LAST, 0))
  125. {
  126. /* Next time we'll be awaiting TOTAL threads again. */
  127. /* BAR_CANCELLED should never be set in state here, because
  128. cancellation means that at least one of the threads has been
  129. cancelled, thus on a cancellable barrier we should never see
  130. all threads to arrive. */
  131. struct gomp_thread *thr = gomp_thread ();
  132. struct gomp_team *team = thr->ts.team;
  133. bar->awaited = bar->total;
  134. team->work_share_cancelled = 0;
  135. if (__builtin_expect (team->task_count, 0))
  136. {
  137. gomp_barrier_handle_tasks (state);
  138. state &= ~BAR_WAS_LAST;
  139. }
  140. else
  141. {
  142. state += BAR_INCR - BAR_WAS_LAST;
  143. __atomic_store_n (&bar->generation, state, MEMMODEL_RELEASE);
  144. futex_wake ((int *) &bar->generation, INT_MAX);
  145. return false;
  146. }
  147. }
  148. if (__builtin_expect (state & BAR_CANCELLED, 0))
  149. return true;
  150. generation = state;
  151. do
  152. {
  153. do_wait ((int *) &bar->generation, generation);
  154. gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
  155. if (__builtin_expect (gen & BAR_CANCELLED, 0))
  156. return true;
  157. if (__builtin_expect (gen & BAR_TASK_PENDING, 0))
  158. {
  159. gomp_barrier_handle_tasks (state);
  160. gen = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
  161. }
  162. generation |= gen & BAR_WAITING_FOR_TASK;
  163. }
  164. while (gen != state + BAR_INCR);
  165. return false;
  166. }
  167. bool
  168. gomp_team_barrier_wait_cancel (gomp_barrier_t *bar)
  169. {
  170. return gomp_team_barrier_wait_cancel_end (bar, gomp_barrier_wait_start (bar));
  171. }
  172. void
  173. gomp_team_barrier_cancel (struct gomp_team *team)
  174. {
  175. gomp_mutex_lock (&team->task_lock);
  176. if (team->barrier.generation & BAR_CANCELLED)
  177. {
  178. gomp_mutex_unlock (&team->task_lock);
  179. return;
  180. }
  181. team->barrier.generation |= BAR_CANCELLED;
  182. gomp_mutex_unlock (&team->task_lock);
  183. futex_wake ((int *) &team->barrier.generation, INT_MAX);
  184. }