single.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 SINGLE construct. */
  21. #include "libgomp.h"
  22. /* This routine is called when first encountering a SINGLE construct that
  23. doesn't have a COPYPRIVATE clause. Returns true if this is the thread
  24. that should execute the clause. */
  25. bool
  26. GOMP_single_start (void)
  27. {
  28. #ifdef HAVE_SYNC_BUILTINS
  29. struct gomp_thread *thr = gomp_thread ();
  30. struct gomp_team *team = thr->ts.team;
  31. unsigned long single_count;
  32. if (__builtin_expect (team == NULL, 0))
  33. return true;
  34. single_count = thr->ts.single_count++;
  35. return __sync_bool_compare_and_swap (&team->single_count, single_count,
  36. single_count + 1L);
  37. #else
  38. bool ret = gomp_work_share_start (false);
  39. if (ret)
  40. gomp_work_share_init_done ();
  41. gomp_work_share_end_nowait ();
  42. return ret;
  43. #endif
  44. }
  45. /* This routine is called when first encountering a SINGLE construct that
  46. does have a COPYPRIVATE clause. Returns NULL if this is the thread
  47. that should execute the clause; otherwise the return value is pointer
  48. given to GOMP_single_copy_end by the thread that did execute the clause. */
  49. void *
  50. GOMP_single_copy_start (void)
  51. {
  52. struct gomp_thread *thr = gomp_thread ();
  53. bool first;
  54. void *ret;
  55. first = gomp_work_share_start (false);
  56. if (first)
  57. {
  58. gomp_work_share_init_done ();
  59. ret = NULL;
  60. }
  61. else
  62. {
  63. gomp_team_barrier_wait (&thr->ts.team->barrier);
  64. ret = thr->ts.work_share->copyprivate;
  65. gomp_work_share_end_nowait ();
  66. }
  67. return ret;
  68. }
  69. /* This routine is called when the thread that entered a SINGLE construct
  70. with a COPYPRIVATE clause gets to the end of the construct. */
  71. void
  72. GOMP_single_copy_end (void *data)
  73. {
  74. struct gomp_thread *thr = gomp_thread ();
  75. struct gomp_team *team = thr->ts.team;
  76. if (team != NULL)
  77. {
  78. thr->ts.work_share->copyprivate = data;
  79. gomp_team_barrier_wait (&team->barrier);
  80. }
  81. gomp_work_share_end_nowait ();
  82. }