]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgomp/config/posix/lock.c
c-cppbuiltin.c (c_cpp_builtins): Change _OPENMP value to 200805.
[thirdparty/gcc.git] / libgomp / config / posix / lock.c
1 /* Copyright (C) 2005, 2008 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 is the default PTHREADS implementation of the public OpenMP
29 locking primitives.
30
31 Because OpenMP uses different entry points for normal and recursive
32 locks, and pthreads uses only one entry point, a system may be able
33 to do better and streamline the locking as well as reduce the size
34 of the types exported. */
35
36 /* We need Unix98 extensions to get recursive locks. On Tru64 UNIX V4.0F,
37 the declarations are available without _XOPEN_SOURCE, which actually
38 breaks compilation. */
39 #ifndef __osf__
40 #define _XOPEN_SOURCE 500
41 #endif
42
43 #include "libgomp.h"
44
45 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
46 void
47 gomp_init_lock_30 (omp_lock_t *lock)
48 {
49 pthread_mutex_init (lock, NULL);
50 }
51
52 void
53 gomp_destroy_lock_30 (omp_lock_t *lock)
54 {
55 pthread_mutex_destroy (lock);
56 }
57
58 void
59 gomp_set_lock_30 (omp_lock_t *lock)
60 {
61 pthread_mutex_lock (lock);
62 }
63
64 void
65 gomp_unset_lock_30 (omp_lock_t *lock)
66 {
67 pthread_mutex_unlock (lock);
68 }
69
70 int
71 gomp_test_lock_30 (omp_lock_t *lock)
72 {
73 return pthread_mutex_trylock (lock) == 0;
74 }
75
76 void
77 gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
78 {
79 pthread_mutex_init (&lock->lock, NULL);
80 lock->count = 0;
81 lock->owner = NULL;
82 }
83
84 void
85 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
86 {
87 pthread_mutex_destroy (&lock->lock);
88 }
89
90 void
91 gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
92 {
93 void *me = gomp_icv (true);
94
95 if (lock->owner != me)
96 {
97 pthread_mutex_lock (&lock->lock);
98 lock->owner = me;
99 }
100 lock->count++;
101 }
102
103 void
104 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
105 {
106 if (--lock->count == 0)
107 {
108 lock->owner = NULL;
109 pthread_mutex_unlock (&lock->lock);
110 }
111 }
112
113 int
114 gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
115 {
116 void *me = gomp_icv (true);
117
118 if (lock->owner != me)
119 {
120 if (pthread_mutex_trylock (&lock->lock) != 0)
121 return 0;
122 lock->owner = me;
123 }
124
125 return ++lock->count;
126 }
127
128 #else
129
130 void
131 gomp_init_lock_30 (omp_lock_t *lock)
132 {
133 sem_init (lock, 0, 1);
134 }
135
136 void
137 gomp_destroy_lock_30 (omp_lock_t *lock)
138 {
139 sem_destroy (lock);
140 }
141
142 void
143 gomp_set_lock_30 (omp_lock_t *lock)
144 {
145 while (sem_wait (lock) != 0)
146 ;
147 }
148
149 void
150 gomp_unset_lock_30 (omp_lock_t *lock)
151 {
152 sem_post (lock);
153 }
154
155 int
156 gomp_test_lock_30 (omp_lock_t *lock)
157 {
158 return sem_trywait (lock) == 0;
159 }
160
161 void
162 gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
163 {
164 sem_init (&lock->lock, 0, 1);
165 lock->count = 0;
166 lock->owner = NULL;
167 }
168
169 void
170 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
171 {
172 sem_destroy (&lock->lock);
173 }
174
175 void
176 gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
177 {
178 void *me = gomp_icv (true);
179
180 if (lock->owner != me)
181 {
182 while (sem_wait (&lock->lock) != 0)
183 ;
184 lock->owner = me;
185 }
186 lock->count++;
187 }
188
189 void
190 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
191 {
192 if (--lock->count == 0)
193 {
194 lock->owner = NULL;
195 sem_post (&lock->lock);
196 }
197 }
198
199 int
200 gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
201 {
202 void *me = gomp_icv (true);
203
204 if (lock->owner != me)
205 {
206 if (sem_trywait (&lock->lock) != 0)
207 return 0;
208 lock->owner = me;
209 }
210
211 return ++lock->count;
212 }
213 #endif
214
215 #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
216 void
217 gomp_init_lock_25 (omp_lock_25_t *lock)
218 {
219 pthread_mutex_init (lock, NULL);
220 }
221
222 void
223 gomp_destroy_lock_25 (omp_lock_25_t *lock)
224 {
225 pthread_mutex_destroy (lock);
226 }
227
228 void
229 gomp_set_lock_25 (omp_lock_25_t *lock)
230 {
231 pthread_mutex_lock (lock);
232 }
233
234 void
235 gomp_unset_lock_25 (omp_lock_25_t *lock)
236 {
237 pthread_mutex_unlock (lock);
238 }
239
240 int
241 gomp_test_lock_25 (omp_lock_25_t *lock)
242 {
243 return pthread_mutex_trylock (lock) == 0;
244 }
245
246 void
247 gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock)
248 {
249 pthread_mutexattr_t attr;
250
251 pthread_mutexattr_init (&attr);
252 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
253 pthread_mutex_init (&lock->lock, &attr);
254 lock->count = 0;
255 pthread_mutexattr_destroy (&attr);
256 }
257
258 void
259 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock)
260 {
261 pthread_mutex_destroy (&lock->lock);
262 }
263
264 void
265 gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock)
266 {
267 pthread_mutex_lock (&lock->lock);
268 lock->count++;
269 }
270
271 void
272 gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock)
273 {
274 lock->count--;
275 pthread_mutex_unlock (&lock->lock);
276 }
277
278 int
279 gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock)
280 {
281 if (pthread_mutex_trylock (&lock->lock) == 0)
282 return ++lock->count;
283 return 0;
284 }
285
286 omp_lock_symver (omp_init_lock)
287 omp_lock_symver (omp_destroy_lock)
288 omp_lock_symver (omp_set_lock)
289 omp_lock_symver (omp_unset_lock)
290 omp_lock_symver (omp_test_lock)
291 omp_lock_symver (omp_init_nest_lock)
292 omp_lock_symver (omp_destroy_nest_lock)
293 omp_lock_symver (omp_set_nest_lock)
294 omp_lock_symver (omp_unset_nest_lock)
295 omp_lock_symver (omp_test_nest_lock)
296
297 #else
298
299 ialias (omp_init_lock)
300 ialias (omp_init_nest_lock)
301 ialias (omp_destroy_lock)
302 ialias (omp_destroy_nest_lock)
303 ialias (omp_set_lock)
304 ialias (omp_set_nest_lock)
305 ialias (omp_unset_lock)
306 ialias (omp_unset_nest_lock)
307 ialias (omp_test_lock)
308 ialias (omp_test_nest_lock)
309
310 #endif