]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgomp/config/posix95/lock.c
* c-cppbuiltin.c (c_cpp_builtins): Change _OPENMP value to
[thirdparty/gcc.git] / libgomp / config / posix95 / lock.c
CommitLineData
fd6481cf 1/* Copyright (C) 2006, 2008 Free Software Foundation, Inc.
c1c80391 2
3 This file is part of the GNU OpenMP Library (libgomp).
4
5 Libgomp is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
13 more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with libgomp; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301, USA. */
19
20/* As a special exception, if you link this library with other files, some
21 of which are compiled with GCC, to produce an executable, this library
22 does not by itself cause the resulting executable to be covered by the
23 GNU General Public License. This exception does not however invalidate
24 any other reasons why the executable file might be covered by the GNU
25 General Public License. */
26
27/* This is the POSIX95 implementation of the public OpenMP locking primitives.
28
29 Because OpenMP uses different entry points for normal and recursive
30 locks, and pthreads uses only one entry point, a system may be able
31 to do better and streamline the locking as well as reduce the size
32 of the types exported. */
33
34#include "libgomp.h"
35
fd6481cf 36#ifdef HAVE_BROKEN_POSIX_SEMAPHORES
37void
38gomp_init_lock_30 (omp_lock_t *lock)
39{
40 pthread_mutex_init (lock, NULL);
41}
42
43void
44gomp_destroy_lock_30 (omp_lock_t *lock)
45{
46 pthread_mutex_destroy (lock);
47}
48
49void
50gomp_set_lock_30 (omp_lock_t *lock)
51{
52 pthread_mutex_lock (lock);
53}
54
55void
56gomp_unset_lock_30 (omp_lock_t *lock)
57{
58 pthread_mutex_unlock (lock);
59}
60
61int
62gomp_test_lock_30 (omp_lock_t *lock)
63{
64 return pthread_mutex_trylock (lock) == 0;
65}
66
67void
68gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
69{
70 pthread_mutex_init (&lock->lock, NULL);
71 lock->owner = NULL;
72 lock->count = 0;
73}
74
75void
76gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
77{
78 pthread_mutex_destroy (&lock->lock);
79}
c1c80391 80
81void
fd6481cf 82gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
83{
84 void *me = gomp_icv (true);
85
86 if (lock->owner != me)
87 {
88 pthread_mutex_lock (&lock->lock);
89 lock->owner = me;
90 }
91
92 lock->count++;
93}
94
95void
96gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
97{
98 lock->count--;
99
100 if (lock->count == 0)
101 {
102 lock->owner = NULL;
103 pthread_mutex_unlock (&lock->lock);
104 }
105}
106
107int
108gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
109{
110 void *me = gomp_icv (true);
111
112 if (lock->owner != me)
113 {
114 if (pthread_mutex_trylock (&lock->lock) != 0)
115 return 0;
116 lock->owner = me;
117 }
118
119 return ++lock->count;
120}
121
122#else
123
124void
125gomp_init_lock_30 (omp_lock_t *lock)
126{
127 sem_init (lock, 0, 1);
128}
129
130void
131gomp_destroy_lock_30 (omp_lock_t *lock)
132{
133 sem_destroy (lock);
134}
135
136void
137gomp_set_lock_30 (omp_lock_t *lock)
138{
139 while (sem_wait (lock) != 0)
140 ;
141}
142
143void
144gomp_unset_lock_30 (omp_lock_t *lock)
145{
146 sem_post (lock);
147}
148
149int
150gomp_test_lock_30 (omp_lock_t *lock)
151{
152 return sem_trywait (lock) == 0;
153}
154
155void
156gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
157{
158 sem_init (&lock->lock, 0, 1);
159 lock->count = 0;
160 lock->owner = NULL;
161}
162
163void
164gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
165{
166 sem_destroy (&lock->lock);
167}
168
169void
170gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
171{
172 void *me = gomp_icv (true);
173
174 if (lock->owner != me)
175 {
176 while (sem_wait (&lock->lock) != 0)
177 ;
178 lock->owner = me;
179 }
180 lock->count++;
181}
182
183void
184gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
185{
186 if (--lock->count == 0)
187 {
188 lock->owner = NULL;
189 sem_post (&lock->lock);
190 }
191}
192
193int
194gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
195{
196 void *me = gomp_icv (true);
197
198 if (lock->owner != me)
199 {
200 if (sem_trywait (&lock->lock) != 0)
201 return 0;
202 lock->owner = me;
203 }
204
205 return ++lock->count;
206}
207#endif
208
209#ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
210void
211gomp_init_lock_25 (omp_lock_25_t *lock)
c1c80391 212{
213 pthread_mutex_init (lock, NULL);
214}
215
216void
fd6481cf 217gomp_destroy_lock_25 (omp_lock_25_t *lock)
c1c80391 218{
219 pthread_mutex_destroy (lock);
220}
221
222void
fd6481cf 223gomp_set_lock_25 (omp_lock_25_t *lock)
c1c80391 224{
225 pthread_mutex_lock (lock);
226}
227
228void
fd6481cf 229gomp_unset_lock_25 (omp_lock_25_t *lock)
c1c80391 230{
231 pthread_mutex_unlock (lock);
232}
233
234int
fd6481cf 235gomp_test_lock_25 (omp_lock_25_t *lock)
c1c80391 236{
237 return pthread_mutex_trylock (lock) == 0;
238}
239
240void
fd6481cf 241gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock)
c1c80391 242{
243 pthread_mutex_init (&lock->lock, NULL);
244 lock->owner = (pthread_t) 0;
245 lock->count = 0;
246}
247
248void
fd6481cf 249gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock)
c1c80391 250{
251 pthread_mutex_destroy (&lock->lock);
252}
253
254void
fd6481cf 255gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock)
c1c80391 256{
257 pthread_t me = pthread_self ();
258
259 if (lock->owner != me)
260 {
261 pthread_mutex_lock (&lock->lock);
262 lock->owner = me;
263 }
264
265 lock->count++;
266}
267
268void
fd6481cf 269gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock)
c1c80391 270{
271 lock->count--;
272
273 if (lock->count == 0)
274 {
275 lock->owner = (pthread_t) 0;
276 pthread_mutex_unlock (&lock->lock);
277 }
278}
279
280int
fd6481cf 281gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock)
c1c80391 282{
283 pthread_t me = pthread_self ();
284
285 if (lock->owner != me)
286 {
287 if (pthread_mutex_trylock (&lock->lock) != 0)
288 return 0;
289 lock->owner = me;
290 }
291
292 return ++lock->count;
293}
294
fd6481cf 295omp_lock_symver (omp_init_lock)
296omp_lock_symver (omp_destroy_lock)
297omp_lock_symver (omp_set_lock)
298omp_lock_symver (omp_unset_lock)
299omp_lock_symver (omp_test_lock)
300omp_lock_symver (omp_init_nest_lock)
301omp_lock_symver (omp_destroy_nest_lock)
302omp_lock_symver (omp_set_nest_lock)
303omp_lock_symver (omp_unset_nest_lock)
304omp_lock_symver (omp_test_nest_lock)
305
306#else
307
c1c80391 308ialias (omp_init_lock)
309ialias (omp_init_nest_lock)
310ialias (omp_destroy_lock)
311ialias (omp_destroy_nest_lock)
312ialias (omp_set_lock)
313ialias (omp_set_nest_lock)
314ialias (omp_unset_lock)
315ialias (omp_unset_nest_lock)
316ialias (omp_test_lock)
317ialias (omp_test_nest_lock)
fd6481cf 318
319#endif