1 /* Copyright (C) 2005, 2008, 2009, 2012 Free Software Foundation, Inc.
2 Contributed by Richard Henderson <rth@redhat.com>.
4 This file is part of the GNU OpenMP Library (libgomp).
6 Libgomp is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
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 General Public License for
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* This is the default PTHREADS implementation of the public OpenMP
28 Because OpenMP uses different entry points for normal and recursive
29 locks, and pthreads uses only one entry point, a system may be able
30 to do better and streamline the locking as well as reduce the size
31 of the types exported. */
33 /* We need Unix98 extensions to get recursive locks. */
34 #define _XOPEN_SOURCE 500
38 #ifdef HAVE_BROKEN_POSIX_SEMAPHORES
40 gomp_init_lock_30 (omp_lock_t
*lock
)
42 pthread_mutex_init (lock
, NULL
);
46 gomp_destroy_lock_30 (omp_lock_t
*lock
)
48 pthread_mutex_destroy (lock
);
52 gomp_set_lock_30 (omp_lock_t
*lock
)
54 pthread_mutex_lock (lock
);
58 gomp_unset_lock_30 (omp_lock_t
*lock
)
60 pthread_mutex_unlock (lock
);
64 gomp_test_lock_30 (omp_lock_t
*lock
)
66 return pthread_mutex_trylock (lock
) == 0;
70 gomp_init_nest_lock_30 (omp_nest_lock_t
*lock
)
72 pthread_mutex_init (&lock
->lock
, NULL
);
78 gomp_destroy_nest_lock_30 (omp_nest_lock_t
*lock
)
80 pthread_mutex_destroy (&lock
->lock
);
84 gomp_set_nest_lock_30 (omp_nest_lock_t
*lock
)
86 void *me
= gomp_icv (true);
88 if (lock
->owner
!= me
)
90 pthread_mutex_lock (&lock
->lock
);
97 gomp_unset_nest_lock_30 (omp_nest_lock_t
*lock
)
99 if (--lock
->count
== 0)
102 pthread_mutex_unlock (&lock
->lock
);
107 gomp_test_nest_lock_30 (omp_nest_lock_t
*lock
)
109 void *me
= gomp_icv (true);
111 if (lock
->owner
!= me
)
113 if (pthread_mutex_trylock (&lock
->lock
) != 0)
118 return ++lock
->count
;
124 gomp_init_lock_30 (omp_lock_t
*lock
)
126 sem_init (lock
, 0, 1);
130 gomp_destroy_lock_30 (omp_lock_t
*lock
)
136 gomp_set_lock_30 (omp_lock_t
*lock
)
138 while (sem_wait (lock
) != 0)
143 gomp_unset_lock_30 (omp_lock_t
*lock
)
149 gomp_test_lock_30 (omp_lock_t
*lock
)
151 return sem_trywait (lock
) == 0;
155 gomp_init_nest_lock_30 (omp_nest_lock_t
*lock
)
157 sem_init (&lock
->lock
, 0, 1);
163 gomp_destroy_nest_lock_30 (omp_nest_lock_t
*lock
)
165 sem_destroy (&lock
->lock
);
169 gomp_set_nest_lock_30 (omp_nest_lock_t
*lock
)
171 void *me
= gomp_icv (true);
173 if (lock
->owner
!= me
)
175 while (sem_wait (&lock
->lock
) != 0)
183 gomp_unset_nest_lock_30 (omp_nest_lock_t
*lock
)
185 if (--lock
->count
== 0)
188 sem_post (&lock
->lock
);
193 gomp_test_nest_lock_30 (omp_nest_lock_t
*lock
)
195 void *me
= gomp_icv (true);
197 if (lock
->owner
!= me
)
199 if (sem_trywait (&lock
->lock
) != 0)
204 return ++lock
->count
;
208 #ifdef LIBGOMP_GNU_SYMBOL_VERSIONING
210 gomp_init_lock_25 (omp_lock_25_t
*lock
)
212 pthread_mutex_init (lock
, NULL
);
216 gomp_destroy_lock_25 (omp_lock_25_t
*lock
)
218 pthread_mutex_destroy (lock
);
222 gomp_set_lock_25 (omp_lock_25_t
*lock
)
224 pthread_mutex_lock (lock
);
228 gomp_unset_lock_25 (omp_lock_25_t
*lock
)
230 pthread_mutex_unlock (lock
);
234 gomp_test_lock_25 (omp_lock_25_t
*lock
)
236 return pthread_mutex_trylock (lock
) == 0;
240 gomp_init_nest_lock_25 (omp_nest_lock_25_t
*lock
)
242 pthread_mutexattr_t attr
;
244 pthread_mutexattr_init (&attr
);
245 pthread_mutexattr_settype (&attr
, PTHREAD_MUTEX_RECURSIVE
);
246 pthread_mutex_init (&lock
->lock
, &attr
);
248 pthread_mutexattr_destroy (&attr
);
252 gomp_destroy_nest_lock_25 (omp_nest_lock_25_t
*lock
)
254 pthread_mutex_destroy (&lock
->lock
);
258 gomp_set_nest_lock_25 (omp_nest_lock_25_t
*lock
)
260 pthread_mutex_lock (&lock
->lock
);
265 gomp_unset_nest_lock_25 (omp_nest_lock_25_t
*lock
)
268 pthread_mutex_unlock (&lock
->lock
);
272 gomp_test_nest_lock_25 (omp_nest_lock_25_t
*lock
)
274 if (pthread_mutex_trylock (&lock
->lock
) == 0)
275 return ++lock
->count
;
279 omp_lock_symver (omp_init_lock
)
280 omp_lock_symver (omp_destroy_lock
)
281 omp_lock_symver (omp_set_lock
)
282 omp_lock_symver (omp_unset_lock
)
283 omp_lock_symver (omp_test_lock
)
284 omp_lock_symver (omp_init_nest_lock
)
285 omp_lock_symver (omp_destroy_nest_lock
)
286 omp_lock_symver (omp_set_nest_lock
)
287 omp_lock_symver (omp_unset_nest_lock
)
288 omp_lock_symver (omp_test_nest_lock
)
292 ialias (omp_init_lock
)
293 ialias (omp_init_nest_lock
)
294 ialias (omp_destroy_lock
)
295 ialias (omp_destroy_nest_lock
)
296 ialias (omp_set_lock
)
297 ialias (omp_set_nest_lock
)
298 ialias (omp_unset_lock
)
299 ialias (omp_unset_nest_lock
)
300 ialias (omp_test_lock
)
301 ialias (omp_test_nest_lock
)