OSDN Git Service

2007-01-14 Daniel Franke <franke.daniel@gmail.com>
[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, NULL, nlock);
76           if (plock != NULL)
77             {
78               gomp_mutex_destroy (nlock);
79               free (nlock);
80             }
81           else
82             plock = nlock;
83 #else
84           gomp_mutex_lock (&create_lock_lock);
85           plock = *pptr;
86           if (plock == NULL)
87             {
88               plock = gomp_malloc (sizeof (gomp_mutex_t));
89               gomp_mutex_init (plock);
90               __sync_synchronize ();
91               *pptr = plock;
92             }
93           gomp_mutex_unlock (&create_lock_lock);
94 #endif
95         }
96     }
97
98   gomp_mutex_lock (plock);
99 }
100
101 void
102 GOMP_critical_name_end (void **pptr)
103 {
104   gomp_mutex_t *plock;
105
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;
114
115   gomp_mutex_unlock (plock);
116 }
117
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.  */
124
125 static gomp_mutex_t atomic_lock;
126
127 void
128 GOMP_atomic_start (void)
129 {
130   gomp_mutex_lock (&atomic_lock);
131 }
132
133 void
134 GOMP_atomic_end (void)
135 {
136   gomp_mutex_unlock (&atomic_lock);
137 }
138
139 #if !GOMP_MUTEX_INIT_0
140 static void __attribute__((constructor))
141 initialize_critical (void)
142 {
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
148 }
149 #endif