1169695Skan/* Copyright (C) 2005 Free Software Foundation, Inc.
2169695Skan   Contributed by Richard Henderson <rth@redhat.com>.
3169695Skan
4169695Skan   This file is part of the GNU OpenMP Library (libgomp).
5169695Skan
6169695Skan   Libgomp is free software; you can redistribute it and/or modify it
7169695Skan   under the terms of the GNU Lesser General Public License as published by
8169695Skan   the Free Software Foundation; either version 2.1 of the License, or
9169695Skan   (at your option) any later version.
10169695Skan
11169695Skan   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12169695Skan   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13169695Skan   FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
14169695Skan   more details.
15169695Skan
16169695Skan   You should have received a copy of the GNU Lesser General Public License
17169695Skan   along with libgomp; see the file COPYING.LIB.  If not, write to the
18169695Skan   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19169695Skan   MA 02110-1301, USA.  */
20169695Skan
21169695Skan/* As a special exception, if you link this library with other files, some
22169695Skan   of which are compiled with GCC, to produce an executable, this library
23169695Skan   does not by itself cause the resulting executable to be covered by the
24169695Skan   GNU General Public License.  This exception does not however invalidate
25169695Skan   any other reasons why the executable file might be covered by the GNU
26169695Skan   General Public License.  */
27169695Skan
28169695Skan/* This file handles the SINGLE construct.  */
29169695Skan
30169695Skan#include "libgomp.h"
31169695Skan
32169695Skan
33169695Skan/* This routine is called when first encountering a SINGLE construct that
34169695Skan   doesn't have a COPYPRIVATE clause.  Returns true if this is the thread
35169695Skan   that should execute the clause.  */
36169695Skan
37169695Skanbool
38169695SkanGOMP_single_start (void)
39169695Skan{
40169695Skan  bool ret = gomp_work_share_start (false);
41169695Skan  gomp_mutex_unlock (&gomp_thread ()->ts.work_share->lock);
42169695Skan  gomp_work_share_end_nowait ();
43169695Skan  return ret;
44169695Skan}
45169695Skan
46169695Skan/* This routine is called when first encountering a SINGLE construct that
47169695Skan   does have a COPYPRIVATE clause.  Returns NULL if this is the thread
48169695Skan   that should execute the clause; otherwise the return value is pointer
49169695Skan   given to GOMP_single_copy_end by the thread that did execute the clause.  */
50169695Skan
51169695Skanvoid *
52169695SkanGOMP_single_copy_start (void)
53169695Skan{
54169695Skan  struct gomp_thread *thr = gomp_thread ();
55169695Skan
56169695Skan  bool first;
57169695Skan  void *ret;
58169695Skan
59169695Skan  first = gomp_work_share_start (false);
60169695Skan  gomp_mutex_unlock (&thr->ts.work_share->lock);
61169695Skan
62169695Skan  if (first)
63169695Skan    ret = NULL;
64169695Skan  else
65169695Skan    {
66169695Skan      gomp_barrier_wait (&thr->ts.team->barrier);
67169695Skan
68169695Skan      ret = thr->ts.work_share->copyprivate;
69169695Skan      gomp_work_share_end_nowait ();
70169695Skan    }
71169695Skan
72169695Skan  return ret;
73169695Skan}
74169695Skan
75169695Skan/* This routine is called when the thread that entered a SINGLE construct
76169695Skan   with a COPYPRIVATE clause gets to the end of the construct.  */
77169695Skan
78169695Skanvoid
79169695SkanGOMP_single_copy_end (void *data)
80169695Skan{
81169695Skan  struct gomp_thread *thr = gomp_thread ();
82169695Skan  struct gomp_team *team = thr->ts.team;
83169695Skan
84169695Skan  if (team != NULL)
85169695Skan    {
86169695Skan      thr->ts.work_share->copyprivate = data;
87169695Skan      gomp_barrier_wait (&team->barrier);
88169695Skan    }
89169695Skan
90169695Skan  gomp_work_share_end_nowait ();
91169695Skan}
92