bar.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 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. #ifndef GOMP_BARRIER_H
  24. #define GOMP_BARRIER_H 1
  25. #include "mutex.h"
  26. typedef struct
  27. {
  28. /* Make sure total/generation is in a mostly read cacheline, while
  29. awaited in a separate cacheline. */
  30. unsigned total __attribute__((aligned (64)));
  31. unsigned generation;
  32. unsigned awaited __attribute__((aligned (64)));
  33. unsigned awaited_final;
  34. } gomp_barrier_t;
  35. typedef unsigned int gomp_barrier_state_t;
  36. /* The generation field contains a counter in the high bits, with a few
  37. low bits dedicated to flags. Note that TASK_PENDING and WAS_LAST can
  38. share space because WAS_LAST is never stored back to generation. */
  39. #define BAR_TASK_PENDING 1
  40. #define BAR_WAS_LAST 1
  41. #define BAR_WAITING_FOR_TASK 2
  42. #define BAR_CANCELLED 4
  43. #define BAR_INCR 8
  44. static inline void gomp_barrier_init (gomp_barrier_t *bar, unsigned count)
  45. {
  46. bar->total = count;
  47. bar->awaited = count;
  48. bar->awaited_final = count;
  49. bar->generation = 0;
  50. }
  51. static inline void gomp_barrier_reinit (gomp_barrier_t *bar, unsigned count)
  52. {
  53. __atomic_add_fetch (&bar->awaited, count - bar->total, MEMMODEL_ACQ_REL);
  54. bar->total = count;
  55. }
  56. static inline void gomp_barrier_destroy (gomp_barrier_t *bar)
  57. {
  58. }
  59. extern void gomp_barrier_wait (gomp_barrier_t *);
  60. extern void gomp_barrier_wait_last (gomp_barrier_t *);
  61. extern void gomp_barrier_wait_end (gomp_barrier_t *, gomp_barrier_state_t);
  62. extern void gomp_team_barrier_wait (gomp_barrier_t *);
  63. extern void gomp_team_barrier_wait_final (gomp_barrier_t *);
  64. extern void gomp_team_barrier_wait_end (gomp_barrier_t *,
  65. gomp_barrier_state_t);
  66. extern bool gomp_team_barrier_wait_cancel (gomp_barrier_t *);
  67. extern bool gomp_team_barrier_wait_cancel_end (gomp_barrier_t *,
  68. gomp_barrier_state_t);
  69. extern void gomp_team_barrier_wake (gomp_barrier_t *, int);
  70. struct gomp_team;
  71. extern void gomp_team_barrier_cancel (struct gomp_team *);
  72. static inline gomp_barrier_state_t
  73. gomp_barrier_wait_start (gomp_barrier_t *bar)
  74. {
  75. unsigned int ret = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
  76. ret &= -BAR_INCR | BAR_CANCELLED;
  77. /* A memory barrier is needed before exiting from the various forms
  78. of gomp_barrier_wait, to satisfy OpenMP API version 3.1 section
  79. 2.8.6 flush Construct, which says there is an implicit flush during
  80. a barrier region. This is a convenient place to add the barrier,
  81. so we use MEMMODEL_ACQ_REL here rather than MEMMODEL_ACQUIRE. */
  82. if (__atomic_add_fetch (&bar->awaited, -1, MEMMODEL_ACQ_REL) == 0)
  83. ret |= BAR_WAS_LAST;
  84. return ret;
  85. }
  86. static inline gomp_barrier_state_t
  87. gomp_barrier_wait_cancel_start (gomp_barrier_t *bar)
  88. {
  89. return gomp_barrier_wait_start (bar);
  90. }
  91. /* This is like gomp_barrier_wait_start, except it decrements
  92. bar->awaited_final rather than bar->awaited and should be used
  93. for the gomp_team_end barrier only. */
  94. static inline gomp_barrier_state_t
  95. gomp_barrier_wait_final_start (gomp_barrier_t *bar)
  96. {
  97. unsigned int ret = __atomic_load_n (&bar->generation, MEMMODEL_ACQUIRE);
  98. ret &= -BAR_INCR | BAR_CANCELLED;
  99. /* See above gomp_barrier_wait_start comment. */
  100. if (__atomic_add_fetch (&bar->awaited_final, -1, MEMMODEL_ACQ_REL) == 0)
  101. ret |= BAR_WAS_LAST;
  102. return ret;
  103. }
  104. static inline bool
  105. gomp_barrier_last_thread (gomp_barrier_state_t state)
  106. {
  107. return state & BAR_WAS_LAST;
  108. }
  109. /* All the inlines below must be called with team->task_lock
  110. held. */
  111. static inline void
  112. gomp_team_barrier_set_task_pending (gomp_barrier_t *bar)
  113. {
  114. bar->generation |= BAR_TASK_PENDING;
  115. }
  116. static inline void
  117. gomp_team_barrier_clear_task_pending (gomp_barrier_t *bar)
  118. {
  119. bar->generation &= ~BAR_TASK_PENDING;
  120. }
  121. static inline void
  122. gomp_team_barrier_set_waiting_for_tasks (gomp_barrier_t *bar)
  123. {
  124. bar->generation |= BAR_WAITING_FOR_TASK;
  125. }
  126. static inline bool
  127. gomp_team_barrier_waiting_for_tasks (gomp_barrier_t *bar)
  128. {
  129. return (bar->generation & BAR_WAITING_FOR_TASK) != 0;
  130. }
  131. static inline bool
  132. gomp_team_barrier_cancelled (gomp_barrier_t *bar)
  133. {
  134. return __builtin_expect ((bar->generation & BAR_CANCELLED) != 0, 0);
  135. }
  136. static inline void
  137. gomp_team_barrier_done (gomp_barrier_t *bar, gomp_barrier_state_t state)
  138. {
  139. bar->generation = (state & -BAR_INCR) + BAR_INCR;
  140. }
  141. #endif /* GOMP_BARRIER_H */