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
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. */
36 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
38 gomp_init_lock_30 (omp_lock_t
*lock
)
40 pthread_mutex_init (lock
, NULL
);
44 gomp_destroy_lock_30 (omp_lock_t
*lock
)
46 pthread_mutex_destroy (lock
);
50 gomp_set_lock_30 (omp_lock_t
*lock
)
52 pthread_mutex_lock (lock
);
56 gomp_unset_lock_30 (omp_lock_t
*lock
)
58 pthread_mutex_unlock (lock
);
62 gomp_test_lock_30 (omp_lock_t
*lock
)
64 return pthread_mutex_trylock (lock
) == 0;
68 gomp_init_nest_lock_30 (omp_nest_lock_t
*lock
)
70 pthread_mutex_init (&lock
->lock
, NULL
);
76 gomp_destroy_nest_lock_30 (omp_nest_lock_t
*lock
)
78 pthread_mutex_destroy (&lock
->lock
);
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
);
96 gomp_unset_nest_lock_30 (omp_nest_lock_t
*lock
)
100 if (lock
->count
== 0)
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)
119 return ++lock
->count
;
125 gomp_init_lock_30 (omp_lock_t
*lock
)
127 sem_init (lock
, 0, 1);
131 gomp_destroy_lock_30 (omp_lock_t
*lock
)
137 gomp_set_lock_30 (omp_lock_t
*lock
)
139 while (sem_wait (lock
) != 0)
144 gomp_unset_lock_30 (omp_lock_t
*lock
)
150 gomp_test_lock_30 (omp_lock_t
*lock
)
152 return sem_trywait (lock
) == 0;
156 gomp_init_nest_lock_30 (omp_nest_lock_t
*lock
)
158 sem_init (&lock
->lock
, 0, 1);
164 gomp_destroy_nest_lock_30 (omp_nest_lock_t
*lock
)
166 sem_destroy (&lock
->lock
);
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)
184 gomp_unset_nest_lock_30 (omp_nest_lock_t
*lock
)
186 if (--lock
->count
== 0)
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)
205 return ++lock
->count
;
209 #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
211 gomp_init_lock_25 (omp_lock_25_t
*lock
)
213 pthread_mutex_init (lock
, NULL
);
217 gomp_destroy_lock_25 (omp_lock_25_t
*lock
)
219 pthread_mutex_destroy (lock
);
223 gomp_set_lock_25 (omp_lock_25_t
*lock
)
225 pthread_mutex_lock (lock
);
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;
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;
249 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t
*lock
)
251 pthread_mutex_destroy (&lock
->lock
);
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
);
269 gomp_unset_nest_lock_25 (omp_nest_lock_25_t
*lock
)
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)
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
)
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
)