nested_struct2.c: Remove ffi_type_mylong definition.
[official-gcc.git] / libgomp / critical.c
blob70159ec7a6ed0691ec523815d397619884acfc71
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, plock, nlock);
76 if (plock != nlock)
78 gomp_mutex_destroy (nlock);
79 free (nlock);
81 #else
82 gomp_mutex_lock (&create_lock_lock);
83 plock = *pptr;
84 if (plock == NULL)
86 plock = gomp_malloc (sizeof (gomp_mutex_t));
87 gomp_mutex_init (plock);
88 __sync_synchronize ();
89 *pptr = plock;
91 gomp_mutex_unlock (&create_lock_lock);
92 #endif
96 gomp_mutex_lock (plock);
99 void
100 GOMP_critical_name_end (void **pptr)
102 gomp_mutex_t *plock;
104 /* If a mutex fits within the space for a pointer, and is zero initialized,
105 then use the pointer space directly. */
106 if (GOMP_MUTEX_INIT_0
107 && sizeof (gomp_mutex_t) <= sizeof (void *)
108 && __alignof (gomp_mutex_t) <= sizeof (void *))
109 plock = (gomp_mutex_t *)pptr;
110 else
111 plock = *pptr;
113 gomp_mutex_unlock (plock);
116 /* This mutex is used when atomic operations don't exist for the target
117 in the mode requested. The result is not globally atomic, but works so
118 long as all parallel references are within #pragma omp atomic directives.
119 According to responses received from omp@openmp.org, appears to be within
120 spec. Which makes sense, since that's how several other compilers
121 handle this situation as well. */
123 static gomp_mutex_t atomic_lock;
125 void
126 GOMP_atomic_start (void)
128 gomp_mutex_lock (&atomic_lock);
131 void
132 GOMP_atomic_end (void)
134 gomp_mutex_unlock (&atomic_lock);
137 #if !GOMP_MUTEX_INIT_0
138 static void __attribute__((constructor))
139 initialize_critical (void)
141 gomp_mutex_init (&default_lock);
142 gomp_mutex_init (&atomic_lock);
143 #ifndef HAVE_SYNC_BUILTINS
144 gomp_mutex_init (&create_lock_lock);
145 #endif
147 #endif