1 /* Copyright (C) 2006, 2008, 2009 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 General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
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 General Public License for
15 Under Section 7 of GPL version 3, you are granted additional
16 permissions described in the GCC Runtime Library Exception, version
17 3.1, as published by the Free Software Foundation.
19 You should have received a copy of the GNU General Public License and
20 a copy of the GCC Runtime Library Exception along with this program;
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 <http://www.gnu.org/licenses/>. */
24 /* This is the POSIX95 implementation of the public OpenMP locking primitives.
26 Because OpenMP uses different entry points for normal and recursive
27 locks, and pthreads uses only one entry point, a system may be able
28 to do better and streamline the locking as well as reduce the size
29 of the types exported. */
33 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
35 gomp_init_lock_30 (omp_lock_t *lock)
37 pthread_mutex_init (lock, NULL);
41 gomp_destroy_lock_30 (omp_lock_t *lock)
43 pthread_mutex_destroy (lock);
47 gomp_set_lock_30 (omp_lock_t *lock)
49 pthread_mutex_lock (lock);
53 gomp_unset_lock_30 (omp_lock_t *lock)
55 pthread_mutex_unlock (lock);
59 gomp_test_lock_30 (omp_lock_t *lock)
61 return pthread_mutex_trylock (lock) == 0;
65 gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
67 pthread_mutex_init (&lock->lock, NULL);
73 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
75 pthread_mutex_destroy (&lock->lock);
79 gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
81 void *me = gomp_icv (true);
83 if (lock->owner != me)
85 pthread_mutex_lock (&lock->lock);
93 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
100 pthread_mutex_unlock (&lock->lock);
105 gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
107 void *me = gomp_icv (true);
109 if (lock->owner != me)
111 if (pthread_mutex_trylock (&lock->lock) != 0)
116 return ++lock->count;
122 gomp_init_lock_30 (omp_lock_t *lock)
124 sem_init (lock, 0, 1);
128 gomp_destroy_lock_30 (omp_lock_t *lock)
134 gomp_set_lock_30 (omp_lock_t *lock)
136 while (sem_wait (lock) != 0)
141 gomp_unset_lock_30 (omp_lock_t *lock)
147 gomp_test_lock_30 (omp_lock_t *lock)
149 return sem_trywait (lock) == 0;
153 gomp_init_nest_lock_30 (omp_nest_lock_t *lock)
155 sem_init (&lock->lock, 0, 1);
161 gomp_destroy_nest_lock_30 (omp_nest_lock_t *lock)
163 sem_destroy (&lock->lock);
167 gomp_set_nest_lock_30 (omp_nest_lock_t *lock)
169 void *me = gomp_icv (true);
171 if (lock->owner != me)
173 while (sem_wait (&lock->lock) != 0)
181 gomp_unset_nest_lock_30 (omp_nest_lock_t *lock)
183 if (--lock->count == 0)
186 sem_post (&lock->lock);
191 gomp_test_nest_lock_30 (omp_nest_lock_t *lock)
193 void *me = gomp_icv (true);
195 if (lock->owner != me)
197 if (sem_trywait (&lock->lock) != 0)
202 return ++lock->count;
206 #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
208 gomp_init_lock_25 (omp_lock_25_t *lock)
210 pthread_mutex_init (lock, NULL);
214 gomp_destroy_lock_25 (omp_lock_25_t *lock)
216 pthread_mutex_destroy (lock);
220 gomp_set_lock_25 (omp_lock_25_t *lock)
222 pthread_mutex_lock (lock);
226 gomp_unset_lock_25 (omp_lock_25_t *lock)
228 pthread_mutex_unlock (lock);
232 gomp_test_lock_25 (omp_lock_25_t *lock)
234 return pthread_mutex_trylock (lock) == 0;
238 gomp_init_nest_lock_25 (omp_nest_lock_25_t *lock)
240 pthread_mutex_init (&lock->lock, NULL);
241 lock->owner = (pthread_t) 0;
246 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t *lock)
248 pthread_mutex_destroy (&lock->lock);
252 gomp_set_nest_lock_25 (omp_nest_lock_25_t *lock)
254 pthread_t me = pthread_self ();
256 if (lock->owner != me)
258 pthread_mutex_lock (&lock->lock);
266 gomp_unset_nest_lock_25 (omp_nest_lock_25_t *lock)
270 if (lock->count == 0)
272 lock->owner = (pthread_t) 0;
273 pthread_mutex_unlock (&lock->lock);
278 gomp_test_nest_lock_25 (omp_nest_lock_25_t *lock)
280 pthread_t me = pthread_self ();
282 if (lock->owner != me)
284 if (pthread_mutex_trylock (&lock->lock) != 0)
289 return ++lock->count;
292 omp_lock_symver (omp_init_lock)
293 omp_lock_symver (omp_destroy_lock)
294 omp_lock_symver (omp_set_lock)
295 omp_lock_symver (omp_unset_lock)
296 omp_lock_symver (omp_test_lock)
297 omp_lock_symver (omp_init_nest_lock)
298 omp_lock_symver (omp_destroy_nest_lock)
299 omp_lock_symver (omp_set_nest_lock)
300 omp_lock_symver (omp_unset_nest_lock)
301 omp_lock_symver (omp_test_nest_lock)
305 ialias (omp_init_lock)
306 ialias (omp_init_nest_lock)
307 ialias (omp_destroy_lock)
308 ialias (omp_destroy_nest_lock)
309 ialias (omp_set_lock)
310 ialias (omp_set_nest_lock)
311 ialias (omp_unset_lock)
312 ialias (omp_unset_nest_lock)
313 ialias (omp_test_lock)
314 ialias (omp_test_nest_lock)