Merge with trank @ 137446
[official-gcc.git] / libgomp / config / posix95 / lock.c
blobe27437ead16b1110412fbb3cfa70d2a1e0a4ed4c
1 /* Copyright (C) 2006, 2008 Free Software Foundation, Inc.
3 This file is part of the GNU OpenMP Library (libgomp).
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.
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.
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. */
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. */
27 /* This is the POSIX95 implementation of the public OpenMP locking primitives.
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. */
34 #include "libgomp.h"
36 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
37 void
38 gomp_init_lock_30 (omp_lock_t *lock)
40 pthread_mutex_init (lock, NULL);
43 void
44 gomp_destroy_lock_30 (omp_lock_t *lock)
46 pthread_mutex_destroy (lock);
49 void
50 gomp_set_lock_30 (omp_lock_t *lock)
52 pthread_mutex_lock (lock);
55 void
56 gomp_unset_lock_30 (omp_lock_t *lock)
58 pthread_mutex_unlock (lock);
61 int
62 gomp_test_lock_30 (omp_lock_t *lock)
64 return pthread_mutex_trylock (lock) == 0;
67 void
68 gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
70 pthread_mutex_init (&lock->lock, NULL);
71 lock->owner = NULL;
72 lock->count = 0;
75 void
76 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
78 pthread_mutex_destroy (&lock->lock);
81 void
82 gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
84 void *me = gomp_icv (true);
86 if (lock->owner != me)
88 pthread_mutex_lock (&lock->lock);
89 lock->owner = me;
92 lock->count++;
95 void
96 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
98 lock->count--;
100 if (lock->count == 0)
102 lock->owner = NULL;
103 pthread_mutex_unlock (&lock->lock);
108 gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
110 void *me = gomp_icv (true);
112 if (lock->owner != me)
114 if (pthread_mutex_trylock (&lock->lock) != 0)
115 return 0;
116 lock->owner = me;
119 return ++lock->count;
122 #else
124 void
125 gomp_init_lock_30 (omp_lock_t *lock)
127 sem_init (lock, 0, 1);
130 void
131 gomp_destroy_lock_30 (omp_lock_t *lock)
133 sem_destroy (lock);
136 void
137 gomp_set_lock_30 (omp_lock_t *lock)
139 while (sem_wait (lock) != 0)
143 void
144 gomp_unset_lock_30 (omp_lock_t *lock)
146 sem_post (lock);
150 gomp_test_lock_30 (omp_lock_t *lock)
152 return sem_trywait (lock) == 0;
155 void
156 gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
158 sem_init (&lock->lock, 0, 1);
159 lock->count = 0;
160 lock->owner = NULL;
163 void
164 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
166 sem_destroy (&lock->lock);
169 void
170 gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
172 void *me = gomp_icv (true);
174 if (lock->owner != me)
176 while (sem_wait (&lock->lock) != 0)
178 lock->owner = me;
180 lock->count++;
183 void
184 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
186 if (--lock->count == 0)
188 lock->owner = NULL;
189 sem_post (&lock->lock);
194 gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
196 void *me = gomp_icv (true);
198 if (lock->owner != me)
200 if (sem_trywait (&lock->lock) != 0)
201 return 0;
202 lock->owner = me;
205 return ++lock->count;
207 #endif
209 #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
210 void
211 gomp_init_lock_25 (omp_lock_25_t *lock)
213 pthread_mutex_init (lock, NULL);
216 void
217 gomp_destroy_lock_25 (omp_lock_25_t *lock)
219 pthread_mutex_destroy (lock);
222 void
223 gomp_set_lock_25 (omp_lock_25_t *lock)
225 pthread_mutex_lock (lock);
228 void
229 gomp_unset_lock_25 (omp_lock_25_t *lock)
231 pthread_mutex_unlock (lock);
235 gomp_test_lock_25 (omp_lock_25_t *lock)
237 return pthread_mutex_trylock (lock) == 0;
240 void
241 gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock)
243 pthread_mutex_init (&lock->lock, NULL);
244 lock->owner = (pthread_t) 0;
245 lock->count = 0;
248 void
249 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock)
251 pthread_mutex_destroy (&lock->lock);
254 void
255 gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock)
257 pthread_t me = pthread_self ();
259 if (lock->owner != me)
261 pthread_mutex_lock (&lock->lock);
262 lock->owner = me;
265 lock->count++;
268 void
269 gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock)
271 lock->count--;
273 if (lock->count == 0)
275 lock->owner = (pthread_t) 0;
276 pthread_mutex_unlock (&lock->lock);
281 gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock)
283 pthread_t me = pthread_self ();
285 if (lock->owner != me)
287 if (pthread_mutex_trylock (&lock->lock) != 0)
288 return 0;
289 lock->owner = me;
292 return ++lock->count;
295 omp_lock_symver (omp_init_lock)
296 omp_lock_symver (omp_destroy_lock)
297 omp_lock_symver (omp_set_lock)
298 omp_lock_symver (omp_unset_lock)
299 omp_lock_symver (omp_test_lock)
300 omp_lock_symver (omp_init_nest_lock)
301 omp_lock_symver (omp_destroy_nest_lock)
302 omp_lock_symver (omp_set_nest_lock)
303 omp_lock_symver (omp_unset_nest_lock)
304 omp_lock_symver (omp_test_nest_lock)
306 #else
308 ialias (omp_init_lock)
309 ialias (omp_init_nest_lock)
310 ialias (omp_destroy_lock)
311 ialias (omp_destroy_nest_lock)
312 ialias (omp_set_lock)
313 ialias (omp_set_nest_lock)
314 ialias (omp_unset_lock)
315 ialias (omp_unset_nest_lock)
316 ialias (omp_test_lock)
317 ialias (omp_test_nest_lock)
319 #endif