1*404b540aSrobert /* Copyright (C) 2005 Free Software Foundation, Inc.
2*404b540aSrobert Contributed by Richard Henderson <rth@redhat.com>.
3*404b540aSrobert
4*404b540aSrobert This file is part of the GNU OpenMP Library (libgomp).
5*404b540aSrobert
6*404b540aSrobert Libgomp is free software; you can redistribute it and/or modify it
7*404b540aSrobert under the terms of the GNU Lesser General Public License as published by
8*404b540aSrobert the Free Software Foundation; either version 2.1 of the License, or
9*404b540aSrobert (at your option) any later version.
10*404b540aSrobert
11*404b540aSrobert Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
12*404b540aSrobert WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13*404b540aSrobert FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14*404b540aSrobert more details.
15*404b540aSrobert
16*404b540aSrobert You should have received a copy of the GNU Lesser General Public License
17*404b540aSrobert along with libgomp; see the file COPYING.LIB. If not, write to the
18*404b540aSrobert Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19*404b540aSrobert MA 02110-1301, USA. */
20*404b540aSrobert
21*404b540aSrobert /* As a special exception, if you link this library with other files, some
22*404b540aSrobert of which are compiled with GCC, to produce an executable, this library
23*404b540aSrobert does not by itself cause the resulting executable to be covered by the
24*404b540aSrobert GNU General Public License. This exception does not however invalidate
25*404b540aSrobert any other reasons why the executable file might be covered by the GNU
26*404b540aSrobert General Public License. */
27*404b540aSrobert
28*404b540aSrobert /* This file handles the CRITICAL construct. */
29*404b540aSrobert
30*404b540aSrobert #include "libgomp.h"
31*404b540aSrobert #include <stdlib.h>
32*404b540aSrobert
33*404b540aSrobert
34*404b540aSrobert static gomp_mutex_t default_lock;
35*404b540aSrobert
36*404b540aSrobert void
GOMP_critical_start(void)37*404b540aSrobert GOMP_critical_start (void)
38*404b540aSrobert {
39*404b540aSrobert gomp_mutex_lock (&default_lock);
40*404b540aSrobert }
41*404b540aSrobert
42*404b540aSrobert void
GOMP_critical_end(void)43*404b540aSrobert GOMP_critical_end (void)
44*404b540aSrobert {
45*404b540aSrobert gomp_mutex_unlock (&default_lock);
46*404b540aSrobert }
47*404b540aSrobert
48*404b540aSrobert #ifndef HAVE_SYNC_BUILTINS
49*404b540aSrobert static gomp_mutex_t create_lock_lock;
50*404b540aSrobert #endif
51*404b540aSrobert
52*404b540aSrobert void
GOMP_critical_name_start(void ** pptr)53*404b540aSrobert GOMP_critical_name_start (void **pptr)
54*404b540aSrobert {
55*404b540aSrobert gomp_mutex_t *plock;
56*404b540aSrobert
57*404b540aSrobert /* If a mutex fits within the space for a pointer, and is zero initialized,
58*404b540aSrobert then use the pointer space directly. */
59*404b540aSrobert if (GOMP_MUTEX_INIT_0
60*404b540aSrobert && sizeof (gomp_mutex_t) <= sizeof (void *)
61*404b540aSrobert && __alignof (gomp_mutex_t) <= sizeof (void *))
62*404b540aSrobert plock = (gomp_mutex_t *)pptr;
63*404b540aSrobert
64*404b540aSrobert /* Otherwise we have to be prepared to malloc storage. */
65*404b540aSrobert else
66*404b540aSrobert {
67*404b540aSrobert plock = *pptr;
68*404b540aSrobert
69*404b540aSrobert if (plock == NULL)
70*404b540aSrobert {
71*404b540aSrobert #ifdef HAVE_SYNC_BUILTINS
72*404b540aSrobert gomp_mutex_t *nlock = gomp_malloc (sizeof (gomp_mutex_t));
73*404b540aSrobert gomp_mutex_init (nlock);
74*404b540aSrobert
75*404b540aSrobert plock = __sync_val_compare_and_swap (pptr, NULL, nlock);
76*404b540aSrobert if (plock != NULL)
77*404b540aSrobert {
78*404b540aSrobert gomp_mutex_destroy (nlock);
79*404b540aSrobert free (nlock);
80*404b540aSrobert }
81*404b540aSrobert else
82*404b540aSrobert plock = nlock;
83*404b540aSrobert #else
84*404b540aSrobert gomp_mutex_lock (&create_lock_lock);
85*404b540aSrobert plock = *pptr;
86*404b540aSrobert if (plock == NULL)
87*404b540aSrobert {
88*404b540aSrobert plock = gomp_malloc (sizeof (gomp_mutex_t));
89*404b540aSrobert gomp_mutex_init (plock);
90*404b540aSrobert __sync_synchronize ();
91*404b540aSrobert *pptr = plock;
92*404b540aSrobert }
93*404b540aSrobert gomp_mutex_unlock (&create_lock_lock);
94*404b540aSrobert #endif
95*404b540aSrobert }
96*404b540aSrobert }
97*404b540aSrobert
98*404b540aSrobert gomp_mutex_lock (plock);
99*404b540aSrobert }
100*404b540aSrobert
101*404b540aSrobert void
GOMP_critical_name_end(void ** pptr)102*404b540aSrobert GOMP_critical_name_end (void **pptr)
103*404b540aSrobert {
104*404b540aSrobert gomp_mutex_t *plock;
105*404b540aSrobert
106*404b540aSrobert /* If a mutex fits within the space for a pointer, and is zero initialized,
107*404b540aSrobert then use the pointer space directly. */
108*404b540aSrobert if (GOMP_MUTEX_INIT_0
109*404b540aSrobert && sizeof (gomp_mutex_t) <= sizeof (void *)
110*404b540aSrobert && __alignof (gomp_mutex_t) <= sizeof (void *))
111*404b540aSrobert plock = (gomp_mutex_t *)pptr;
112*404b540aSrobert else
113*404b540aSrobert plock = *pptr;
114*404b540aSrobert
115*404b540aSrobert gomp_mutex_unlock (plock);
116*404b540aSrobert }
117*404b540aSrobert
118*404b540aSrobert /* This mutex is used when atomic operations don't exist for the target
119*404b540aSrobert in the mode requested. The result is not globally atomic, but works so
120*404b540aSrobert long as all parallel references are within #pragma omp atomic directives.
121*404b540aSrobert According to responses received from omp@openmp.org, appears to be within
122*404b540aSrobert spec. Which makes sense, since that's how several other compilers
123*404b540aSrobert handle this situation as well. */
124*404b540aSrobert
125*404b540aSrobert static gomp_mutex_t atomic_lock;
126*404b540aSrobert
127*404b540aSrobert void
GOMP_atomic_start(void)128*404b540aSrobert GOMP_atomic_start (void)
129*404b540aSrobert {
130*404b540aSrobert gomp_mutex_lock (&atomic_lock);
131*404b540aSrobert }
132*404b540aSrobert
133*404b540aSrobert void
GOMP_atomic_end(void)134*404b540aSrobert GOMP_atomic_end (void)
135*404b540aSrobert {
136*404b540aSrobert gomp_mutex_unlock (&atomic_lock);
137*404b540aSrobert }
138*404b540aSrobert
139*404b540aSrobert #if !GOMP_MUTEX_INIT_0
140*404b540aSrobert static void __attribute__((constructor))
initialize_critical(void)141*404b540aSrobert initialize_critical (void)
142*404b540aSrobert {
143*404b540aSrobert gomp_mutex_init (&default_lock);
144*404b540aSrobert gomp_mutex_init (&atomic_lock);
145*404b540aSrobert #ifndef HAVE_SYNC_BUILTINS
146*404b540aSrobert gomp_mutex_init (&create_lock_lock);
147*404b540aSrobert #endif
148*404b540aSrobert }
149*404b540aSrobert #endif
150