bar.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 the default implementation of a barrier synchronization mechanism
  21. for libgomp. This type is private to the library. Note that we rely on
  22. being able to adjust the barrier count while threads are blocked, so the
  23. POSIX pthread_barrier_t won't work. */
  24. #ifndef GOMP_BARRIER_H
  25. #define GOMP_BARRIER_H 1
  26. #include <pthread.h>
  27. typedef struct
  28. {
  29. gomp_mutex_t mutex1;
  30. #ifndef HAVE_SYNC_BUILTINS
  31. gomp_mutex_t mutex2;
  32. #endif
  33. gomp_sem_t sem1;
  34. gomp_sem_t sem2;
  35. unsigned total;
  36. unsigned arrived;
  37. unsigned generation;
  38. bool cancellable;
  39. } gomp_barrier_t;
  40. typedef unsigned int gomp_barrier_state_t;
  41. /* The generation field contains a counter in the high bits, with a few
  42. low bits dedicated to flags. Note that TASK_PENDING and WAS_LAST can
  43. share space because WAS_LAST is never stored back to generation. */
  44. #define BAR_TASK_PENDING 1
  45. #define BAR_WAS_LAST 1
  46. #define BAR_WAITING_FOR_TASK 2
  47. #define BAR_CANCELLED 4
  48. #define BAR_INCR 8
  49. extern void gomp_barrier_init (gomp_barrier_t *, unsigned);
  50. extern void gomp_barrier_reinit (gomp_barrier_t *, unsigned);
  51. extern void gomp_barrier_destroy (gomp_barrier_t *);
  52. extern void gomp_barrier_wait (gomp_barrier_t *);
  53. extern void gomp_barrier_wait_end (gomp_barrier_t *, gomp_barrier_state_t);
  54. extern void gomp_team_barrier_wait (gomp_barrier_t *);
  55. extern void gomp_team_barrier_wait_end (gomp_barrier_t *,
  56. gomp_barrier_state_t);
  57. extern bool gomp_team_barrier_wait_cancel (gomp_barrier_t *);
  58. extern bool gomp_team_barrier_wait_cancel_end (gomp_barrier_t *,
  59. gomp_barrier_state_t);
  60. extern void gomp_team_barrier_wake (gomp_barrier_t *, int);
  61. struct gomp_team;
  62. extern void gomp_team_barrier_cancel (struct gomp_team *);
  63. static inline gomp_barrier_state_t
  64. gomp_barrier_wait_start (gomp_barrier_t *bar)
  65. {
  66. unsigned int ret;
  67. gomp_mutex_lock (&bar->mutex1);
  68. ret = bar->generation & (-BAR_INCR | BAR_CANCELLED);
  69. if (++bar->arrived == bar->total)
  70. ret |= BAR_WAS_LAST;
  71. return ret;
  72. }
  73. static inline gomp_barrier_state_t
  74. gomp_barrier_wait_cancel_start (gomp_barrier_t *bar)
  75. {
  76. unsigned int ret;
  77. gomp_mutex_lock (&bar->mutex1);
  78. ret = bar->generation & (-BAR_INCR | BAR_CANCELLED);
  79. if (ret & BAR_CANCELLED)
  80. return ret;
  81. if (++bar->arrived == bar->total)
  82. ret |= BAR_WAS_LAST;
  83. return ret;
  84. }
  85. static inline void
  86. gomp_team_barrier_wait_final (gomp_barrier_t *bar)
  87. {
  88. gomp_team_barrier_wait (bar);
  89. }
  90. static inline bool
  91. gomp_barrier_last_thread (gomp_barrier_state_t state)
  92. {
  93. return state & BAR_WAS_LAST;
  94. }
  95. static inline void
  96. gomp_barrier_wait_last (gomp_barrier_t *bar)
  97. {
  98. gomp_barrier_wait (bar);
  99. }
  100. /* All the inlines below must be called with team->task_lock
  101. held. */
  102. static inline void
  103. gomp_team_barrier_set_task_pending (gomp_barrier_t *bar)
  104. {
  105. bar->generation |= BAR_TASK_PENDING;
  106. }
  107. static inline void
  108. gomp_team_barrier_clear_task_pending (gomp_barrier_t *bar)
  109. {
  110. bar->generation &= ~BAR_TASK_PENDING;
  111. }
  112. static inline void
  113. gomp_team_barrier_set_waiting_for_tasks (gomp_barrier_t *bar)
  114. {
  115. bar->generation |= BAR_WAITING_FOR_TASK;
  116. }
  117. static inline bool
  118. gomp_team_barrier_waiting_for_tasks (gomp_barrier_t *bar)
  119. {
  120. return (bar->generation & BAR_WAITING_FOR_TASK) != 0;
  121. }
  122. static inline bool
  123. gomp_team_barrier_cancelled (gomp_barrier_t *bar)
  124. {
  125. return __builtin_expect ((bar->generation & BAR_CANCELLED) != 0, 0);
  126. }
  127. static inline void
  128. gomp_team_barrier_done (gomp_barrier_t *bar, gomp_barrier_state_t state)
  129. {
  130. bar->generation = (state & -BAR_INCR) + BAR_INCR;
  131. }
  132. #endif /* GOMP_BARRIER_H */