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