OSDN Git Service

PR libgomp/25877
[pf3gnuchains/gcc-fork.git] / libgomp / critical.c
1 /* Copyright (C) 2005 Free Software Foundation, Inc.
2    Contributed by Richard Henderson <rth@redhat.com>.
3
4    This file is part of the GNU OpenMP Library (libgomp).
5
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.
10
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.
15
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.  */
20
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.  */
27
28 /* This file handles the CRITICAL construct.  */
29
30 #include "libgomp.h"
31 #include <stdlib.h>
32
33
34 static gomp_mutex_t default_lock;
35
36 void
37 GOMP_critical_start (void)
38 {
39   gomp_mutex_lock (&default_lock);
40 }
41
42 void
43 GOMP_critical_end (void)
44 {
45   gomp_mutex_unlock (&default_lock);
46 }
47
48 #ifndef HAVE_SYNC_BUILTINS
49 static gomp_mutex_t create_lock_lock;
50 #endif
51
52 void
53 GOMP_critical_name_start (void **pptr)
54 {
55   gomp_mutex_t *plock;
56
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;
63
64   /* Otherwise we have to be prepared to malloc storage.  */
65   else
66     {
67       plock = *pptr;
68
69       if (plock == NULL)
70         {
71 #ifdef HAVE_SYNC_BUILTINS
72           gomp_mutex_t *nlock = gomp_malloc (sizeof (gomp_mutex_t));
73           gomp_mutex_init (nlock);
74
75           plock = __sync_val_compare_and_swap (pptr, plock, nlock);
76           if (plock != nlock)
77             {
78               gomp_mutex_destroy (nlock);
79               free (nlock);
80             }
81 #else
82           gomp_mutex_lock (&create_lock_lock);
83           plock = *pptr;
84           if (plock == NULL)
85             {
86               plock = gomp_malloc (sizeof (gomp_mutex_t));
87               gomp_mutex_init (plock);
88               __sync_synchronize ();
89               *pptr = plock;
90             }
91           gomp_mutex_unlock (&create_lock_lock);
92 #endif
93         }
94     }
95
96   gomp_mutex_lock (plock);
97 }
98
99 void
100 GOMP_critical_name_end (void **pptr)
101 {
102   gomp_mutex_t *plock;
103
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;
112
113   gomp_mutex_unlock (plock);
114 }
115
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.  */
122
123 static gomp_mutex_t atomic_lock;
124
125 void
126 GOMP_atomic_start (void)
127 {
128   gomp_mutex_lock (&atomic_lock);
129 }
130
131 void
132 GOMP_atomic_end (void)
133 {
134   gomp_mutex_unlock (&atomic_lock);
135 }
136
137 #if !GOMP_MUTEX_INIT_0
138 static void __attribute__((constructor))
139 initialize_critical (void)
140 {
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
146 }
147 #endif